KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package org.enhydra.xml.xmlc.dom.lazydom;
25
26 import org.enhydra.xml.lazydom.NodeIdMap;
27 import org.enhydra.xml.xmlc.codegen.JavaClass;
28 import org.enhydra.xml.xmlc.codegen.JavaCode;
29 import org.enhydra.xml.xmlc.codegen.JavaField;
30 import org.enhydra.xml.xmlc.codegen.JavaMethod;
31 import org.enhydra.xml.xmlc.codegen.JavaModifiers;
32 import org.enhydra.xml.xmlc.compiler.ElementInfo;
33 import org.enhydra.xml.xmlc.dom.AccessorGenerator;
34 import org.enhydra.xml.xmlc.dom.generic.GenericAccessorGenerator;
35 import org.w3c.dom.Document JavaDoc;
36
37 /*
38  * FIXME: Should syncAccessMethods just generate an error for the
39  * LazyDOM? This way, elementIds could be statics and access code
40  * simpler.
41  * Na, better yet, make syncAccessMethods a compiler-time option.
42  */

43
44 /**
45  * Generate access methods for the Lazy DOM.
46  */

47 public class LazyDOMAccessorGenerator extends GenericAccessorGenerator
48     implements AccessorGenerator {
49     /**
50      * Map of document nodes to node ids.
51      */

52     private NodeIdMap fNodeIdMap;
53
54     /**
55      * Constructor.
56      */

57     protected LazyDOMAccessorGenerator(Document JavaDoc document) {
58         fNodeIdMap = new NodeIdMap(document);
59     }
60
61     /**
62      * Get the name of the field that will store an elements' node id.
63      */

64     private String JavaDoc getElementNodeIdField(ElementInfo elementInfo) {
65         return "$elementId_" + elementInfo.getXmlId();
66     }
67
68     /**
69      * This method does nothing for the Lazy DOM.
70      * @see AccessorGenerator#createAccessMethodInit
71      */

72     public void createAccessMethodInit(ElementInfo elementInfo,
73                                        String JavaDoc elementVarName,
74                                        JavaCode body) {
75     }
76
77     /**
78      * Create code cached information so that element access methods
79      * return null.
80      * @see AccessorGenerator#createNullElementAccess
81      */

82     public void createNullElementAccess(ElementInfo elementInfo,
83                                         JavaCode body) {
84         body.addln(getElementNodeIdField(elementInfo) + " = -1;");
85         body.addln(getElementField(elementInfo) + " = null;");
86     }
87
88     /**
89      * Create code to reset the element access information from a node.
90      * @see AccessorGenerator#createResetElementAccess
91      */

92     public void createResetElementAccess(ElementInfo elementInfo,
93                                          String JavaDoc nodeVarName,
94                                          JavaCode body) {
95         body.addln(getElementNodeIdField(elementInfo) + " = "
96                    + fNodeIdMap.getIdStr(elementInfo.getElement())
97                    + ";");
98         body.addln(getElementField(elementInfo) + " = ("
99                    + elementInfo.getClassName() + ")" + nodeVarName + ";");
100     }
101
102     /**
103      * Create code to test if an element is expanded and expand if necessary.
104      */

105     private void createExpandedElementCheck(ElementInfo elementInfo,
106                                             JavaCode body) {
107         String JavaDoc fieldName = getElementField(elementInfo);
108         String JavaDoc idFieldName = getElementNodeIdField(elementInfo);
109         body.addln(new String JavaDoc[] {
110             "if ((" + fieldName + " == null) && (" + idFieldName + " >= 0)) {",
111             " " + fieldName + " = (" + elementInfo.getClassName() + ")"
112             + LazyDOMDocBuilderGenerator.LAZY_DOCUMENT_FIELD + ".getNodeById(" + idFieldName + ");",
113             "}",
114         });
115     }
116
117     /**
118      * Create an element accessor method (getElementXXX() or getTextXXX()).
119      * @see AccessorGenerator#createAccessorMethod
120      */

121     public JavaMethod createAccessorMethod(ElementInfo elementInfo,
122                                            ElementInfo.AccessorInfo accessor,
123                                            boolean createDelegation,
124                                            JavaClass docClass) {
125         JavaMethod method = setupGetElementMethod(elementInfo, accessor,
126                                                   createDelegation,
127                                                   docClass);
128
129         // Create return of value
130
JavaCode body = method.getCode();
131         createExpandedElementCheck(elementInfo, body);
132         body.addln("return " + getElementField(elementInfo) + ";");
133         
134         // Create fields to store element id and element.
135
JavaField field = new JavaField(getElementNodeIdField(elementInfo),
136                                         "int",
137                                         JavaModifiers.PRIVATE,
138                                         (String JavaDoc)null,
139                                         fNodeIdMap.getIdStr(elementInfo.getElement()));
140         docClass.addField(field);
141         field = new JavaField(getElementField(elementInfo),
142                               elementInfo.getClassName(),
143                               JavaModifiers.PRIVATE,
144                               (String JavaDoc)null, null);
145         docClass.addField(field);
146
147         return method;
148     }
149
150     /**
151      * Create the setText method for an element.
152      * @see AccessorGenerator#createSetTextMethod
153      */

154     public JavaMethod createSetTextMethod(ElementInfo elementInfo,
155                                           boolean createDelegation,
156                                           JavaClass docClass) {
157         JavaMethod method = setupSetTextMethod(elementInfo,
158                                                createDelegation,
159                                                docClass);
160
161         // Create set of value
162
JavaCode body = method.getCode();
163         createExpandedElementCheck(elementInfo, body);
164         body.addln("doSetText(" + getElementField(elementInfo) + ", text);");
165
166         return method;
167     }
168 }
169
Popular Tags