KickJava   Java API By Example, From Geeks To Geeks.

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


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: LazyDOMDocBuilderGenerator.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.OutputOptions;
27 import org.enhydra.xml.lazydom.LazyDocument;
28 import org.enhydra.xml.lazydom.TemplateDOM;
29 import org.enhydra.xml.xmlc.XMLCException;
30 import org.enhydra.xml.xmlc.codegen.JavaClass;
31 import org.enhydra.xml.xmlc.codegen.JavaCode;
32 import org.enhydra.xml.xmlc.codegen.JavaField;
33 import org.enhydra.xml.xmlc.codegen.JavaLang;
34 import org.enhydra.xml.xmlc.codegen.JavaMethod;
35 import org.enhydra.xml.xmlc.codegen.JavaModifiers;
36 import org.enhydra.xml.xmlc.compiler.ClassGenerator;
37 import org.enhydra.xml.xmlc.compiler.ElementTable;
38 import org.enhydra.xml.xmlc.dom.AccessorGenerator;
39 import org.enhydra.xml.xmlc.dom.DocBuilderGenerator;
40 import org.enhydra.xml.xmlc.dom.XMLCDocument;
41 import org.enhydra.xml.xmlc.dom.generic.DOMBuilderGenerator;
42 import org.w3c.dom.Document JavaDoc;
43
44 //FIXME: review use of document vs XMLCDocument.
45

46 /**
47  * Class to generate code to build the document tree for the Lazy DOM.
48  * This creates a static template DOM that is used to instantiate individual
49  * nodes.
50  * <p>
51  * The static template is initialized by a set of methods to get around the
52  * method size limitation of doing this as an array initializer.
53  */

54 public class LazyDOMDocBuilderGenerator implements DocBuilderGenerator {
55     /*
56      * Maximum creation-cost per build method. One unit is approximately the
57      * cost to create one node.
58      */

59     private static final int MAX_CREATE_COST_PER_BUILD_METHOD = 256;
60
61     /**
62      * Name of static template document table.
63      */

64     private static String JavaDoc TEMPLATE_DOCUMENT_FIELD = "fTemplateDocument";
65     
66     /*
67      * Name of variable in init function that holds document type object.
68      */

69     private static final String JavaDoc DOC_TYPE_VAR = "documentType";
70
71     /**
72      * Name for the variable holding the Document during creation.
73      */

74     public static final String JavaDoc DOCUMENT_VAR_NAME = "document";
75
76     /**
77      * Name of class field containing OutputOptions that was used
78      * to preformat the object.
79      */

80     private static String JavaDoc PREFORMAT_OUTPUT_OPTIONS_FIELD = "fPreFormatOutputOptions";
81     
82     /**
83      * Field containing LazyDocument.
84      */

85     static String JavaDoc LAZY_DOCUMENT_FIELD = "fLazyDocument";
86
87     /**
88      * Base name of template DOM building methods.
89      */

90     private static final String JavaDoc SUBDOCUMENT_BUILD_METHOD = "buildTemplateSubDocument";
91
92     /**
93      * Use a TemplateDOM object to assign node ids. Ensures that
94      * the node ids are identical.
95      */

96     private TemplateDOM templateDOM;
97
98     /**
99      * If not null, the object to preformat node text.
100      */

101     private PreFormatter fPreFormatter;
102
103     /**
104      * Generate code to create a document from a TemplateDOM.
105      * @param document Document that will be recreated.
106      * @param templateFieldName Name of template field.
107      * @param docFieldName Name of field for document object.
108      */

109     public void generateDocCreateFromTemplate(Document document,
110                                               String JavaDoc templateFieldName,
111                                               String JavaDoc docFieldName,
112                                               JavaCode body) {
113         String JavaDoc docClass = document.getClass().getName();
114
115         body.addln(docFieldName + " = (" + docClass + ")(("
116                    + LazyDomFactory.class.getName()
117                    + ")" + ClassGenerator.DOM_FACTORY_FIELD_NAME
118                    + ").createDocument(" + templateFieldName + ");");
119     }
120
121     /**
122      * Do initialization for the preformatter.
123      */

124     private void preFormatterInit(JavaClass docClass) {
125         // Create static variable that will hold the OutputOptions used to
126
// format the class
127
docClass.addField(new JavaField(PREFORMAT_OUTPUT_OPTIONS_FIELD,
128                                         OutputOptions.class.getName(),
129                                         JavaModifiers.PRIVATE | JavaModifiers.STATIC | JavaModifiers.FINAL,
130                                         new String JavaDoc[]{"Options used to preformat the document when compiled"},
131                                         null));
132
133         // Generate code to recreate the output options.
134
fPreFormatter.createOutputOptionsCodeGenerator(PREFORMAT_OUTPUT_OPTIONS_FIELD,
135                                                        docClass.getClassInitializer());
136     }
137
138     /**
139      * Generate template document initialization,
140      */

141     private void createTemplateDocInit(XMLCDocument xmlcDoc,
142                                        AccessorGenerator accessorGenerator,
143                                        ElementTable elementTable,
144                                        JavaClass docClass) throws XMLCException {
145         LazyDocument document = (LazyDocument)xmlcDoc.getDocument();
146
147         // This assigns node ids.
148
templateDOM = new TemplateDOM(document);
149
150         JavaCode body = docClass.getClassInitializer();
151         DOMBuilderGenerator domBuilderGenerator
152             = new DOMBuilderGenerator(SUBDOCUMENT_BUILD_METHOD,
153                                       xmlcDoc.getDocument(),
154                                       LazyDocument.class.getName(),
155                                       new LazyDOMNodeCreateGenerator(xmlcDoc,
156                                                                      fPreFormatter),
157                                       accessorGenerator,
158                                       elementTable,
159                                       docClass,
160                                       MAX_CREATE_COST_PER_BUILD_METHOD,
161                                       true);
162
163         JavaField field = new JavaField(TEMPLATE_DOCUMENT_FIELD,
164                                         TemplateDOM.class.getName(),
165                                         JavaModifiers.PRIVATE | JavaModifiers.STATIC | JavaModifiers.FINAL,
166                                         "Template document shared by all instances.",
167                                         null);
168         docClass.addField(field);
169         body.add(document.getClass().getName() + " " + DOCUMENT_VAR_NAME + " = ");
170         domBuilderGenerator.createMethodCall(body);
171
172         // Set up the template document.
173
body.addln(TEMPLATE_DOCUMENT_FIELD + " = new " + TemplateDOM.class.getName()
174                    + "(" + DOCUMENT_VAR_NAME + ");");
175
176         if (fPreFormatter != null) {
177             preFormatterInit(docClass);
178         }
179     }
180
181     /**
182      * Generate code to set preformat OutputOptions in document.
183      */

184     private void createPreformattedTextSetup(JavaCode body) {
185         body.addln(LAZY_DOCUMENT_FIELD + ".setPreFormatOutputOptions("
186                    + PREFORMAT_OUTPUT_OPTIONS_FIELD + ");");
187     }
188
189     /**
190      * Create method that creates an unexpanded instance document.
191      */

192     private void createBuildDocument(XMLCDocument xmlcDoc,
193                                      JavaClass docClass,
194                                      JavaMethod buildDocumentMethod) {
195         // Create document field
196
JavaField docField = new JavaField(LAZY_DOCUMENT_FIELD,
197                                            LazyDocument.class.getName(),
198                                            JavaModifiers.PRIVATE,
199                                            "Lazy DOM document", null);
200         docClass.addField(docField);
201
202         // Add code to build document
203
JavaCode body = buildDocumentMethod.getCode();
204
205         // Create from template using a XMLCDomFactory
206
generateDocCreateFromTemplate(xmlcDoc.getDocument(),
207                                       TEMPLATE_DOCUMENT_FIELD,
208                                       LAZY_DOCUMENT_FIELD,
209                                       body);
210         if (fPreFormatter != null) {
211             createPreformattedTextSetup(body);
212         }
213
214         // Save document information.
215
body.addln("setDocument(" + LAZY_DOCUMENT_FIELD + ", "
216                    + JavaLang.createStringConst(xmlcDoc.getDomFactory().getMIMEType()) + ", "
217                    + JavaLang.createStringConst(xmlcDoc.getEncoding()) + ");");
218     }
219
220     /**
221      * Generate the document builder method.
222      * @see DocBuilderGenerator#createBuildDocumentMethod
223      */

224     public void createBuildDocumentMethod(XMLCDocument xmlcDoc,
225                                           AccessorGenerator accessorGenerator,
226                                           ElementTable elementTable,
227                                           JavaClass docClass,
228                                           JavaMethod buildDocumentMethod) throws XMLCException {
229         fPreFormatter = new PreFormatter(xmlcDoc);
230         createTemplateDocInit(xmlcDoc, accessorGenerator,
231                               elementTable, docClass);
232         createBuildDocument(xmlcDoc, docClass, buildDocumentMethod);
233     }
234 }
235
236
Popular Tags