KickJava   Java API By Example, From Geeks To Geeks.

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


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: GenericAccessorGenerator.java,v 1.3 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.codegen.JavaClass;
27 import org.enhydra.xml.xmlc.codegen.JavaCode;
28 import org.enhydra.xml.xmlc.codegen.JavaField;
29 import org.enhydra.xml.xmlc.codegen.JavaMethod;
30 import org.enhydra.xml.xmlc.codegen.JavaModifiers;
31 import org.enhydra.xml.xmlc.codegen.JavaParameter;
32 import org.enhydra.xml.xmlc.compiler.ClassGenerator;
33 import org.enhydra.xml.xmlc.compiler.ElementInfo;
34 import org.enhydra.xml.xmlc.dom.AccessorGenerator;
35
36 /**
37  * Generate access methods for a DOM only using the W3C DOM.
38  * Many methods in the class are protected so it can be used
39  * as a base for other AccessorGenerator classes.
40  */

41 public class GenericAccessorGenerator implements AccessorGenerator {
42     /**
43      * Parameter for setTextXXX methods.
44      * A static since in can be shared.
45      */

46     private static final JavaParameter[] setTextParam = {
47         new JavaParameter("text", "String",
48                           "New value for text child.")
49     };
50
51     /**
52      * Get the name of the field that will store an element.
53      */

54     protected String JavaDoc getElementField(ElementInfo elementInfo) {
55         return "$element_" + elementInfo.getJavaId();
56     }
57
58     /**
59      * Create code to setup an access method for an element when building the
60      * DOM.
61      * @see AccessorGenerator#createAccessMethodInit
62      */

63     public void createAccessMethodInit(ElementInfo elementInfo,
64                                        String JavaDoc elementVarName,
65                                        JavaCode body) {
66         body.add(getElementField(elementInfo)
67                  + " = (" + elementInfo.getReferenceName() + ")"
68                  + elementVarName + ";");
69     }
70
71     /**
72      * Create code cached information so that element access methods
73      * return null.
74      * @see AccessorGenerator#createNullElementAccess
75      */

76     public void createNullElementAccess(ElementInfo elementInfo,
77                                         JavaCode body) {
78         body.add(getElementField(elementInfo) + " = null;");
79     }
80
81     /**
82      * Create code to reset the element access information from a node.
83      * @see AccessorGenerator#createResetElementAccess
84      */

85     public void createResetElementAccess(ElementInfo elementInfo,
86                                          String JavaDoc nodeVarName,
87                                          JavaCode body) {
88         body.addln(getElementField(elementInfo) + " = ("
89                  + elementInfo.getReferenceName() + ")" + nodeVarName + ";");
90     }
91
92     /**
93      * Set up a accessor (getElementXXX or getTagXXX) method object for an
94      * element. Creates the method, documentation, and delegate check code.
95      */

96     protected JavaMethod setupGetElementMethod(ElementInfo elementInfo,
97                                                ElementInfo.AccessorInfo accessor,
98                                                boolean createDelegation,
99                                                JavaClass docClass) {
100         String JavaDoc[] doc = {
101             "Get the element with id <CODE>" + elementInfo.getXmlId() + "</CODE>.",
102             "@see " + accessor.fReturnType,
103         };
104         JavaMethod method
105             = new JavaMethod(accessor.fName,
106                              accessor.fReturnType,
107                              JavaModifiers.PUBLIC,
108                              null, doc);
109         docClass.addMethod(method);
110
111         // Setup delegate check and call, if requested.
112
JavaCode body = method.getCode();
113         if (createDelegation) {
114             body.addln(new String JavaDoc[] {
115                 "if (" + ClassGenerator.DELEGATE_FIELD_NAME + " != null) {",
116                 " return " + ClassGenerator.DELEGATE_FIELD_NAME + "." + method.getName() + "();",
117                 "}"
118             });
119         }
120         return method;
121     }
122
123     /**
124      * Create an element accessor method (getElementXXX() or getTextXXX()).
125      * @see AccessorGenerator#createAccessorMethod
126      */

127     public JavaMethod createAccessorMethod(ElementInfo elementInfo,
128                                            ElementInfo.AccessorInfo accessor,
129                                            boolean createDelegation,
130                                            JavaClass docClass) {
131         JavaMethod method = setupGetElementMethod(elementInfo, accessor,
132                                                   createDelegation,
133                                                   docClass);
134
135         // Create return of value
136
JavaCode body = method.getCode();
137         body.addln("return " + getElementField(elementInfo) + ";");
138
139         // Create field to store element.
140
JavaField field = new JavaField(getElementField(elementInfo),
141                                         elementInfo.getReferenceName(),
142                                         JavaModifiers.PRIVATE,
143                                         (String JavaDoc)null, null);
144         docClass.addField(field);
145         return method;
146     }
147
148     /**
149      * Setup the setText method for an element.
150      * Creates the method, documentation, and delegate check code.
151      */

152     protected JavaMethod setupSetTextMethod(ElementInfo elementInfo,
153                                             boolean createDelegation,
154                                             JavaClass docClass) {
155         String JavaDoc[] doc = {
156             "Get the value of text child of element <CODE>" + elementInfo.getXmlId() + "</CODE>.",
157             "@see org.w3c.dom.Text"
158         };
159
160         JavaMethod method
161             = new JavaMethod("setText" + elementInfo.getJavaId(),
162                              "void",
163                              JavaModifiers.PUBLIC,
164                              setTextParam, doc);
165         docClass.addMethod(method);
166
167         // Setup delegate check and call, if requested.
168
JavaCode body = method.getCode();
169         if (createDelegation) {
170             body.addln(new String JavaDoc[] {
171                 "if (" + ClassGenerator.DELEGATE_FIELD_NAME + " != null) {",
172                 " " + ClassGenerator.DELEGATE_FIELD_NAME + "." + method.getName() + "(text);",
173                 " return;",
174                 "}"
175             });
176         }
177         return method;
178     }
179
180     /**
181      * Create the setText method for an element.
182      * @see AccessorGenerator#createSetTextMethod
183      */

184     public JavaMethod createSetTextMethod(ElementInfo elementInfo,
185                                           boolean createDelegation,
186                                           JavaClass docClass) {
187         JavaMethod method = setupSetTextMethod(elementInfo,
188                                                createDelegation,
189                                                docClass);
190
191         // Create set of value
192
JavaCode body = method.getCode();
193         body.addln("doSetText(" + getElementField(elementInfo) + ", text);");
194         return method;
195     }
196 }
197
Popular Tags