KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > bridge > MetaDataHandlerV2


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
45 // Standard imports
46
import java.io.File JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Arrays JavaDoc;
51
52 //
53
public class MetaDataHandlerV2 implements MetaDataHandler {
54
55     // strings not to be resourced
56
private final String JavaDoc[] CUSTOM_INPUT_DOC_TYPES = {
57         "chtml", "htm", "html", "xhtml"
58     }; // nores
59
private final String JavaDoc FILE_PROTOCOL = "file:"; // nores
60

61     //
62
private MetaData metaData = null;
63
64     //
65
public MetaDataHandlerV2() {
66         MetaDataDocument doc = new MetaDataDocument();
67
68         metaData = doc.getMetaData();
69         metaData.setCompileOptions(new CompileOptions(doc));
70         metaData.setDocumentClass(new DocumentClass(doc));
71         metaData.setDOMEdits(new DOMEdits(doc));
72     }
73
74     // Compiler Options
75
public boolean getCompileSource() {
76         return metaData.getCompileOptions().getCompileSource();
77     }
78
79     public void setCompileSource(boolean b) {
80         setCompileSource(metaData, b);
81     }
82
83     private void setCompileSource(MetaData meta, boolean b) {
84         meta.getCompileOptions().setCompileSource(b);
85     }
86
87     public String JavaDoc getDocumentOutput() {
88         return PathHandle.createPathString(metaData.getCompileOptions().getDocumentOutput());
89     }
90
91     public void setDocumentOutput(String JavaDoc s) {
92         setDocumentOutput(metaData, s);
93     }
94
95     private void setDocumentOutput(MetaData meta, String JavaDoc s) {
96         String JavaDoc path = PathHandle.createPathString(s);
97
98         meta.getCompileOptions().setDocumentOutput(path);
99     }
100
101     public String JavaDoc getInputDocument() {
102         String JavaDoc doc = null;
103
104         doc = metaData.getCompileOptions().getInputDocument();
105         if (doc.startsWith(FILE_PROTOCOL)) {
106             doc = doc.substring(FILE_PROTOCOL.length());
107         }
108         return PathHandle.createPathString(doc);
109     }
110
111     public void setInputDocument(String JavaDoc s) {
112         setInputDocument(metaData, s);
113     }
114
115     private void setInputDocument(MetaData meta, String JavaDoc s) {
116         PathHandle handle = null;
117         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
118
119         handle = PathHandle.createPathHandle(s);
120         if (!handle.hasExtension(CUSTOM_INPUT_DOC_TYPES)) {
121             buf.append(FILE_PROTOCOL);
122         }
123         buf.append(handle.getPath());
124         meta.getCompileOptions().setInputDocument(buf.toString());
125     }
126
127     public boolean getKeepGeneratedSource() {
128         return metaData.getCompileOptions().getKeepGeneratedSource();
129     }
130
131     public void setKeepGeneratedSource(boolean b) {
132         setKeepGeneratedSource(metaData, b);
133     }
134
135     private void setKeepGeneratedSource(MetaData meta, boolean b) {
136         meta.getCompileOptions().setKeepGeneratedSource(b);
137     }
138
139     public boolean getPrintAccessorInfo() {
140         return metaData.getCompileOptions().getPrintAccessorInfo();
141     }
142
143     public void setPrintAccessorInfo(boolean b) {
144         setPrintAccessorInfo(metaData, b);
145     }
146
147     private void setPrintAccessorInfo(MetaData meta, boolean b) {
148         meta.getCompileOptions().setPrintAccessorInfo(b);
149     }
150
151     public boolean getPrintDocumentInfo() {
152         return metaData.getCompileOptions().getPrintDocumentInfo();
153     }
154
155     public void setPrintDocumentInfo(boolean b) {
156         setPrintDocumentInfo(metaData, b);
157     }
158
159     private void setPrintDocumentInfo(MetaData meta, boolean b) {
160         meta.getCompileOptions().setPrintDocumentInfo(b);
161     }
162
163     public boolean getPrintDOM() {
164         return metaData.getCompileOptions().getPrintDOM();
165     }
166
167     public void setPrintDOM(boolean b) {
168         setPrintDOM(metaData, b);
169     }
170
171     private void setPrintDOM(MetaData meta, boolean b) {
172         meta.getCompileOptions().setPrintDOM(b);
173     }
174
175     public boolean getPrintParseInfo() {
176         return metaData.getCompileOptions().getPrintParseInfo();
177     }
178
179     public void setPrintParseInfo(boolean b) {
180         setPrintParseInfo(metaData, b);
181     }
182
183     private void setPrintParseInfo(MetaData meta, boolean b) {
184         meta.getCompileOptions().setPrintParseInfo(b);
185     }
186
187     public boolean getPrintVersion() {
188         return metaData.getCompileOptions().getPrintVersion();
189     }
190
191     public void setPrintVersion(boolean b) {
192         setPrintVersion(metaData, b);
193     }
194
195     private void setPrintVersion(MetaData meta, boolean b) {
196         meta.getCompileOptions().setPrintVersion(b);
197     }
198
199     public boolean getVerbose() {
200         return metaData.getCompileOptions().getVerbose();
201     }
202
203     public void setVerbose(boolean b) {
204         setVerbose(metaData, b);
205     }
206
207     private void setVerbose(MetaData meta, boolean b) {
208         meta.getCompileOptions().setVerbose(b);
209     }
210
211     // Document Class
212
public String JavaDoc getClassName() {
213         return metaData.getDocumentClass().getName();
214     }
215
216     public void setClassName(String JavaDoc n) {
217         setClassName(metaData, n);
218     }
219
220     private void setClassName(MetaData meta, String JavaDoc n) {
221         meta.getDocumentClass().setName(n);
222     }
223
224     public File JavaDoc getJavaClassSource() {
225         return metaData.getDocumentClass().getJavaClassSource();
226     }
227
228     public void setJavaClassSource(File JavaDoc f, OtterXMLCNode node) {
229         setJavaClassSource(metaData, f, node);
230     }
231
232     private void setJavaClassSource(MetaData meta, File JavaDoc f,
233                                     OtterXMLCNode node) {
234         String JavaDoc outputRoot = node.getGenerateToRoot();
235         String JavaDoc path = f.getAbsolutePath();
236         MetaDataDocument doc = meta.getDocument();
237
238         outputRoot = outputRoot.replace('\\', '/').trim();
239         path = path.replace('\\', '/').trim();
240         String JavaDoc name = pathToClass(outputRoot, path);
241
242         meta.getDocumentClass().setName(name);
243         meta.getCompileOptions().setClassOutputRoot(outputRoot);
244         meta.getCompileOptions().setSourceOutputRoot(outputRoot);
245         try {
246             doc.completeModifications();
247         } catch (XMLCException e) {
248             e.printStackTrace();
249         }
250     }
251
252     public File JavaDoc getJavaInterfaceSource() {
253         return metaData.getDocumentClass().getJavaInterfaceSource();
254     }
255
256     public void setJavaInterfaceSource(File JavaDoc f, OtterXMLCNode node) {
257         setJavaInterfaceSource(metaData);
258     }
259
260     private void setJavaInterfaceSource(MetaData meta) {
261
262         // the rest should be automatic.
263
try {
264             meta.getDocument().completeModifications();
265         } catch (XMLCException e) {
266             e.printStackTrace();
267         }
268     }
269
270     public String JavaDoc getPackageName() {
271         String JavaDoc name = null;
272
273         try {
274             name = metaData.getDocumentClass().getPackageName();
275         } catch (NullPointerException JavaDoc e) {
276
277             // Workaround?
278
name = null;
279         }
280         return name;
281     }
282
283     public boolean getRecompilation() {
284         return metaData.getDocumentClass().getRecompilation();
285     }
286
287     public void setRecompilation(boolean b) {
288         setRecompilation(metaData, b);
289     }
290
291     private void setRecompilation(MetaData meta, boolean b) {
292         meta.getDocumentClass().setRecompilation(b);
293     }
294
295     // DOM Edits
296
public Object JavaDoc[] getDeleteElements() {
297         return metaData.getDOMEdits().getDeleteElements();
298     }
299
300     private void mergeDeleteElements(MetaData meta,
301                                      DeleteElement[] elements) {
302         List JavaDoc eList = Arrays.asList(meta.getDOMEdits().getDeleteElements());
303
304         for (int i = 0; i < elements.length; i++) {
305             if (!eList.contains(elements[i])) {
306                 meta.getDOMEdits().addDeleteElement(elements[i]);
307             }
308         }
309     }
310
311     public Object JavaDoc[] getURLMappings() {
312         return metaData.getDOMEdits().getURLMappings();
313     }
314
315     // Other
316
public Object JavaDoc getMetaData() {
317         return metaData;
318     }
319
320     public void parse(String JavaDoc[] files, String JavaDoc[] args, PrintWriter JavaDoc writer,
321                       OtterXMLCNode node) throws XMLCException, IOException JavaDoc {
322         XMLCOptions opParser = null;
323         Reporter reporter = null;
324         String JavaDoc[] parseArgs = new String JavaDoc[files.length + args.length + 1];
325         MetaData newMeta = null;
326
327         reporter = new Reporter(writer);
328         for (int i = 0; i < args.length; i++) {
329             parseArgs[i] = args[i];
330         }
331         for (int i = args.length; i < (args.length + files.length); i++) {
332             parseArgs[i] = files[(i - args.length)];
333         }
334         metaData.getDocument().completeModifications();
335         if (parseArgs.length > 1) {
336             parseArgs[parseArgs.length - 1] = node.getFilePath().replace('\\',
337                     '/');
338             try {
339                 opParser = new XMLCOptions();
340                 newMeta = opParser.parse(parseArgs, reporter);
341                 mergeMetaData(newMeta, node);
342             } catch (Exception JavaDoc e) {
343                 e.printStackTrace();
344                 if (e instanceof XMLCException) {
345                     throw (XMLCException) e;
346                 } else if (e instanceof IOException JavaDoc) {
347                     throw (IOException JavaDoc) e;
348                 } else {
349                     throw new XMLCException(e);
350                 }
351             }
352         }
353     }
354
355     private void mergeMetaData(MetaData newMeta, OtterXMLCNode node) {
356
357         // merge group 1
358
setKeepGeneratedSource(newMeta, getKeepGeneratedSource());
359         setPrintAccessorInfo(newMeta, getPrintAccessorInfo());
360         setPrintDOM(newMeta, getPrintDOM());
361         setPrintParseInfo(newMeta, getPrintParseInfo());
362         setPrintVersion(newMeta, getPrintVersion());
363         setRecompilation(newMeta, getRecompilation());
364         setVerbose(newMeta, getVerbose());
365
366         // merge group 2
367
setClassName(newMeta, getClassName());
368         setCompileSource(newMeta, getCompileSource());
369         mergeDeleteElements(newMeta, (DeleteElement[]) getDeleteElements());
370         setDocumentOutput(newMeta, getDocumentOutput());
371         setInputDocument(newMeta, getInputDocument().replace('\\', '/'));
372         setJavaClassSource(newMeta, getJavaClassSource(), node);
373         setJavaInterfaceSource(newMeta);
374         this.metaData = newMeta;
375     }
376
377     public void save(File JavaDoc op) throws IOException JavaDoc {
378         try {
379         metaData.getDocument().serialize(op);
380       } catch (XMLCException e) {
381             e.printStackTrace();
382         }
383     }
384
385     // Private Stuff
386
private String JavaDoc pathToClass(String JavaDoc outputRoot, String JavaDoc in) {
387         String JavaDoc out = in;
388         int index = 0;
389
390         if (out.startsWith(outputRoot)) {
391             out = out.substring(outputRoot.length());
392         }
393         index = out.lastIndexOf('.' + Constants.TYPE_JAVA);
394         if (index > -1) {
395             out = out.substring(0, index);
396         }
397         out = out.replace('\\', '.');
398         out = out.replace('/', '.');
399         while ((out.length() > 0) && (out.charAt(0) == '.')) {
400             out = out.substring(1);
401         }
402         while ((out.length() > 0) && (out.charAt(out.length()-1) == '.')) {
403             out = out.substring(0, out.length() - 1);
404         }
405         return out;
406     }
407
408 }
409
Popular Tags