KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > ccsg > HandlerSGImpl


1 /*
2  * Copyright 2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.ws.jaxme.generator.sg.impl.ccsg;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.xml.bind.ValidationEvent;
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.ws.jaxme.ValidationEvents;
26 import org.apache.ws.jaxme.generator.sg.AttributeSG;
27 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
28 import org.apache.ws.jaxme.generator.sg.TypeSG;
29 import org.apache.ws.jaxme.generator.types.BooleanSG;
30 import org.apache.ws.jaxme.generator.types.StringSG;
31 import org.apache.ws.jaxme.js.DirectAccessible;
32 import org.apache.ws.jaxme.js.JavaMethod;
33 import org.apache.ws.jaxme.js.JavaQName;
34 import org.apache.ws.jaxme.js.JavaQNameImpl;
35 import org.apache.ws.jaxme.js.JavaSource;
36 import org.apache.ws.jaxme.js.LocalJavaField;
37 import org.apache.ws.jaxme.js.TypedValue;
38 import org.apache.ws.jaxme.js.impl.TypedValueImpl;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42
43 /** Base implementation of
44  * {@link org.apache.ws.jaxme.generator.sg.impl.ccsg.HandlerSG},
45  * for derivation of various subclasses.
46  */

47 public abstract class HandlerSGImpl implements HandlerSG {
48     protected final ComplexTypeSG ctSG;
49     private final JavaSource javaSource;
50     private DirectAccessible paramLocalName;
51     private DirectAccessible paramNamespaceURI;
52     private DirectAccessible paramResult;
53     private DirectAccessible paramQName;
54     private DirectAccessible paramAttrs;
55
56     protected HandlerSGImpl(ComplexTypeSG pType, JavaSource pJs) {
57         ctSG = pType;
58         javaSource = pJs;
59     }
60
61     /** Returns the class being generated.
62      */

63     public JavaSource getJavaSource() {
64         return javaSource;
65     }
66
67     protected void setParamAttrs(DirectAccessible pParamAttrs) { paramAttrs = pParamAttrs; }
68
69     protected DirectAccessible getParamAttrs() { return paramAttrs; }
70
71     protected void setParamLocalName(DirectAccessible pParamLocalName) { paramLocalName = pParamLocalName; }
72
73     protected DirectAccessible getParamLocalName() { return paramLocalName; }
74
75     protected void setParamNamespaceURI(DirectAccessible pParamNamespaceURI) { paramNamespaceURI = pParamNamespaceURI; }
76
77     protected DirectAccessible getParamNamespaceURI() { return paramNamespaceURI; }
78
79     protected void setParamQName(DirectAccessible pParamQName) { paramQName = pParamQName; }
80
81     protected DirectAccessible getParamQName() { return paramQName; }
82
83     protected void setParamResult(DirectAccessible pParamResult) { paramResult = pParamResult; }
84
85     protected DirectAccessible getParamResult() { return paramResult; }
86
87     /** Returns the set of namespace URI's in <code>pAttributes</code>.
88      */

89     private Set JavaDoc createSetOfExplicitURIs(AttributeSG[] pAttributes) {
90         Set JavaDoc result = new HashSet JavaDoc();
91         for (int i = 0; i < pAttributes.length; i++) {
92             AttributeSG attr = pAttributes[i];
93             if (!attr.isWildcard()) {
94                 String JavaDoc uri = attr.getName().getNamespaceURI();
95                 if (uri == null) { uri = ""; }
96                 result.add(uri);
97             }
98         }
99         return result;
100     }
101
102     public JavaMethod newAddAttributeMethod() throws SAXException JavaDoc {
103         AttributeSG[] myAttributes = ctSG.getAttributes();
104         if (myAttributes.length == 0) {
105             return null;
106         }
107         
108         JavaMethod jm = getJavaSource().newJavaMethod("addAttribute", JavaQNameImpl.VOID, JavaSource.PUBLIC);
109         DirectAccessible pURI = jm.addParam(String JavaDoc.class, "pURI");
110         DirectAccessible pLocalName = jm.addParam(String JavaDoc.class, "pLocalName");
111         DirectAccessible pValue = jm.addParam(String JavaDoc.class, "pValue");
112         jm.addThrows(SAXException JavaDoc.class);
113         jm.addIf(pURI, " == null");
114         jm.addLine(pURI, " = \"\";");
115         jm.addEndIf();
116         
117         JavaQName resultType = ctSG.getClassContext().getXMLInterfaceName();
118         LocalJavaField result = jm.newJavaField(resultType);
119         result.addLine("(", resultType, ") result");
120
121         Set JavaDoc uris = createSetOfExplicitURIs(myAttributes);
122         boolean first = true;
123         for (Iterator JavaDoc iter = uris.iterator(); iter.hasNext(); ) {
124             String JavaDoc uri = (String JavaDoc) iter.next();
125             jm.addIf(first, JavaSource.getQuoted(uri), ".equals(", pURI, ")");
126             first = false;
127             boolean firstInNamespace = true;
128             for (int i = 0; i < myAttributes.length; i++) {
129                 AttributeSG attr = myAttributes[i];
130                 if (attr.isWildcard()) {
131                     continue;
132                 }
133                 String JavaDoc jUri = attr.getName().getNamespaceURI();
134                 if (jUri == null) { jUri = ""; }
135                 if (!uri.equals(jUri)) {
136                     continue;
137                 }
138                 
139                 jm.addIf(firstInNamespace, JavaSource.getQuoted(attr.getName().getLocalName()), ".equals(", pLocalName, ")");
140                 firstInNamespace = false;
141                 TypedValue v = createSimpleTypeConversion(jm, myAttributes[i].getTypeSG(), pValue, "@" + myAttributes[i].getName());
142                 attr.getPropertySG().setValue(jm, result, v, null);
143                 jm.addLine("return;");
144             }
145             if (!firstInNamespace) {
146                 jm.addEndIf();
147             }
148         }
149         if (!first) {
150             jm.addEndIf();
151         }
152         
153         AttributeSG wildcard = null;
154         for (int i = 0; i < myAttributes.length; i++) {
155             if (myAttributes[i].isWildcard()) {
156                 wildcard = myAttributes[i];
157                 break;
158             }
159         }
160         if (wildcard == null) {
161             jm.addLine("super.addAttribute(", pURI, ", ", pLocalName, ", ", pValue, ");");
162         } else {
163             jm.addTry();
164             jm.addLine(result, ".", wildcard.getPropertySG().getXMLSetMethodName(), "(new ",
165                     QName JavaDoc.class, "(", pURI, ", ", pLocalName, "), ", pValue, ");");
166             jm.addCatch(IllegalArgumentException JavaDoc.class);
167             jm.addLine("getHandler().validationEvent(", ValidationEvent.class, ".ERROR, ",
168                     JavaSource.getQuoted("Invalid namespace for anyAttribute: '"),
169                     " + ", pURI, " + ", JavaSource.getQuoted("', attribute name is '"),
170                     " + ", pLocalName, " + ", JavaSource.getQuoted("'"),
171                     ", ", ValidationEvents.class, ".EVENT_UNKNOWN_ANY_ATTRIBUTE, null);");
172             jm.addEndTry();
173         }
174         return jm;
175     }
176
177     public JavaMethod newStartElementMethod() throws SAXException JavaDoc {
178         JavaSource js = getJavaSource();
179         JavaMethod jm = js.newJavaMethod("startElement", boolean.class, JavaSource.PUBLIC);
180         DirectAccessible pNamespaceURI = jm.addParam(String JavaDoc.class, "pNamespaceURI");
181         DirectAccessible pLocalName = jm.addParam(String JavaDoc.class, "pLocalName");
182         DirectAccessible pQName = jm.addParam(String JavaDoc.class, "pQName");
183         DirectAccessible pAttr = jm.addParam(Attributes JavaDoc.class, "pAttr");
184         setParamNamespaceURI(pNamespaceURI);
185         setParamLocalName(pLocalName);
186         setParamQName(pQName);
187         setParamAttrs(pAttr);
188         setParamResult(null);
189         jm.addThrows(SAXException JavaDoc.class);
190         return jm;
191     }
192
193     public JavaMethod newEndElementMethod() throws SAXException JavaDoc {
194         JavaMethod jm = getJavaSource().newJavaMethod("endElement", void.class, JavaSource.PUBLIC);
195         DirectAccessible pNamespaceURI = jm.addParam(String JavaDoc.class, "pNamespaceURI");
196         DirectAccessible pLocalName = jm.addParam(String JavaDoc.class, "pLocalName");
197         DirectAccessible pQName = jm.addParam(String JavaDoc.class, "pQName");
198         DirectAccessible pResult = jm.addParam(Object JavaDoc.class, "pResult");
199         setParamNamespaceURI(pNamespaceURI);
200         setParamLocalName(pLocalName);
201         setParamQName(pQName);
202         setParamAttrs(null);
203         setParamResult(pResult);
204         jm.addThrows(SAXException JavaDoc.class);
205         return jm;
206     }
207
208     public JavaMethod newIsFinishedMethod() throws SAXException JavaDoc {
209         JavaMethod jm = getJavaSource().newJavaMethod("isFinished", boolean.class, JavaSource.PUBLIC);
210         return jm;
211     }
212
213     public JavaMethod newIsEmptyMethod() throws SAXException JavaDoc {
214         JavaMethod jm = getJavaSource().newJavaMethod("isEmpty", boolean.class, JavaSource.PUBLIC);
215         return jm;
216     }
217     
218     public JavaMethod newIsAtomicMethod() throws SAXException JavaDoc {
219         JavaMethod jm = getJavaSource().newJavaMethod("isAtomic", boolean.class, JavaSource.PUBLIC);
220         return jm;
221     }
222
223     protected TypedValue createSimpleTypeConversion(JavaMethod pJm, TypeSG pType,
224                                                     TypedValue pValue, String JavaDoc pName)
225             throws SAXException JavaDoc {
226         JavaQName runtimeType = pType.getSimpleTypeSG().getRuntimeType();
227         boolean causingParseConversionEvent = pType.getSimpleTypeSG().isCausingParseConversionEvent();
228         LocalJavaField f = null;
229         if (causingParseConversionEvent) {
230             f = pJm.newJavaField(runtimeType);
231             pJm.addTry();
232         }
233         Object JavaDoc s = new Object JavaDoc[]{"(", StringSG.STRING_TYPE, ") ", pValue};
234         TypedValue result = new TypedValueImpl(pType.getSimpleTypeSG().getCastFromString(pJm, s, "getHandler()"),
235                                                pType.getSimpleTypeSG().getRuntimeType());
236         if (causingParseConversionEvent) {
237             pJm.addLine(f, " = ", result, ";");
238             result = f;
239             DirectAccessible e = pJm.addCatch(Exception JavaDoc.class);
240             pJm.addLine("getHandler().parseConversionEvent(",
241                         JavaSource.getQuoted("Failed to convert value of " + pName + ": "),
242                         " + ", pValue, ", ", e, ");");
243             Object JavaDoc o;
244             if (runtimeType.isPrimitive()) {
245                 if (BooleanSG.BOOLEAN_TYPE.equals(runtimeType)) {
246                     o = "false";
247                 } else {
248                     o = "0";
249                 }
250             } else {
251                 o = "null";
252             }
253             pJm.addLine(f, " = ", o, ";");
254             pJm.addEndTry();
255         }
256         return result;
257     }
258
259     public void generate() throws SAXException JavaDoc {
260         newAddAttributeMethod();
261         newStartElementMethod();
262         newEndElementMethod();
263         newIsFinishedMethod();
264         newIsEmptyMethod();
265         newIsAtomicMethod();
266     }
267 }
268
Popular Tags