KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > metadata > DocumentClass


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  * $Id: DocumentClass.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.metadata;
25
26 import java.io.File JavaDoc;
27
28 import org.enhydra.xml.xmlc.XMLCError;
29 import org.enhydra.xml.xmlc.XMLCException;
30 import org.enhydra.xml.xmlc.codegen.JavaLang;
31 import org.w3c.dom.Document JavaDoc;
32
33 /**
34  * Specify properties of the XMLC document class to generate.
35  */

36 public class DocumentClass extends MetaDataElement {
37     /**
38      * Element name.
39      */

40     public static final String JavaDoc TAG_NAME = "documentClass";
41
42     /**
43      * Attribute names.
44      */

45     private static final String JavaDoc NAME_ATTR = "name";
46     private static final String JavaDoc GENERATE_ATTR = "generate";
47     private static final String JavaDoc DELEGATE_SUPPORT_ATTR = "delegateSupport";
48     private static final String JavaDoc CREATE_META_DATA_ATTR = "createMetaData";
49     private static final String JavaDoc RECOMPILATION_ATTR = "recompilation";
50     private static final String JavaDoc DEFERRED_PARSING_ATTR = "deferParsing";
51     private static final String JavaDoc EXTENDS_ATTR = "extends";
52     private static final String JavaDoc DOM_FACTORY_ATTR = "domFactory";
53     private static final String JavaDoc DOM_ATTR = "dom";
54     private static final String JavaDoc CREATE_GET_TAG_METHODS_ATTR = "createGetTagMethods";
55     private static final String JavaDoc GET_TAG_RETURN_TYPE_ATTR = "getTagReturnType";
56
57     /**
58      * Deprecated attribute that has been moved to the InputDocument object.
59      * If a file is parsed that has these options, its value is moved to the
60      * InputDocument object.
61      */

62     private static final String JavaDoc RECOMPILE_SOURCE_FILE_ATTR = "recompileSourceFile";
63
64     /**
65      * Suffix for interface implementations.
66      */

67     public static final String JavaDoc IMPLEMENTATION_SUFFIX = "Impl";
68
69     /**
70      * Default DOM if neither -domfactory or -dom are specified.
71      */

72     public static final DOMType DEFAULT_DOM_TYPE = DOMType.LAZYDOM;
73
74     /*
75      * Argument to getTagReturnType as a short-cut for
76      * org.w3c.dom.Element.
77      */

78     public static final String JavaDoc ACCESSOR_TYPE_ELEMENT = "Element";
79
80     /*
81      * Argument to getTagReturnType as a short-cut for
82      * org.w3c.dom.html.HTMLElement.
83      */

84     public static final String JavaDoc ACCESSOR_TYPE_HTML_ELEMENT = "HTMLElement";
85
86     /*
87      * Argument to getTagReturnType to indicate the actual
88      * class name of the element should be used.
89      */

90     public static final String JavaDoc ACCESSOR_TYPE_CLASS = "class";
91
92     /*
93      * Argument to getTagReturnType to indicate the value obtained
94      * from the nodeClassToInterface method in the XMLCDomFactory object being
95      * used to compile the document should be used.
96      */

97     public static final String JavaDoc ACCESSOR_TYPE_INTERFACE = "interface";
98
99     /*
100      * Base class/interface name. This name is take from either the
101      * name attribute or determined from the source file. Its
102      * calculated at modification completion time to avoid throwing
103      * errors in get method if calculated on the fly.
104      * FIXME: had to go back to calculating on the fly, due to
105      * ordering problems in handling defaults.
106      */

107     private transient boolean determinedBaseClassName;
108     private transient String JavaDoc baseClassName;
109
110     /**
111      * Constructor.
112      */

113     public DocumentClass(Document JavaDoc ownerDoc) {
114         super(ownerDoc, TAG_NAME);
115     }
116
117     /**
118      * Get the base class name, calculating if its not already been done.
119      * Returns null if there is no base class.
120      */

121     private String JavaDoc getBaseClassName() {
122         if (!determinedBaseClassName) {
123             if (getMetaData().getCompileOptions().getCreateSource()) {
124                 baseClassName = classNameFromSourceName();
125             }
126             determinedBaseClassName = true;
127         }
128         return baseClassName;
129     }
130
131     /**
132      * Get the name attribute value, or the default value if its not
133      * specified.
134      */

135     public String JavaDoc getName() {
136         String JavaDoc name = getAttributeNull(NAME_ATTR);
137         if (name == null) {
138             return getBaseClassName();
139         } else {
140             return name;
141         }
142     }
143
144     /**
145      * Set the name attribute value.
146      */

147     public void setName(String JavaDoc value) {
148         setRemoveAttribute(NAME_ATTR, value);
149     }
150
151     /**
152      * Determine if the name attribute is specified.
153      */

154     public boolean isNameSpecified() {
155         return isAttributeSpecified(NAME_ATTR);
156     }
157
158     /**
159      * Get the generate attribute value or the default if it is not specified.
160      */

161     public GenerateType getGenerate() {
162         String JavaDoc value = getAttributeNull(GENERATE_ATTR);
163         if (value == null) {
164             return GenerateType.CLASS;
165         } else {
166             return GenerateType.getType(value);
167         }
168     }
169
170     /**
171      * Set the generate attribute value.
172      */

173     public void setGenerate(GenerateType value) {
174         if (value == null) {
175             removeAttribute(GENERATE_ATTR);
176         } else {
177             setAttribute(GENERATE_ATTR, value.toString());
178         }
179     }
180
181     /**
182      * Determine if the generate attribute is specified.
183      */

184     public boolean isGenerateSpecified() {
185         return isAttributeSpecified(GENERATE_ATTR);
186     }
187
188     /**
189      * Get the delegateSupport attribute value.
190      */

191     public boolean getDelegateSupport() {
192         return getBooleanAttribute(DELEGATE_SUPPORT_ATTR);
193     }
194
195     /**
196      * Set the delegateSupport attribute value.
197      */

198     public void setDelegateSupport(boolean value) {
199         setBooleanAttribute(DELEGATE_SUPPORT_ATTR, value);
200     }
201
202     /**
203      * Get the createMetaData attribute value.
204      */

205     public boolean getCreateMetaData() {
206         return getBooleanAttribute(CREATE_META_DATA_ATTR);
207     }
208
209     /**
210      * Set the createMetaData attribute value.
211      */

212     public void setCreateMetaData(boolean value) {
213         setBooleanAttribute(CREATE_META_DATA_ATTR, value);
214     }
215
216     /**
217      * Get the recompilation attribute value.
218      */

219     public boolean getRecompilation() {
220         return getBooleanAttribute(RECOMPILATION_ATTR);
221     }
222
223     /**
224      * Set the recompilation attribute value. This attribute is a short cut
225      * for setting other attributes. Setting this to true has the
226      * following affects, however setting it to false doesn't modify
227      * any other attributes.
228      * <UL>
229      * <LI> Sets generate to BOTH.
230      * <LI> Sets delegate support to true.
231      * <LI> Sets deferred parsing support to false;
232      * </UL>
233      */

234     public void setRecompilation(boolean value) {
235         setBooleanAttribute(RECOMPILATION_ATTR, value);
236         if (value) {
237             setGenerate(GenerateType.BOTH);
238             setDelegateSupport(true);
239             setDeferredParsing(false);
240         }
241     }
242
243     /**
244      * Get the deferred parsing attribute.
245      */

246     public boolean getDeferredParsing() {
247         return getBooleanAttribute(DEFERRED_PARSING_ATTR);
248     }
249
250     /**
251      * Set the deferred parsing attribute value. This attribute is a short cut
252      * for setting other attributes. Setting this to true has the
253      * following effects, however setting it to false doesn't modify
254      * any other attributes.
255      * <UL>
256      * <LI> Sets generate to CLASS.
257      * <LI> Sets delegate support to false.
258      * <LI> Sets recompilation support to false.
259      * </UL>
260      */

261     public void setDeferredParsing(boolean value) {
262         setBooleanAttribute(DEFERRED_PARSING_ATTR, value);
263         if (value) {
264             setGenerate(GenerateType.CLASS);
265             setDelegateSupport(false);
266             setRecompilation(false);
267         }
268     }
269
270     /**
271      * Get the extends attribute value.
272      */

273     public String JavaDoc getExtends() {
274         return getAttributeNull(EXTENDS_ATTR);
275     }
276
277     /**
278      * Set the extends attribute value.
279      */

280     public void setExtends(String JavaDoc value) {
281         setRemoveAttribute(EXTENDS_ATTR, value);
282     }
283
284     /**
285      * Get the domFactory attribute value.
286      */

287     public String JavaDoc getDomFactory() {
288         return getAttributeNull(DOM_FACTORY_ATTR);
289     }
290
291     /**
292      * Set the domFactory attribute value. If not null, clears the
293      * dom attribute.
294      */

295     public void setDomFactory(String JavaDoc value) {
296         setRemoveAttribute(DOM_FACTORY_ATTR, value);
297         if (value != null) {
298             removeAttribute(DOM_ATTR);
299         }
300     }
301
302     /**
303      * Get the dom attribute value.
304      */

305     public DOMType getDom() {
306         return DOMType.getType(getAttributeNull(DOM_ATTR));
307     }
308
309     /**
310      * Set the dom attribute value. If not null, clears the
311      * domFactory attribute.
312      */

313     public void setDom(DOMType value) {
314         if (value == null) {
315             removeAttribute(DOM_ATTR);
316         } else {
317             setAttribute(DOM_ATTR, value.getName());
318             removeAttribute(DOM_FACTORY_ATTR);
319         }
320     }
321
322     /**
323      * Get the XMLCDomFactory class to use. This is chosen based on
324      * the dom or domFactory attributes.
325      */

326     public String JavaDoc getDomFactoryClass(boolean html) {
327         String JavaDoc domFactory = getAttributeNull(DOM_FACTORY_ATTR);
328         if (domFactory == null) {
329             DOMType domType = getDom();
330         Class JavaDoc dfClass;
331             if (domType == null) {
332                 domType = DEFAULT_DOM_TYPE;
333             }
334             if (html) {
335         dfClass = domType.getHTMLDomFactoryClass();
336             } else {
337         dfClass = domType.getXMLDomFactoryClass();
338             }
339         if (dfClass == null) {
340         throw new XMLCError("The '" + domType.getName() +
341                     "' DOM type only supports " +
342                     (html?"XML":"HTML") + " documents.");
343         }
344         domFactory = dfClass.getName();
345         }
346         return domFactory;
347     }
348
349     /**
350      * Get the list of interfaces that the class will implement.
351      */

352     public String JavaDoc[] getImplements() {
353         Implements[] impls = (Implements[])getChildren(Implements.class);
354         String JavaDoc[] implNames = new String JavaDoc[impls.length];
355         for (int idx = 0; idx < impls.length; idx++) {
356             implNames[idx] = impls[idx].getName();
357         }
358         return implNames;
359     }
360
361     /**
362      * Add an interface to the list of interfaces the class will implement.
363      */

364     public void addImplements(String JavaDoc interfaceName) {
365         Implements impl
366             = (Implements)getOwnerDocument().createElement(Implements.TAG_NAME);
367         impl.setName(interfaceName);
368         appendChild(impl);
369     }
370
371     /**
372      * Get the package name, or null if not set.
373      */

374     public String JavaDoc getPackageName() {
375         String JavaDoc base = getName();
376         if (base == null) {
377             return null;
378         } else {
379             return JavaLang.getPackageName(base);
380         }
381     }
382
383     /**
384      * Get the unqualified class name attribute. If only a class is being
385      * generated, this is the base name, otherwise its the interface name with
386      * `Impl" appended.
387      */

388     public String JavaDoc getUnqualClassName() {
389         String JavaDoc simpleName = JavaLang.simpleClassName(getName());
390         if (getGenerate() == GenerateType.CLASS) {
391             return simpleName;
392         } else {
393             return simpleName + IMPLEMENTATION_SUFFIX;
394         }
395     }
396
397     /**
398      * Get the unqualified interface name, or null if there is no interface
399      * associated with the class.
400      */

401     public String JavaDoc getUnqualInterfaceName() {
402         if (getGenerate() == GenerateType.CLASS) {
403             return null;
404         } else {
405             return JavaLang.simpleClassName(getName());
406         }
407     }
408
409     /**
410      * Get the source output directory (under sourceOutputRoot).
411      */

412     private File JavaDoc getSourceOutputDir() {
413         String JavaDoc sourceOutputRoot = getMetaData().getCompileOptions().getSourceOutputRoot();
414
415         File JavaDoc sourceDirectory = null;
416         String JavaDoc packageName = getPackageName();
417         if ((sourceOutputRoot != null) && (packageName != null)) {
418             sourceDirectory = new File JavaDoc(sourceOutputRoot);
419             if (packageName != null) {
420                 String JavaDoc[] packageParts = JavaLang.parseClassName(packageName);
421                 for (int i = 0; i < packageParts.length; i++) {
422                     sourceDirectory = new File JavaDoc(sourceDirectory, packageParts[i]);
423                 }
424             }
425         }
426         return sourceDirectory;
427     }
428
429     /*
430      * Construct a source file path for the given unqualified name.
431      */

432     private File JavaDoc getSourceFile(String JavaDoc unqualName) {
433         File JavaDoc sourceDirectory = getSourceOutputDir();
434         String JavaDoc fileName = unqualName + ".java";
435         if (sourceDirectory == null) {
436             return new File JavaDoc(fileName);
437         } else {
438             return new File JavaDoc(sourceDirectory, fileName);
439         }
440     }
441
442     /*
443      * Get the name of the generated java class source file.
444      */

445     public File JavaDoc getJavaClassSource() {
446         return getSourceFile(getUnqualClassName());
447     }
448
449     /*
450      * Get the name of the generated java interface source file.
451      */

452     public File JavaDoc getJavaInterfaceSource() {
453         return getSourceFile(getUnqualInterfaceName());
454     }
455
456     /**
457      * Get the createGetTagMethods attribute value.
458      */

459     public boolean getCreateGetTagMethods() {
460         return getBooleanAttribute(CREATE_GET_TAG_METHODS_ATTR);
461     }
462
463     /**
464      * Set the createGetTagMethods attribute value.
465      */

466     public void setCreateGetTagMethods(boolean value) {
467         setBooleanAttribute(CREATE_GET_TAG_METHODS_ATTR, value);
468     }
469
470     /**
471      * Get the return type of getTagXXX() methods or the default if not
472      * specified.
473      */

474     public String JavaDoc getGetTagReturnType() {
475         String JavaDoc type = getAttributeNull(GET_TAG_RETURN_TYPE_ATTR);
476         if (type == null) {
477             return ACCESSOR_TYPE_ELEMENT;
478         } else {
479             return type;
480         }
481     }
482
483     /**
484      * Set the return type for getTagXXX() methods or null to delete
485      * attribute.
486      */

487     public void setGetTagReturnType(String JavaDoc value) {
488         if (ACCESSOR_TYPE_ELEMENT.equals(value)) {
489             value = null;
490         }
491         setRemoveAttribute(GET_TAG_RETURN_TYPE_ATTR, value);
492     }
493
494     /**
495      * Get the class name from the source file name.
496      */

497     private String JavaDoc classNameFromSourceName() {
498         String JavaDoc srcName = getMetaData().getInputDocument().getUrl();
499
500         if (srcName == null) {
501             //FIXME: should be an Exception
502
throw new XMLCError("No input document name from which to determine the class name");
503         }
504
505         String JavaDoc className = new File JavaDoc(srcName).getName();
506         int dotIdx = className.lastIndexOf('.');
507         if (dotIdx >= 0) {
508             className = className.substring(0, dotIdx);
509         }
510         if (!JavaLang.legalJavaIdentifier(className)) {
511             //FIXME: should be an Exception
512
throw new XMLCError("Can't use source file name \""
513                                 + className
514                                 + "\" as class name: "
515                                 + "not a legal Java identifier");
516         }
517         return className;
518     }
519
520     /**
521      * Move deprecated attribute to InputDocument.
522      */

523     private void moveToInputDocument() {
524        InputDocument inputDoc = getMetaData().getInputDocument();
525        String JavaDoc value = getAttributeNull(RECOMPILE_SOURCE_FILE_ATTR);
526        if (value != null) {
527            inputDoc.setRecompileSource(value);
528            removeAttribute(RECOMPILE_SOURCE_FILE_ATTR);
529        }
530     }
531
532     /**
533      * Complete modifications to DOM, check some error cases.
534      * @see MetaDataElement#completeModifications
535      */

536     protected void completeModifications() throws XMLCException {
537         if (isAttributeSpecified(DOM_FACTORY_ATTR)
538             && isAttributeSpecified(DOM_ATTR)) {
539             throw new XMLCException("Can't specify both the "
540                                     + DOM_FACTORY_ATTR
541                                     + " and " + DOM_ATTR
542                                     + " attributes in <"
543                                     + TAG_NAME + ">");
544         }
545         if (isAttributeSpecified(RECOMPILE_SOURCE_FILE_ATTR)) {
546             moveToInputDocument();
547         }
548     }
549 }
550
Popular Tags