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