001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022:
023: package org.enhydra.kelp.common.bridge;
024:
025: // XMLC imports
026: import org.enhydra.xml.xmlc.XMLCException;
027: import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions;
028: import org.enhydra.xml.xmlc.metadata.DeleteElement;
029: import org.enhydra.xml.xmlc.metadata.CompileOptions;
030: import org.enhydra.xml.xmlc.metadata.DocumentClass;
031: import org.enhydra.xml.xmlc.metadata.DOMEdits;
032: import org.enhydra.xml.xmlc.metadata.MetaData;
033: import org.enhydra.xml.xmlc.metadata.MetaDataDocument;
034:
035: // ToolBox imports
036: import org.enhydra.tool.common.PathHandle;
037:
038: // Kelp imports
039: import org.enhydra.kelp.common.Constants;
040: import org.enhydra.kelp.common.node.OtterXMLCNode;
041:
042: // Standard imports
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.PrintWriter;
046: import java.util.List;
047: import java.util.Arrays;
048:
049: //
050: public class MetaDataHandlerV2 implements MetaDataHandler {
051:
052: // strings not to be resourced
053: private final String[] CUSTOM_INPUT_DOC_TYPES = { "chtml", "htm",
054: "html", "xhtml" }; // nores
055: private final String FILE_PROTOCOL = "file:"; // nores
056:
057: //
058: private MetaData metaData = null;
059:
060: //
061: public MetaDataHandlerV2() {
062: MetaDataDocument doc = new MetaDataDocument();
063:
064: metaData = doc.getMetaData();
065: metaData.setCompileOptions(new CompileOptions(doc));
066: metaData.setDocumentClass(new DocumentClass(doc));
067: metaData.setDOMEdits(new DOMEdits(doc));
068: }
069:
070: // Compiler Options
071: public boolean getCompileSource() {
072: return metaData.getCompileOptions().getCompileSource();
073: }
074:
075: public void setCompileSource(boolean b) {
076: setCompileSource(metaData, b);
077: }
078:
079: private void setCompileSource(MetaData meta, boolean b) {
080: meta.getCompileOptions().setCompileSource(b);
081: }
082:
083: public String getDocumentOutput() {
084: return PathHandle.createPathString(metaData.getCompileOptions()
085: .getDocumentOutput());
086: }
087:
088: public void setDocumentOutput(String s) {
089: setDocumentOutput(metaData, s);
090: }
091:
092: private void setDocumentOutput(MetaData meta, String s) {
093: String path = PathHandle.createPathString(s);
094:
095: meta.getCompileOptions().setDocumentOutput(path);
096: }
097:
098: public String getInputDocument() {
099: String doc = null;
100:
101: doc = metaData.getCompileOptions().getInputDocument();
102: if (doc.startsWith(FILE_PROTOCOL)) {
103: doc = doc.substring(FILE_PROTOCOL.length());
104: }
105: return PathHandle.createPathString(doc);
106: }
107:
108: public void setInputDocument(String s) {
109: setInputDocument(metaData, s);
110: }
111:
112: private void setInputDocument(MetaData meta, String s) {
113: PathHandle handle = null;
114: StringBuffer buf = new StringBuffer();
115:
116: handle = PathHandle.createPathHandle(s);
117: if (!handle.hasExtension(CUSTOM_INPUT_DOC_TYPES)) {
118: buf.append(FILE_PROTOCOL);
119: }
120: buf.append(handle.getPath());
121: meta.getCompileOptions().setInputDocument(buf.toString());
122: }
123:
124: public boolean getKeepGeneratedSource() {
125: return metaData.getCompileOptions().getKeepGeneratedSource();
126: }
127:
128: public void setKeepGeneratedSource(boolean b) {
129: setKeepGeneratedSource(metaData, b);
130: }
131:
132: private void setKeepGeneratedSource(MetaData meta, boolean b) {
133: meta.getCompileOptions().setKeepGeneratedSource(b);
134: }
135:
136: public boolean getPrintAccessorInfo() {
137: return metaData.getCompileOptions().getPrintAccessorInfo();
138: }
139:
140: public void setPrintAccessorInfo(boolean b) {
141: setPrintAccessorInfo(metaData, b);
142: }
143:
144: private void setPrintAccessorInfo(MetaData meta, boolean b) {
145: meta.getCompileOptions().setPrintAccessorInfo(b);
146: }
147:
148: public boolean getPrintDocumentInfo() {
149: return metaData.getCompileOptions().getPrintDocumentInfo();
150: }
151:
152: public void setPrintDocumentInfo(boolean b) {
153: setPrintDocumentInfo(metaData, b);
154: }
155:
156: private void setPrintDocumentInfo(MetaData meta, boolean b) {
157: meta.getCompileOptions().setPrintDocumentInfo(b);
158: }
159:
160: public boolean getPrintDOM() {
161: return metaData.getCompileOptions().getPrintDOM();
162: }
163:
164: public void setPrintDOM(boolean b) {
165: setPrintDOM(metaData, b);
166: }
167:
168: private void setPrintDOM(MetaData meta, boolean b) {
169: meta.getCompileOptions().setPrintDOM(b);
170: }
171:
172: public boolean getPrintParseInfo() {
173: return metaData.getCompileOptions().getPrintParseInfo();
174: }
175:
176: public void setPrintParseInfo(boolean b) {
177: setPrintParseInfo(metaData, b);
178: }
179:
180: private void setPrintParseInfo(MetaData meta, boolean b) {
181: meta.getCompileOptions().setPrintParseInfo(b);
182: }
183:
184: public boolean getPrintVersion() {
185: return metaData.getCompileOptions().getPrintVersion();
186: }
187:
188: public void setPrintVersion(boolean b) {
189: setPrintVersion(metaData, b);
190: }
191:
192: private void setPrintVersion(MetaData meta, boolean b) {
193: meta.getCompileOptions().setPrintVersion(b);
194: }
195:
196: public boolean getVerbose() {
197: return metaData.getCompileOptions().getVerbose();
198: }
199:
200: public void setVerbose(boolean b) {
201: setVerbose(metaData, b);
202: }
203:
204: private void setVerbose(MetaData meta, boolean b) {
205: meta.getCompileOptions().setVerbose(b);
206: }
207:
208: // Document Class
209: public String getClassName() {
210: return metaData.getDocumentClass().getName();
211: }
212:
213: public void setClassName(String n) {
214: setClassName(metaData, n);
215: }
216:
217: private void setClassName(MetaData meta, String n) {
218: meta.getDocumentClass().setName(n);
219: }
220:
221: public File getJavaClassSource() {
222: return metaData.getDocumentClass().getJavaClassSource();
223: }
224:
225: public void setJavaClassSource(File f, OtterXMLCNode node) {
226: setJavaClassSource(metaData, f, node);
227: }
228:
229: private void setJavaClassSource(MetaData meta, File f,
230: OtterXMLCNode node) {
231: String outputRoot = node.getGenerateToRoot();
232: String path = f.getAbsolutePath();
233: MetaDataDocument doc = meta.getDocument();
234:
235: outputRoot = outputRoot.replace('\\', '/').trim();
236: path = path.replace('\\', '/').trim();
237: String name = pathToClass(outputRoot, path);
238:
239: meta.getDocumentClass().setName(name);
240: meta.getCompileOptions().setClassOutputRoot(outputRoot);
241: meta.getCompileOptions().setSourceOutputRoot(outputRoot);
242: try {
243: doc.completeModifications();
244: } catch (XMLCException e) {
245: e.printStackTrace();
246: }
247: }
248:
249: public File getJavaInterfaceSource() {
250: return metaData.getDocumentClass().getJavaInterfaceSource();
251: }
252:
253: public void setJavaInterfaceSource(File f, OtterXMLCNode node) {
254: setJavaInterfaceSource(metaData);
255: }
256:
257: private void setJavaInterfaceSource(MetaData meta) {
258:
259: // the rest should be automatic.
260: try {
261: meta.getDocument().completeModifications();
262: } catch (XMLCException e) {
263: e.printStackTrace();
264: }
265: }
266:
267: public String getPackageName() {
268: String name = null;
269:
270: try {
271: name = metaData.getDocumentClass().getPackageName();
272: } catch (NullPointerException e) {
273:
274: // Workaround?
275: name = null;
276: }
277: return name;
278: }
279:
280: public boolean getRecompilation() {
281: return metaData.getDocumentClass().getRecompilation();
282: }
283:
284: public void setRecompilation(boolean b) {
285: setRecompilation(metaData, b);
286: }
287:
288: private void setRecompilation(MetaData meta, boolean b) {
289: meta.getDocumentClass().setRecompilation(b);
290: }
291:
292: // DOM Edits
293: public Object[] getDeleteElements() {
294: return metaData.getDOMEdits().getDeleteElements();
295: }
296:
297: private void mergeDeleteElements(MetaData meta,
298: DeleteElement[] elements) {
299: List eList = Arrays.asList(meta.getDOMEdits()
300: .getDeleteElements());
301:
302: for (int i = 0; i < elements.length; i++) {
303: if (!eList.contains(elements[i])) {
304: meta.getDOMEdits().addDeleteElement(elements[i]);
305: }
306: }
307: }
308:
309: public Object[] getURLMappings() {
310: return metaData.getDOMEdits().getURLMappings();
311: }
312:
313: // Other
314: public Object getMetaData() {
315: return metaData;
316: }
317:
318: public void parse(String[] files, String[] args,
319: PrintWriter writer, OtterXMLCNode node)
320: throws XMLCException, IOException {
321: XMLCOptions opParser = null;
322: Reporter reporter = null;
323: String[] parseArgs = new String[files.length + args.length + 1];
324: MetaData newMeta = null;
325:
326: reporter = new Reporter(writer);
327: for (int i = 0; i < args.length; i++) {
328: parseArgs[i] = args[i];
329: }
330: for (int i = args.length; i < (args.length + files.length); i++) {
331: parseArgs[i] = files[(i - args.length)];
332: }
333: metaData.getDocument().completeModifications();
334: if (parseArgs.length > 1) {
335: parseArgs[parseArgs.length - 1] = node.getFilePath()
336: .replace('\\', '/');
337: try {
338: opParser = new XMLCOptions();
339: newMeta = opParser.parse(parseArgs, reporter);
340: mergeMetaData(newMeta, node);
341: } catch (Exception e) {
342: e.printStackTrace();
343: if (e instanceof XMLCException) {
344: throw (XMLCException) e;
345: } else if (e instanceof IOException) {
346: throw (IOException) e;
347: } else {
348: throw new XMLCException(e);
349: }
350: }
351: }
352: }
353:
354: private void mergeMetaData(MetaData newMeta, OtterXMLCNode node) {
355:
356: // merge group 1
357: setKeepGeneratedSource(newMeta, getKeepGeneratedSource());
358: setPrintAccessorInfo(newMeta, getPrintAccessorInfo());
359: setPrintDOM(newMeta, getPrintDOM());
360: setPrintParseInfo(newMeta, getPrintParseInfo());
361: setPrintVersion(newMeta, getPrintVersion());
362: setRecompilation(newMeta, getRecompilation());
363: setVerbose(newMeta, getVerbose());
364:
365: // merge group 2
366: setClassName(newMeta, getClassName());
367: setCompileSource(newMeta, getCompileSource());
368: mergeDeleteElements(newMeta,
369: (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 op) throws IOException {
378: try {
379: metaData.getDocument().serialize(op);
380: } catch (XMLCException e) {
381: e.printStackTrace();
382: }
383: }
384:
385: // Private Stuff
386: private String pathToClass(String outputRoot, String in) {
387: String 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)
403: && (out.charAt(out.length() - 1) == '.')) {
404: out = out.substring(0, out.length() - 1);
405: }
406: return out;
407: }
408:
409: }
|