KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > lazydom > LazyDOMNodeCreateGenerator


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

23
24 package org.enhydra.xml.xmlc.dom.lazydom;
25
26 import org.enhydra.xml.io.PreFormattedText;
27 import org.enhydra.xml.lazydom.LazyElement;
28 import org.enhydra.xml.lazydom.LazyNode;
29 import org.enhydra.xml.xmlc.codegen.JavaCode;
30 import org.enhydra.xml.xmlc.codegen.JavaLang;
31 import org.enhydra.xml.xmlc.compiler.ClassGenerator;
32 import org.enhydra.xml.xmlc.dom.XMLCDocument;
33 import org.enhydra.xml.xmlc.dom.generic.NodeCreateGenerator;
34 import org.w3c.dom.Attr JavaDoc;
35 import org.w3c.dom.CDATASection JavaDoc;
36 import org.w3c.dom.Comment JavaDoc;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.DocumentType JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Entity JavaDoc;
41 import org.w3c.dom.EntityReference JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.Notation JavaDoc;
44 import org.w3c.dom.ProcessingInstruction JavaDoc;
45 import org.w3c.dom.Text JavaDoc;
46
47 /**
48  * DOM node creation class that overrides methods in the generic version to
49  * assign node ids.
50  */

51 class LazyDOMNodeCreateGenerator extends NodeCreateGenerator {
52     /**
53      * If not null, the object to preformat node text.
54      */

55     private PreFormatter fPreFormatter;
56
57     /**
58      * Constructor.
59      */

60     public LazyDOMNodeCreateGenerator(XMLCDocument xmlcDoc,
61                                       PreFormatter preFormatter) {
62         super(xmlcDoc);
63         fPreFormatter = preFormatter;
64     }
65
66     /**
67      * Get the string constant for the preformatted text for a node, or "null"
68      * if node should not be associated with the node.
69      */

70     private String JavaDoc getPreFormattedText(Node node) {
71         String JavaDoc text = null;
72         if ((node instanceof PreFormattedText) && (fPreFormatter != null)) {
73             text = fPreFormatter.preFormatNode(node);
74         }
75         return JavaLang.createStringConst(text);
76     }
77
78     /**
79      * Generate Document creation.
80      */

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

108     protected void genDocumentTypeCreate(DocumentType JavaDoc docType,
109                                          String JavaDoc docVarName,
110                                          String JavaDoc varName,
111                                          JavaCode body) {
112         String JavaDoc docTypeClass = docType.getClass().getName();
113         int nodeId = ((LazyNode)docType).getNodeId();
114         body.addln(docTypeClass + " " + varName
115                    + " = (" + docTypeClass + ")"
116                    + ClassGenerator.DOM_FACTORY_FIELD_NAME
117                    + ".createDocumentType("
118                    + JavaLang.createStringConst(docType.getNodeName()) + ", "
119                    + JavaLang.createStringConst(docType.getPublicId()) + ", "
120                    + JavaLang.createStringConst(docType.getSystemId()) + ", "
121                    + JavaLang.createStringConst(docType.getInternalSubset()) + ");");
122         body.addln("((" + LazyNode.class.getName() + ")" + varName
123                    + ").makeTemplateNode(" + nodeId + ");");
124     }
125
126     /**
127      * Generate Entity creation.
128      */

129     protected void genEntityCreate(Entity JavaDoc entity,
130                                    String JavaDoc docVarName,
131                                    String JavaDoc varName,
132                                    JavaCode body) {
133         int nodeId = ((LazyNode)entity).getNodeId();
134         body.addln(varName + " = " + docVarName + ".createTemplateEntity("
135                    + JavaLang.createStringConst(entity.getNodeName()) + ", "
136                    + JavaLang.createStringConst(entity.getPublicId()) + ", "
137                    + JavaLang.createStringConst(entity.getSystemId()) + ", "
138                    + JavaLang.createStringConst(entity.getNotationName()) + ", "
139                    + nodeId + ");");
140     }
141
142     /**
143      * Generate Attr creation.
144      */

145     protected void genAttrCreate(Attr JavaDoc attr,
146                                  String JavaDoc docVarName,
147                                  String JavaDoc varName,
148                                  JavaCode body) {
149         int nodeId = ((LazyNode)attr).getNodeId();
150         body.add(varName + " = ");
151         if (attr.getNamespaceURI() == null) {
152             body.addln(docVarName + ".createTemplateAttribute("
153                        + JavaLang.createStringConst(attr.getName()) + ", "
154                        + nodeId + ");");
155         } else {
156             body.addln(docVarName + ".createTemplateAttributeNS("
157                        + JavaLang.createStringConst(attr.getNamespaceURI()) + ", "
158                        + JavaLang.createStringConst(attr.getName()) + ", "
159                        + nodeId + ");");
160         }
161     }
162
163     /**
164      * Generate CDATASection creation.
165      */

166     protected void genCDATASectionCreate(CDATASection JavaDoc cdataSection,
167                                          String JavaDoc docVarName,
168                                          String JavaDoc varName,
169                                          JavaCode body) {
170         int nodeId = ((LazyNode)cdataSection).getNodeId();
171         body.addln(varName + " = " + docVarName
172                    + ".createTemplateCDATASection("
173                    + JavaLang.createStringConst(cdataSection.getData()) + ", "
174                    + nodeId + ");");
175     }
176
177     /**
178      * Generate Comment creation.
179      */

180     protected void genCommentCreate(Comment JavaDoc comment,
181                                     String JavaDoc docVarName,
182                                     String JavaDoc varName,
183                                     JavaCode body) {
184         int nodeId = ((LazyNode)comment).getNodeId();
185         body.addln(varName + " = " + docVarName + ".createTemplateComment("
186                    + JavaLang.createStringConst(comment.getData()) + ", "
187                    + nodeId + ");");
188     }
189
190     /**
191      * Generate Element creation.
192      */

193     protected void genElementCreate(Element element,
194                                     String JavaDoc docVarName,
195                                     String JavaDoc varName,
196                                     JavaCode body) {
197         int nodeId = ((LazyNode)element).getNodeId();
198         body.add(varName + " = ");
199         if (isDocElement(element)) {
200             String JavaDoc castVar = "((" + LazyElement.class.getName() + ")"
201                 + varName + ")";
202             body.addln(docVarName + ".getDocumentElement();");
203             body.addln(castVar + ".makeTemplateNode(" + nodeId + ");");
204             body.addln(castVar + ".setPreFormattedText("
205                        + getPreFormattedText(element) + ");");
206         } else if (element.getNamespaceURI() == null) {
207             body.addln(docVarName + ".createTemplateElement("
208                        + JavaLang.createStringConst(element.getTagName()) + ", "
209                        + nodeId + ", " + getPreFormattedText(element) + ");");
210         } else {
211             body.addln(docVarName + ".createTemplateElementNS("
212                        + JavaLang.createStringConst(element.getNamespaceURI()) + ", "
213                        + JavaLang.createStringConst(element.getTagName()) + ", "
214                        + nodeId + ", " + getPreFormattedText(element) + ");");
215         }
216     }
217
218     /**
219      * Generate EntityReference creation.
220      */

221     protected void genEntityReferenceCreate(EntityReference JavaDoc entityRef,
222                                             String JavaDoc docVarName,
223                                             String JavaDoc varName,
224                                             JavaCode body) {
225         int nodeId = ((LazyNode)entityRef).getNodeId();
226         body.addln(varName + " = " + docVarName
227                    + ".createTemplateEntityReference("
228                    + JavaLang.createStringConst(entityRef.getNodeName()) + ", "
229                    + nodeId + ");");
230     }
231
232     /**
233      * Generate Notation creation.
234      */

235     protected void genNotationCreate(Notation JavaDoc notation,
236                                      String JavaDoc docVarName,
237                                      String JavaDoc varName,
238                                      JavaCode body) {
239         int nodeId = ((LazyNode)notation).getNodeId();
240         body.addln(varName + " = " + docVarName + ".createNotation("
241                    + JavaLang.createStringConst(notation.getNodeName()) + ", "
242                    + JavaLang.createStringConst(notation.getPublicId()) + ", "
243                    + JavaLang.createStringConst(notation.getSystemId()) + ", "
244                    + nodeId + ");");
245     }
246
247     /**
248      * Generate ProcessingInstruction creation.
249      */

250     protected void genProcessInstructionCreate(ProcessingInstruction JavaDoc procInstr,
251                                                String JavaDoc docVarName,
252                                                String JavaDoc varName,
253                                                JavaCode body) {
254         int nodeId = ((LazyNode)procInstr).getNodeId();
255         body.addln(varName + " = " + docVarName
256                    + ".createTemplateProcessingInstruction("
257                    + JavaLang.createStringConst(procInstr.getNodeName()) + ", "
258                    + JavaLang.createStringConst(procInstr.getData()) + ", "
259                    + nodeId + ");");
260     }
261
262     /**
263      * Generate Text creation.
264      */

265     protected void genTextCreate(Text text,
266                                  String JavaDoc docVarName,
267                                  String JavaDoc varName,
268                                  JavaCode body) {
269         int nodeId = ((LazyNode)text).getNodeId();
270         body.addln(varName + " = " + docVarName + ".createTemplateTextNode("
271                    + JavaLang.createStringConst(text.getData()) + ", "
272                    + nodeId + ", " + getPreFormattedText(text) + ");");
273     }
274 }
275
Popular Tags