View Javadoc
1   /*
2    * #%L
3    * JAXX :: Compiler
4    * %%
5    * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package jaxx.compiler;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.nuiton.util.FileUtil;
27  
28  import java.io.File;
29  import java.net.MalformedURLException;
30  import java.net.URL;
31  
32  /**
33   * Represents a file to be treated by the {@link JAXXCompiler}.
34   *
35   * It contains informations about jaxx file, ident css, class name,...
36   *
37   * <b>Note:</b> This class will be more used in next version (will have the
38   * compiler it-self, symbols table,...).
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 2.0.2
42   */
43  public class JAXXCompilerFile {
44  
45      /** Logger */
46      private static final Log log = LogFactory.getLog(JAXXCompilerFile.class);
47  
48      /** root directory of the source roots. */
49      protected final File basedir;
50  
51      /** relative path from {@link #basedir} to {@link #jaxxFile}. */
52      private final String relativePath;
53  
54      /** location of the jaxx file */
55      protected File jaxxFile;
56  
57      /** location of the ident css file */
58      protected File cssFile;
59  
60      /** full qualified name of the jaxx file class */
61      protected String className;
62  
63      /** compiler associated to the file */
64      protected JAXXCompiler compiler;
65  
66      /**
67       * Css file extension.
68       *
69       * @see CompilerConfiguration#getCssExtension()
70       */
71      protected final String cssExtension;
72  
73      public JAXXCompilerFile(File basedir, File jaxxFile, String cssExtension) {
74          this.basedir = basedir;
75          this.jaxxFile = jaxxFile;
76          this.cssExtension = cssExtension;
77          String absolutePath = jaxxFile.getAbsolutePath();
78          String baseAbsolutePath = basedir.getAbsolutePath();
79          if (!absolutePath.startsWith(baseAbsolutePath)) {
80              throw new IllegalStateException(
81                      "Jaxx file " + jaxxFile + " is not in basedir " + basedir);
82          }
83  
84          relativePath = absolutePath.substring(baseAbsolutePath.length() + 1);
85          if (log.isDebugEnabled()) {
86              log.debug("relativePath = " + relativePath);
87          }
88      }
89  
90      public JAXXCompilerFile(File jaxxFile, String className, String cssExtension) {
91  
92          this.jaxxFile = jaxxFile;
93          this.className = className;
94          this.cssExtension = cssExtension;
95  
96          String extension = FileUtil.extension(jaxxFile);
97          String[] paths = className.split("\\.");
98          File basedir = jaxxFile;
99          for (int i = paths.length - 1; i > -1; i--) {
100             if (basedir == null) {
101                 throw new IllegalStateException("Could not find base dir for " + jaxxFile + " according to fqn " + className);
102             }
103 
104             String path = paths[i];
105             if (basedir.equals(jaxxFile)) {
106                 // first in loop
107                 path += "." + extension;
108             }
109 
110             // check path = base filename
111             if (!path.equals(basedir.getName())) {
112                 throw new IllegalStateException("Should have found directory " + path + ", but was " + basedir.getName());
113             }
114 
115             basedir = basedir.getParentFile();
116         }
117 
118         if (log.isDebugEnabled()) {
119             log.debug("basedir = " + basedir);
120         }
121 
122         // must guess the base directory and relative path
123 
124         String relativePath = jaxxFile.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1);
125 
126         if (log.isDebugEnabled()) {
127             log.debug("relative path = " + relativePath);
128         }
129 
130         this.basedir = basedir;
131         this.relativePath = relativePath;
132     }
133 
134     public File getBasedir() {
135         return basedir;
136     }
137 
138     public String getRelativePath() {
139         return relativePath;
140     }
141 
142     public JAXXCompiler getCompiler() {
143         return compiler;
144     }
145 
146 //    public SymbolTable getSymbolTable() {
147 //        return compiler==null?null:compiler.getSymbolTable();
148 //    }
149 
150     public File getJaxxFile() {
151         if (jaxxFile == null) {
152             jaxxFile = new File(basedir, relativePath);
153         }
154         return jaxxFile;
155     }
156 
157     public URL getJAXXFileURL() {
158         File file = getJaxxFile();
159         try {
160             return file.toURI().toURL();
161         } catch (MalformedURLException e) {
162             throw new IllegalStateException("Url of the jaxx file is malformed... " + file);
163         }
164     }
165 
166     public File getCssFile() {
167         if (cssFile == null) {
168             File file = getJaxxFile();
169             String extension = FileUtil.extension(file);
170             String fileName = file.getName();
171             int length = fileName.length() - extension.length();
172             String identCssFilename = fileName.substring(0, length) + cssExtension;
173             cssFile = new File(file.getParentFile(), identCssFilename);
174         }
175         return cssFile;
176     }
177 
178     public String getClassName() {
179         if (className == null) {
180 
181             className = relativePath.substring(0, relativePath.lastIndexOf("."));
182             className = className.replace(File.separatorChar, '.');
183             className = className.replace('/', '.');
184             className = className.replace('\\', '.');
185             className = className.replace(':', '.');
186 
187         }
188         return className;
189     }
190 
191     public void clear() {
192         if (compiler != null) {
193             compiler.clear();
194         }
195     }
196 
197 //    public void setSymbolTable(SymbolTable symbolTable) {
198 //        this.symbolTable = symbolTable;
199 //    }
200 
201     public void setCompiler(JAXXCompiler compiler) {
202         this.compiler = compiler;
203     }
204 }