Source code: org/enhydra/kelp/common/bridge/MetaDataHandlerV2.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22
23 package org.enhydra.kelp.common.bridge;
24
25 // XMLC imports
26 import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptionsParser;
27 import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions;
28 import org.enhydra.xml.xmlc.commands.options.OptionSet;
29 import org.enhydra.xml.xmlc.commands.options.OptionsParser;
30 import org.enhydra.xml.xmlc.XMLCException;
31 import org.enhydra.xml.xmlc.metadata.DeleteElement;
32 import org.enhydra.xml.xmlc.metadata.CompileOptions;
33 import org.enhydra.xml.xmlc.metadata.DocumentClass;
34 import org.enhydra.xml.xmlc.metadata.DOMEdits;
35 import org.enhydra.xml.xmlc.metadata.MetaData;
36 import org.enhydra.xml.xmlc.metadata.MetaDataDocument;
37
38 // ToolBox imports
39 import org.enhydra.tool.common.PathHandle;
40
41 // Kelp imports
42 import org.enhydra.kelp.common.Constants;
43 import org.enhydra.kelp.common.node.OtterXMLCNode;
44 import org.enhydra.kelp.common.wizard.xmlc.Reporter;
45
46 // Standard imports
47 import java.io.File;
48 import java.io.IOException;
49 import java.io.PrintWriter;
50 import java.util.List;
51 import java.util.Arrays;
52
53 //
54 public class MetaDataHandlerV2 implements MetaDataHandler {
55
56 // strings not to be resourced
57 private final String[] CUSTOM_INPUT_DOC_TYPES = {
58 "chtml", "htm", "html", "xhtml"
59 }; // nores
60 private final String FILE_PROTOCOL = "file:"; // nores
61
62 //
63 private MetaData metaData = null;
64
65 //
66 public MetaDataHandlerV2() {
67 MetaDataDocument doc = new MetaDataDocument();
68
69 metaData = doc.getMetaData();
70 metaData.setCompileOptions(new CompileOptions(doc));
71 metaData.setDocumentClass(new DocumentClass(doc));
72 metaData.setDOMEdits(new DOMEdits(doc));
73 }
74
75 // Compiler Options
76 public boolean getCompileSource() {
77 return metaData.getCompileOptions().getCompileSource();
78 }
79
80 public void setCompileSource(boolean b) {
81 setCompileSource(metaData, b);
82 }
83
84 private void setCompileSource(MetaData meta, boolean b) {
85 meta.getCompileOptions().setCompileSource(b);
86 }
87
88 public String getDocumentOutput() {
89 return PathHandle.createPathString(metaData.getCompileOptions().getDocumentOutput());
90 }
91
92 public void setDocumentOutput(String s) {
93 setDocumentOutput(metaData, s);
94 }
95
96 private void setDocumentOutput(MetaData meta, String s) {
97 String path = PathHandle.createPathString(s);
98
99 meta.getCompileOptions().setDocumentOutput(path);
100 }
101
102 public String getInputDocument() {
103 String doc = null;
104
105 doc = metaData.getCompileOptions().getInputDocument();
106 if (doc.startsWith(FILE_PROTOCOL)) {
107 doc = doc.substring(FILE_PROTOCOL.length());
108 }
109 return PathHandle.createPathString(doc);
110 }
111
112 public void setInputDocument(String s) {
113 setInputDocument(metaData, s);
114 }
115
116 private void setInputDocument(MetaData meta, String s) {
117 PathHandle handle = null;
118 StringBuffer buf = new StringBuffer();
119
120 handle = PathHandle.createPathHandle(s);
121 if (!handle.hasExtension(CUSTOM_INPUT_DOC_TYPES)) {
122 buf.append(FILE_PROTOCOL);
123 }
124 buf.append(handle.getPath());
125 meta.getCompileOptions().setInputDocument(buf.toString());
126 }
127
128 public boolean getKeepGeneratedSource() {
129 return metaData.getCompileOptions().getKeepGeneratedSource();
130 }
131
132 public void setKeepGeneratedSource(boolean b) {
133 setKeepGeneratedSource(metaData, b);
134 }
135
136 private void setKeepGeneratedSource(MetaData meta, boolean b) {
137 meta.getCompileOptions().setKeepGeneratedSource(b);
138 }
139
140 public boolean getPrintAccessorInfo() {
141 return metaData.getCompileOptions().getPrintAccessorInfo();
142 }
143
144 public void setPrintAccessorInfo(boolean b) {
145 setPrintAccessorInfo(metaData, b);
146 }
147
148 private void setPrintAccessorInfo(MetaData meta, boolean b) {
149 meta.getCompileOptions().setPrintAccessorInfo(b);
150 }
151
152 public boolean getPrintDocumentInfo() {
153 return metaData.getCompileOptions().getPrintDocumentInfo();
154 }
155
156 public void setPrintDocumentInfo(boolean b) {
157 setPrintDocumentInfo(metaData, b);
158 }
159
160 private void setPrintDocumentInfo(MetaData meta, boolean b) {
161 meta.getCompileOptions().setPrintDocumentInfo(b);
162 }
163
164 public boolean getPrintDOM() {
165 return metaData.getCompileOptions().getPrintDOM();
166 }
167
168 public void setPrintDOM(boolean b) {
169 setPrintDOM(metaData, b);
170 }
171
172 private void setPrintDOM(MetaData meta, boolean b) {
173 meta.getCompileOptions().setPrintDOM(b);
174 }
175
176 public boolean getPrintParseInfo() {
177 return metaData.getCompileOptions().getPrintParseInfo();
178 }
179
180 public void setPrintParseInfo(boolean b) {
181 setPrintParseInfo(metaData, b);
182 }
183
184 private void setPrintParseInfo(MetaData meta, boolean b) {
185 meta.getCompileOptions().setPrintParseInfo(b);
186 }
187
188 public boolean getPrintVersion() {
189 return metaData.getCompileOptions().getPrintVersion();
190 }
191
192 public void setPrintVersion(boolean b) {
193 setPrintVersion(metaData, b);
194 }
195
196 private void setPrintVersion(MetaData meta, boolean b) {
197 meta.getCompileOptions().setPrintVersion(b);
198 }
199
200 public boolean getVerbose() {
201 return metaData.getCompileOptions().getVerbose();
202 }
203
204 public void setVerbose(boolean b) {
205 setVerbose(metaData, b);
206 }
207
208 private void setVerbose(MetaData meta, boolean b) {
209 meta.getCompileOptions().setVerbose(b);
210 }
211
212 // Document Class
213 public String getClassName() {
214 return metaData.getDocumentClass().getName();
215 }
216
217 public void setClassName(String n) {
218 setClassName(metaData, n);
219 }
220
221 private void setClassName(MetaData meta, String n) {
222 meta.getDocumentClass().setName(n);
223 }
224
225 public File getJavaClassSource() {
226 return metaData.getDocumentClass().getJavaClassSource();
227 }
228
229 public void setJavaClassSource(File f, OtterXMLCNode node) {
230 setJavaClassSource(metaData, f, node);
231 }
232
233 private void setJavaClassSource(MetaData meta, File f,
234 OtterXMLCNode node) {
235 String outputRoot = node.getGenerateToRoot();
236 String path = f.getAbsolutePath();
237 MetaDataDocument doc = meta.getDocument();
238
239 outputRoot = outputRoot.replace('\\', '/').trim();
240 path = path.replace('\\', '/').trim();
241 String name = pathToClass(outputRoot, path);
242
243 meta.getDocumentClass().setName(name);
244 meta.getCompileOptions().setClassOutputRoot(outputRoot);
245 meta.getCompileOptions().setSourceOutputRoot(outputRoot);
246 try {
247 doc.completeModifications();
248 } catch (XMLCException e) {
249 e.printStackTrace();
250 }
251 }
252
253 public File getJavaInterfaceSource() {
254 return metaData.getDocumentClass().getJavaInterfaceSource();
255 }
256
257 public void setJavaInterfaceSource(File f, OtterXMLCNode node) {
258 setJavaInterfaceSource(metaData);
259 }
260
261 private void setJavaInterfaceSource(MetaData meta) {
262
263 // the rest should be automatic.
264 try {
265 meta.getDocument().completeModifications();
266 } catch (XMLCException e) {
267 e.printStackTrace();
268 }
269 }
270
271 public String getPackageName() {
272 String name = null;
273
274 try {
275 name = metaData.getDocumentClass().getPackageName();
276 } catch (NullPointerException e) {
277
278 // Workaround?
279 name = null;
280 }
281 return name;
282 }
283
284 public boolean getRecompilation() {
285 return metaData.getDocumentClass().getRecompilation();
286 }
287
288 public void setRecompilation(boolean b) {
289 setRecompilation(metaData, b);
290 }
291
292 private void setRecompilation(MetaData meta, boolean b) {
293 meta.getDocumentClass().setRecompilation(b);
294 }
295
296 // DOM Edits
297 public Object[] getDeleteElements() {
298 return metaData.getDOMEdits().getDeleteElements();
299 }
300
301 private void mergeDeleteElements(MetaData meta,
302 DeleteElement[] elements) {
303 List eList = Arrays.asList(meta.getDOMEdits().getDeleteElements());
304
305 for (int i = 0; i < elements.length; i++) {
306 if (!eList.contains(elements[i])) {
307 meta.getDOMEdits().addDeleteElement(elements[i]);
308 }
309 }
310 }
311
312 public Object[] getURLMappings() {
313 return metaData.getDOMEdits().getURLMappings();
314 }
315
316 // Other
317 public Object getMetaData() {
318 return metaData;
319 }
320
321 public void parse(String[] files, String[] args, PrintWriter writer,
322 OtterXMLCNode node) throws XMLCException, IOException {
323 XMLCOptions opParser = null;
324 Reporter reporter = null;
325 String[] parseArgs = new String[files.length + args.length + 1];
326 MetaData newMeta = null;
327
328 reporter = new Reporter(writer);
329 for (int i = 0; i < args.length; i++) {
330 parseArgs[i] = args[i];
331 }
332 for (int i = args.length; i < (args.length + files.length); i++) {
333 parseArgs[i] = files[(i - args.length)];
334 }
335 metaData.getDocument().completeModifications();
336 if (parseArgs.length > 1) {
337 parseArgs[parseArgs.length - 1] = node.getFilePath().replace('\\',
338 '/');
339 try {
340 opParser = new XMLCOptions();
341 newMeta = opParser.parse(parseArgs, reporter);
342 mergeMetaData(newMeta, node);
343 } catch (Exception e) {
344 e.printStackTrace();
345 if (e instanceof XMLCException) {
346 throw (XMLCException) e;
347 } else if (e instanceof IOException) {
348 throw (IOException) e;
349 } else {
350 throw new XMLCException(e);
351 }
352 }
353 }
354 }
355
356 private void mergeMetaData(MetaData newMeta, OtterXMLCNode node) {
357
358 // merge group 1
359 setKeepGeneratedSource(newMeta, getKeepGeneratedSource());
360 setPrintAccessorInfo(newMeta, getPrintAccessorInfo());
361 setPrintDOM(newMeta, getPrintDOM());
362 setPrintParseInfo(newMeta, getPrintParseInfo());
363 setPrintVersion(newMeta, getPrintVersion());
364 setRecompilation(newMeta, getRecompilation());
365 setVerbose(newMeta, getVerbose());
366
367 // merge group 2
368 setClassName(newMeta, getClassName());
369 setCompileSource(newMeta, getCompileSource());
370 mergeDeleteElements(newMeta, (DeleteElement[]) getDeleteElements());
371 setDocumentOutput(newMeta, getDocumentOutput());
372 setInputDocument(newMeta, getInputDocument().replace('\\', '/'));
373 setJavaClassSource(newMeta, getJavaClassSource(), node);
374 setJavaInterfaceSource(newMeta);
375 this.metaData = newMeta;
376 }
377
378 public void save(File op) throws IOException {
379 metaData.getDocument().serialize(op);
380 }
381
382 // Private Stuff
383 private String pathToClass(String outputRoot, String in) {
384 String out = in;
385 int index = 0;
386
387 if (out.startsWith(outputRoot)) {
388 out = out.substring(outputRoot.length());
389 }
390 index = out.lastIndexOf('.' + Constants.TYPE_JAVA);
391 if (index > -1) {
392 out = out.substring(0, index);
393 }
394 out = out.replace('\\', '.');
395 out = out.replace('/', '.');
396 while ((out.length() > 0) && (out.charAt(0) == '.')) {
397 out = out.substring(1);
398 }
399 while ((out.length() > 0) && (out.charAt(out.length()-1) == '.')) {
400 out = out.substring(0, out.length() - 1);
401 }
402 return out;
403 }
404
405 }