1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34
35
36
37
38
39
40
41
42
43 public class JAXXCompilerFile {
44
45
46 private static final Log log = LogFactory.getLog(JAXXCompilerFile.class);
47
48
49 protected final File basedir;
50
51
52 private final String relativePath;
53
54
55 protected File jaxxFile;
56
57
58 protected File cssFile;
59
60
61 protected String className;
62
63
64 protected JAXXCompiler compiler;
65
66
67
68
69
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
107 path += "." + extension;
108 }
109
110
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
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
147
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
198
199
200
201 public void setCompiler(JAXXCompiler compiler) {
202 this.compiler = compiler;
203 }
204 }