001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.generator.as3;
023
024import java.io.BufferedOutputStream;
025import java.io.BufferedWriter;
026import java.io.File;
027import java.io.FileOutputStream;
028import java.io.FileWriter;
029import java.io.IOException;
030import java.io.OutputStream;
031import java.io.PrintWriter;
032
033import org.granite.generator.Output;
034import org.granite.generator.Template;
035import org.granite.generator.as3.reflect.JavaType;
036
037/**
038 * @author Franck WOLFF
039 */
040public class JavaAs3Output implements Output<ClientType> {
041
042        private final JavaType javaType;
043        private final ClientType targetType;
044        private final Template template;
045        private final File dir;
046        private final File file;
047        private final boolean outdated;
048        private final String message;
049        
050        public JavaAs3Output(JavaType javaType, Template template, File dir, File file, boolean outdated, String message) {
051                this.javaType = javaType;
052                this.targetType = (javaType != null ? javaType.getClientType() : null);
053                this.template = template;
054                this.dir = dir;
055                this.file = file;
056                this.outdated = outdated;
057                this.message = message;
058        }
059
060        public JavaType getJavaType() {
061                return javaType;
062        }
063
064        @Override
065        public ClientType getTargetType() {
066                return targetType;
067        }
068
069        @Override
070        public String getDescription() {
071                return file.toString();
072        }
073
074        public Template getTemplate() {
075                return template;
076        }
077
078        public File getDir() {
079                return dir;
080        }
081
082        public File getFile() {
083                return file;
084        }
085
086        @Override
087        public boolean isOutdated() {
088                return outdated;
089        }
090
091        @Override
092        public String getMessage() {
093                return message;
094        }
095
096        public OutputStream openStream() throws IOException {
097                File parent = file.getParentFile();
098                if (parent != null)
099                        parent.mkdirs();
100                return new BufferedOutputStream(new FileOutputStream(file));
101        }
102
103        public PrintWriter openWriter() throws IOException {
104                File parent = file.getParentFile();
105                if (parent != null)
106                        parent.mkdirs();
107                return new PrintWriter(new BufferedWriter(new FileWriter(file)));
108        }
109}