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<As3Type> {
040    
041            private final JavaType javaType;
042            private final As3Type 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.getAs3Type();
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            public As3Type getTargetType() {
064                    return targetType;
065            }
066    
067            public String getDescription() {
068                    return file.toString();
069            }
070    
071            public Template getTemplate() {
072                    return template;
073            }
074    
075            public File getDir() {
076                    return dir;
077            }
078    
079            public File getFile() {
080                    return file;
081            }
082    
083            public boolean isOutdated() {
084                    return outdated;
085            }
086    
087            public String getMessage() {
088                    return message;
089            }
090    
091            public OutputStream openStream() throws IOException {
092                    File parent = file.getParentFile();
093                    if (parent != null)
094                            parent.mkdirs();
095                    return new BufferedOutputStream(new FileOutputStream(file));
096            }
097    
098            public PrintWriter openWriter() throws IOException {
099                    File parent = file.getParentFile();
100                    if (parent != null)
101                            parent.mkdirs();
102                    return new PrintWriter(new BufferedWriter(new FileWriter(file)));
103            }
104    }