KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > compiler > AccessConsts


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

23  /*
24  * Enhydra Java Application Server
25  * The Initial Developer of the Original Code is Lutris Technologies Inc.
26  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
27  * Inc.
28  * All Rights Reserved.
29  *
30  * The contents of this file are subject to the Enhydra Public License Version
31  * 1.0 (the "License"); you may not use this file except in compliance with the
32  * License. You may obtain a copy of the License at
33  * http://www.enhydra.org/software/license/epl.html
34  *
35  * Software distributed under the License is distributed on an "AS IS" basis,
36  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
37  * License for the specific language governing rights and limitations under the
38  * License.
39  *
40  * $Id: AccessConsts.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
41  */

42
43 package org.enhydra.xml.xmlc.compiler;
44
45 import java.io.PrintWriter JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.TreeMap JavaDoc;
48
49 import org.enhydra.xml.xmlc.codegen.JavaClass;
50 import org.enhydra.xml.xmlc.codegen.JavaField;
51 import org.enhydra.xml.xmlc.codegen.JavaLang;
52 import org.enhydra.xml.xmlc.codegen.JavaModifiers;
53 import org.enhydra.xml.xmlc.metadata.HTMLCompatibility;
54 import org.enhydra.xml.xmlc.metadata.HTMLSection;
55 import org.enhydra.xml.xmlc.metadata.MetaData;
56
57 /**
58  * Data collection and code generation for constants used to access the
59  * DOM.
60  */

61 class AccessConsts {
62     /**
63      * Information collected about each element.
64      */

65     private ElementTable fElementTable;
66
67     /*
68      * Sorted table of element class fields that have been created, with
69      * class as the key.
70      */

71     private TreeMap JavaDoc fClassNameFields = new TreeMap JavaDoc();
72
73     /**
74      * Table of element class that are not legal Java identifiers.
75      */

76     private TreeMap JavaDoc fInvalidElementClassNames = null;
77
78     /*
79      * Sorted table of element name fields that have been created, with
80      * element name as the key.
81      */

82     private TreeMap JavaDoc fElementNameFields = new TreeMap JavaDoc();
83
84     /**
85      * Table of element names that are not legal Java identifiers.
86      */

87     private TreeMap JavaDoc fInvalidElementNames = null;
88
89     /**
90      * Generate old-style, all upper-case class constants.
91      */

92     private boolean fOldClassConstants;
93
94     /**
95      * Generate old-style, all upper-case name constants.
96      */

97     private boolean fOldNameConstants;
98
99     /**
100      * Class being generated.
101      */

102     private JavaClass fDocClass;
103
104     /**
105      * Constructor.
106      * @param metaData Document metadata.
107      * @param xmlcDoc Must contain parsed document.
108      * @param docClass Object containing the class being generated.
109      */

110     public AccessConsts(MetaData metaData,
111                         ElementTable elementTable) {
112         fElementTable = elementTable;
113
114         //FIXME: Would really rather get defaults..
115
HTMLSection htmlSection = metaData.getHTMLSection();
116         HTMLCompatibility htmlCompat = htmlSection.getCompatibility();
117         if (htmlCompat != null) {
118             fOldClassConstants = htmlCompat.getOldClassConstants();
119             fOldNameConstants = htmlCompat.getOldNameConstants();
120         }
121     }
122
123     /**
124      * Create a field for a single element class name.
125      */

126     private void createElementClassNameField(String JavaDoc className) {
127         String JavaDoc fieldName = "CLASS_"
128             + (fOldClassConstants ? className.toUpperCase() : className);
129
130         JavaField field
131             = new JavaField(fieldName,
132                             "String",
133                             JavaModifiers.PUBLIC_CONST,
134                             "Class attribute constant for element class " + className,
135                             JavaLang.createStringConst(className));
136         fDocClass.addField(field);
137         fClassNameFields.put(className, field);
138     }
139
140     /**
141      * Create class name fields for an element.
142      */

143     private void processElementClassNames(ElementInfo elementInfo) {
144         String JavaDoc[] names = elementInfo.getElementClassNames();
145         if (names != null) {
146             for (int idx = 0; idx < names.length; idx++) {
147                 if (!fClassNameFields.containsKey(names[idx])) {
148                     if (!JavaLang.legalJavaIdentifier(names[idx])) {
149                         if (fInvalidElementClassNames == null) {
150                             fInvalidElementClassNames = new TreeMap JavaDoc();
151                         }
152                         fInvalidElementClassNames.put(names[idx], names[idx]);
153                     } else {
154                         createElementClassNameField(names[idx]);
155                     }
156                 }
157             }
158         }
159     }
160
161     /**
162      * Create a field for an element name.
163      */

164     private void createElementNameField(String JavaDoc name) {
165         String JavaDoc fieldName = "NAME_"
166             + (fOldNameConstants ? name.toUpperCase() : name);
167         JavaField field
168             = new JavaField(fieldName,
169                             "String",
170                             JavaModifiers.PUBLIC_CONST,
171                             "Element name constant for " + name,
172                             JavaLang.createStringConst(name));
173         fDocClass.addField(field);
174         fElementNameFields.put(name, field);
175     }
176
177     /**
178      * Create element name fieldsfor an element.
179      */

180     private void processElementName(ElementInfo elementInfo) {
181         String JavaDoc name = elementInfo.getElementName();
182         if ((name != null) && (name.length() > 0)) {
183             if (!fElementNameFields.containsKey(name)) {
184                 if (!JavaLang.legalJavaIdentifier(name)) {
185                     if (fInvalidElementNames == null) {
186                         fInvalidElementNames = new TreeMap JavaDoc();
187                     }
188                     fInvalidElementNames.put(name, name);
189                 } else {
190                     createElementNameField(name);
191                 }
192             }
193         }
194     }
195
196     /**
197      * Generate the code for the access methods.
198      * @param docClass Object containing the class being generated.
199      */

200     public void generateCode(JavaClass docClass) {
201         fDocClass = docClass;
202         Iterator JavaDoc elements = fElementTable.getElements();
203         while (elements.hasNext()) {
204             ElementInfo elementInfo = (ElementInfo)elements.next();
205             processElementClassNames(elementInfo);
206             processElementName(elementInfo);
207         }
208     }
209     
210     /**
211      * Print the signatures of the access constants are created.
212      */

213     public void printAccessConstants(PrintWriter JavaDoc out) {
214         Iterator JavaDoc classes = fClassNameFields.values().iterator();
215         while (classes.hasNext()) {
216             ((JavaField)classes.next()).printDefinition(out);
217             out.println(';');
218         }
219         Iterator JavaDoc names = fElementNameFields.values().iterator();
220         while (names.hasNext()) {
221             ((JavaField)names.next()).printDefinition(out);
222             out.println(';');
223         }
224     }
225
226     /**
227      * Print names that didn't have access constants generated due to
228      * being illegal Java identifiers. Sort so tests can be reproduced.
229      */

230     public void printOmittedConstants(PrintWriter JavaDoc out) {
231         if (fInvalidElementClassNames != null) {
232             out.println("Element class name constants not created for these class names");
233             out.println("since they are not legal Java identifiers:");
234             Iterator JavaDoc badClasses = fInvalidElementClassNames.keySet().iterator();
235             while (badClasses.hasNext()) {
236                 out.print("`");
237                 out.print((String JavaDoc)badClasses.next());
238                 out.println("'");
239             }
240         }
241         if (fInvalidElementNames != null) {
242             out.println("Element name constants not created for these names");
243             out.println("since they are not legal Java identifiers:");
244             Iterator JavaDoc badNames = fInvalidElementNames.keySet().iterator();
245             while (badNames.hasNext()) {
246                 out.print("`");
247                 out.print((String JavaDoc)badNames.next());
248                 out.println("'");
249             }
250         }
251     }
252 }
253
Popular Tags