001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 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     */
022    
023    package org.granite.generator.as3;
024    
025    import java.io.BufferedOutputStream;
026    import java.io.BufferedWriter;
027    import java.io.File;
028    import java.io.FileOutputStream;
029    import java.io.FileWriter;
030    import java.io.IOException;
031    import java.io.OutputStream;
032    import java.io.PrintWriter;
033    
034    import org.granite.generator.Output;
035    import org.granite.generator.Template;
036    import org.granite.generator.as3.reflect.JavaType;
037    
038    /**
039     * @author Franck WOLFF
040     */
041    public class JavaAs3Output implements Output<ClientType> {
042    
043            private final JavaType javaType;
044            private final ClientType targetType;
045            private final Template template;
046            private final File dir;
047            private final File file;
048            private final boolean outdated;
049            private final String message;
050            
051            public JavaAs3Output(JavaType javaType, Template template, File dir, File file, boolean outdated, String message) {
052                    this.javaType = javaType;
053                    this.targetType = (javaType != null ? javaType.getClientType() : null);
054                    this.template = template;
055                    this.dir = dir;
056                    this.file = file;
057                    this.outdated = outdated;
058                    this.message = message;
059            }
060    
061            public JavaType getJavaType() {
062                    return javaType;
063            }
064    
065            @Override
066            public ClientType getTargetType() {
067                    return targetType;
068            }
069    
070            @Override
071            public String getDescription() {
072                    return file.toString();
073            }
074    
075            public Template getTemplate() {
076                    return template;
077            }
078    
079            public File getDir() {
080                    return dir;
081            }
082    
083            public File getFile() {
084                    return file;
085            }
086    
087            @Override
088            public boolean isOutdated() {
089                    return outdated;
090            }
091    
092            @Override
093            public String getMessage() {
094                    return message;
095            }
096    
097            public OutputStream openStream() throws IOException {
098                    File parent = file.getParentFile();
099                    if (parent != null)
100                            parent.mkdirs();
101                    return new BufferedOutputStream(new FileOutputStream(file));
102            }
103    
104            public PrintWriter openWriter() throws IOException {
105                    File parent = file.getParentFile();
106                    if (parent != null)
107                            parent.mkdirs();
108                    return new PrintWriter(new BufferedWriter(new FileWriter(file)));
109            }
110    }