KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > generic > NodeCreateGenerator


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

23
24 package org.enhydra.xml.xmlc.dom.generic;
25
26 import org.enhydra.xml.xmlc.XMLCError;
27 import org.enhydra.xml.xmlc.XMLCException;
28 import org.enhydra.xml.xmlc.codegen.JavaCode;
29 import org.enhydra.xml.xmlc.codegen.JavaLang;
30 import org.enhydra.xml.xmlc.compiler.ClassGenerator;
31 import org.enhydra.xml.xmlc.dom.XMLCDocument;
32 import org.w3c.dom.Attr JavaDoc;
33 import org.w3c.dom.CDATASection JavaDoc;
34 import org.w3c.dom.Comment JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.DocumentType JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Entity JavaDoc;
39 import org.w3c.dom.EntityReference JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.Notation JavaDoc;
42 import org.w3c.dom.ProcessingInstruction JavaDoc;
43 import org.w3c.dom.Text JavaDoc;
44
45 /**
46  * Class used create DOM nodes. This maybe extended by a class needing to
47  * create nodes in a different. This is called by the document builder. The
48  * code to generate adds is also in here, as added certain type of data to the
49  * DOM is not specified in level 2.
50  */

51 public class NodeCreateGenerator {
52     /** Document object being compiled */
53     private Document fDocument;
54
55     /** The document element. */
56     private Element JavaDoc fDocElement;
57
58     /** Error messages */
59     private static final String JavaDoc ENTITIES_UNSUPPORTED
60         = "creating entity nodes not implemented for this DOM";
61     private static final String JavaDoc NOTATION_UNSUPPORTED
62         = "creating notation nodes not implemented for this DOM";
63     
64     /**
65      * Constructor.
66      */

67     public NodeCreateGenerator(XMLCDocument xmlcDoc) {
68         fDocument = xmlcDoc.getDocument();
69         fDocElement = fDocument.getDocumentElement();
70     }
71
72     /** Is this the document element? */
73     protected boolean isDocElement(Element JavaDoc element) {
74         return (element == fDocElement);
75     }
76
77     /**
78      * Generate Document and DocumentType creation.
79      */

80     protected void genDocumentCreate(Document document,
81                                      String JavaDoc docVar,
82                                      String JavaDoc docTypeVar,
83                                      JavaCode body) {
84         DocumentType JavaDoc docType = document.getDoctype();
85         String JavaDoc docTypeRef;
86         if (docType == null) {
87             docTypeRef = "null";
88         } else {
89             docTypeRef = docTypeVar;
90             genDocumentTypeCreate(docType, docVar, docTypeVar, body);
91         }
92
93         String JavaDoc docClass = document.getClass().getName();
94         body.addln(docClass + " " + docVar + " = (" + docClass + ")"
95                    + ClassGenerator.DOM_FACTORY_FIELD_NAME + ".createDocument("
96                    + JavaLang.createStringConst(fDocElement.getNamespaceURI()) + ", "
97                    + JavaLang.createStringConst(fDocElement.getNodeName()) + ", "
98                    + docTypeRef + ");");
99     }
100     
101     /**
102      * Generate DocumentType creation.
103      */

104     protected void genDocumentTypeCreate(DocumentType JavaDoc docType,
105                                          String JavaDoc docVar,
106                                          String JavaDoc varName,
107                                          JavaCode body) {
108         String JavaDoc docTypeClass = docType.getClass().getName();
109         body.addln(docTypeClass + " " + varName
110                    + " = (" + docTypeClass + ")"
111                    + ClassGenerator.DOM_FACTORY_FIELD_NAME
112                    + ".createDocumentType("
113                    + JavaLang.createStringConst(docType.getNodeName()) + ", "
114                    + JavaLang.createStringConst(docType.getPublicId()) + ", "
115                    + JavaLang.createStringConst(docType.getSystemId()) + ", "
116                    + JavaLang.createStringConst(docType.getInternalSubset()) + ");");
117     }
118
119     /**
120      * Generate Entity creation.
121      */

122     protected void genEntityCreate(Entity JavaDoc entity,
123                                    String JavaDoc docVar,
124                                    String JavaDoc varName,
125                                    JavaCode body) {
126         throw new XMLCError(ENTITIES_UNSUPPORTED);
127     }
128
129     /**
130      * Generate Attr creation.
131      */

132     protected void genAttrCreate(Attr JavaDoc attr,
133                                  String JavaDoc docVar,
134                                  String JavaDoc varName,
135                                  JavaCode body) {
136         body.add(varName + " = ");
137         if (attr.getNamespaceURI() == null) {
138             body.addln(docVar + ".createAttribute("
139                        + JavaLang.createStringConst(attr.getName()) + ");");
140         } else {
141             body.addln(docVar + ".createAttributeNS("
142                        + JavaLang.createStringConst(attr.getNamespaceURI()) + ", "
143                        + JavaLang.createStringConst(attr.getName()) + ");");
144         }
145     }
146
147     /**
148      * Generate CDATASection creation.
149      */

150     protected void genCDATASectionCreate(CDATASection JavaDoc cdataSection,
151                                          String JavaDoc docVar,
152                                          String JavaDoc varName,
153                                          JavaCode body) {
154         body.addln(varName + " = " + docVar + ".createCDATASection("
155                    + JavaLang.createStringConst(cdataSection.getData()) + ");");
156     }
157
158     /**
159      * Generate Comment creation.
160      */

161     protected void genCommentCreate(Comment JavaDoc comment,
162                                     String JavaDoc docVar,
163                                     String JavaDoc varName,
164                                     JavaCode body) {
165         body.addln(varName + " = " + docVar + ".createComment("
166                    + JavaLang.createStringConst(comment.getData()) + ");");
167     }
168
169     /**
170      * Generate Element creation.
171      */

172     protected void genElementCreate(Element JavaDoc element,
173                                     String JavaDoc docVar,
174                                     String JavaDoc varName,
175                                     JavaCode body) {
176         body.add(varName + " = ");
177         if (isDocElement(element)) {
178             // document element is created when document is created, just
179
// get it
180
body.addln(docVar + ".getDocumentElement();");
181         } if (element.getNamespaceURI() == null) {
182             body.addln(docVar + ".createElement("
183                        + JavaLang.createStringConst(element.getTagName()) + ");");
184         } else {
185             body.addln(docVar + ".createElementNS("
186                        + JavaLang.createStringConst(element.getNamespaceURI()) + ", "
187                        + JavaLang.createStringConst(element.getTagName()) + ");");
188         }
189     }
190
191     /**
192      * Generate EntityReference creation.
193      */

194     protected void genEntityReferenceCreate(EntityReference JavaDoc entityRef,
195                                             String JavaDoc docVar,
196                                             String JavaDoc varName,
197                                             JavaCode body) {
198         body.addln(varName + " = " + docVar
199                    + ".createEntityReference("
200                    + JavaLang.createStringConst(entityRef.getNodeName())
201                    + ");");
202     }
203
204     /**
205      * Generate Notation creation.
206      */

207     protected void genNotationCreate(Notation JavaDoc notation,
208                                      String JavaDoc docVar,
209                                      String JavaDoc varName,
210                                      JavaCode body) {
211         throw new XMLCError(NOTATION_UNSUPPORTED);
212     }
213
214     /**
215      * Generate ProcessingInstruction creation.
216      */

217     protected void genProcessInstructionCreate(ProcessingInstruction JavaDoc procInstr,
218                                                String JavaDoc docVar,
219                                                String JavaDoc varName,
220                                                JavaCode body) {
221         body.addln(varName + " = " + docVar
222                    + ".createProcessingInstruction("
223                    + JavaLang.createStringConst(procInstr.getNodeName()) + ", "
224                    + JavaLang.createStringConst(procInstr.getData()) + ");");
225     }
226
227     /**
228      * Generate Text creation.
229      */

230     protected void genTextCreate(Text JavaDoc text,
231                                  String JavaDoc docVar,
232                                  String JavaDoc varName,
233                                  JavaCode body) {
234         body.addln(varName + " = " + docVar + ".createTextNode("
235                    + JavaLang.createStringConst(text.getData()) + ");");
236     }
237
238     /**
239      * Generate code to construct a node, assigning it to the supplied
240      * variable, Can be overriden by derived class
241      */

242     public void genNodeCreate(String JavaDoc docVar,
243                               String JavaDoc varName,
244                               Node JavaDoc node,
245                               JavaCode body) throws XMLCException {
246         switch (node.getNodeType()) {
247         case Node.DOCUMENT_NODE:
248         case Node.DOCUMENT_TYPE_NODE:
249             throw new XMLCError("Creating node of this type must be done directly: "
250                                 + node.getClass().getName());
251
252         case Node.ATTRIBUTE_NODE:
253             genAttrCreate((Attr JavaDoc)node, docVar, varName, body);
254             break;
255
256         case Node.CDATA_SECTION_NODE:
257             genCDATASectionCreate((CDATASection JavaDoc)node, docVar, varName,
258                                   body);
259             break;
260
261         case Node.COMMENT_NODE:
262             genCommentCreate((Comment JavaDoc)node, docVar, varName, body);
263             break;
264
265         case Node.ELEMENT_NODE:
266             genElementCreate((Element JavaDoc)node, docVar, varName, body);
267             break;
268
269         case Node.ENTITY_NODE:
270             genEntityCreate((Entity JavaDoc)node, docVar, varName, body);
271             break;
272
273         case Node.ENTITY_REFERENCE_NODE:
274             genEntityReferenceCreate((EntityReference JavaDoc)node, docVar, varName,
275                                      body);
276             break;
277
278         case Node.NOTATION_NODE:
279             genNotationCreate((Notation JavaDoc)node, docVar, varName, body);
280             break;
281
282         case Node.PROCESSING_INSTRUCTION_NODE:
283             genProcessInstructionCreate((ProcessingInstruction JavaDoc)node,
284                                         docVar, varName, body);
285             break;
286
287         case Node.TEXT_NODE:
288             genTextCreate((Text JavaDoc)node, docVar, varName, body);
289             break;
290
291         case Node.DOCUMENT_FRAGMENT_NODE:
292             throw new XMLCError("Creating node of this type not supported: "
293                                 + node.getClass().getName());
294
295         default:
296             throw new XMLCError("Unknown node type: " + node.getNodeType());
297         }
298     }
299
300     /**
301      * Determine if a node is a child of the document and needs to be inserted
302      * before the document variable.
303      */

304     private boolean preceedsDocElement(Node JavaDoc node) {
305         // Scan following siblings for document element
306
for (Node JavaDoc sibling = node.getNextSibling(); sibling != null;
307              sibling = sibling.getNextSibling()) {
308             if (sibling == fDocElement) {
309                     return true; // doc element after
310
}
311         }
312         return false;
313     }
314
315     /**
316      * Generate code to add a child of the document
317      */

318     private void genAppendDocumentChild(String JavaDoc parentVar,
319                                         String JavaDoc childVar,
320                                         Node JavaDoc child,
321                                         JavaCode body) {
322         // The Document element is automatically created, don't add it
323
if (child != fDocElement) {
324             // Make sure we add it on the correct side of the document element.
325
if (preceedsDocElement(child)) {
326                 body.addln(parentVar + ".insertBefore(" + childVar + ", "
327                            + parentVar + ".getDocumentElement());");
328             } else {
329                 body.addln(parentVar + ".appendChild(" + childVar + ");");
330             }
331         }
332     }
333
334     /**
335      * Generate code to do an append child.
336      */

337     public void genAppendChild(String JavaDoc parentVar,
338                                String JavaDoc childVar,
339                                Node JavaDoc child,
340                                JavaCode body) {
341         if (child.getParentNode() == fDocument) {
342             // special handling for children of Document.
343
genAppendDocumentChild(parentVar, childVar, child, body);
344         } else {
345             body.addln(parentVar + ".appendChild(" + childVar + ");");
346         }
347     }
348
349     /**
350      * Generate code to add attribute to an element.
351      */

352     public void genAddAttribute(String JavaDoc elementVar,
353                                 String JavaDoc attrVar,
354                                 JavaCode body) {
355         body.addln(elementVar + ".setAttributeNode(" + attrVar + ");");
356     }
357
358     /**
359      * Add an entity to a DocumentType
360      */

361     public void genAddEntity(String JavaDoc docTypeVar,
362                              String JavaDoc entityVar,
363                              Entity JavaDoc entity,
364                              JavaCode body) {
365         throw new XMLCError(ENTITIES_UNSUPPORTED);
366     }
367
368     /**
369      * Add an notation to a DocumentType
370      */

371     public void genAddNotation(String JavaDoc docTypeVar,
372                                String JavaDoc notationVar,
373                                Notation JavaDoc notation,
374                                JavaCode body) {
375         throw new XMLCError(NOTATION_UNSUPPORTED);
376     }
377 }
378
Popular Tags