KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004 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  */

17 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.apache.ws.jaxme.WildcardAttribute;
28 import org.apache.ws.jaxme.generator.sg.AttributeSG;
29 import org.apache.ws.jaxme.generator.sg.PropertySG;
30 import org.apache.ws.jaxme.js.DirectAccessible;
31 import org.apache.ws.jaxme.js.JavaClassInitializer;
32 import org.apache.ws.jaxme.js.JavaComment;
33 import org.apache.ws.jaxme.js.JavaField;
34 import org.apache.ws.jaxme.js.JavaMethod;
35 import org.apache.ws.jaxme.js.JavaQName;
36 import org.apache.ws.jaxme.js.JavaQNameImpl;
37 import org.apache.ws.jaxme.js.JavaSource;
38 import org.apache.ws.jaxme.js.LocalJavaField;
39 import org.apache.ws.jaxme.js.Parameter;
40 import org.apache.ws.jaxme.xs.XSWildcard;
41 import org.apache.ws.jaxme.xs.xml.XsAnyURI;
42 import org.apache.ws.jaxme.xs.xml.XsNamespaceList;
43 import org.apache.ws.jaxme.xs.xml.XsSchemaHeader;
44 import org.xml.sax.SAXException JavaDoc;
45
46
47 /** <p>PropertySG for attribute wildcards.</p>
48  *
49  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
50  */

51 public class AnyAttributePropertySG extends JAXBPropertySG {
52     private final XsNamespaceList namespaceList;
53     private final XsSchemaHeader schemaHeader;
54
55     protected AnyAttributePropertySG(AttributeSG pAttribute, XSWildcard pWildcard) {
56         super("anyAttribute", pAttribute.getSchema(), pWildcard, null, null);
57         namespaceList = pWildcard.getNamespaceList();
58         schemaHeader = pWildcard.getSchemaHeader();
59     }
60
61     protected String JavaDoc getTargetNamespace() {
62         String JavaDoc targetNamespace = schemaHeader == null ? "" : schemaHeader.getTargetNamespace().toString();
63         if (targetNamespace == null) {
64             targetNamespace = "";
65         }
66         return targetNamespace;
67     }
68
69     public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
70         if (pSource.isInterface()) {
71             return null;
72         }
73         JavaQName runtimeType = JavaQNameImpl.getInstance(Map JavaDoc.class);
74         JavaField jf = pSource.newJavaField(pController.getXMLFieldName(), runtimeType, JavaSource.PRIVATE);
75         jf.setFinal(true);
76         jf.addLine("new ", HashMap JavaDoc.class, "()");
77         return jf;
78     }
79
80     protected JavaField getNamespaces(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
81         if (namespaceList.isAny() || namespaceList.isOther()) {
82             return null;
83         }
84         String JavaDoc namespaces = pController.getXMLFieldName() + "Namespaces";
85         JavaField jf = pSource.newJavaField(namespaces, Set JavaDoc.class);
86         jf.setStatic(true);
87         jf.setFinal(true);
88         JavaClassInitializer jci = pSource.newJavaClassInitializer();
89         LocalJavaField set = jci.newJavaField(Set JavaDoc.class);
90         set.addLine("new ", HashSet JavaDoc.class, "()");
91         XsAnyURI[] uris = namespaceList.getUris();
92         for (int i = 0; i < uris.length; i++) {
93             jci.addLine(set, ".add(", JavaSource.getQuoted(uris[i].toString()), ");");
94         }
95         jci.addLine(jf, " = ", Collections JavaDoc.class, ".unmodifiableSet(", set, ");");
96         return jf;
97     }
98
99     protected void getValidNamespaceCheck(PropertySG pController, JavaMethod pMethod, Parameter pName)
100             throws SAXException JavaDoc {
101         if (namespaceList.isAny()) {
102             // No checks for namespace
103
} else if (namespaceList.isOther()) {
104             String JavaDoc targetNamespace = getTargetNamespace();
105             pMethod.addIf(JavaSource.getQuoted(targetNamespace), ".equals(", pName, ".getNamespaceURI())");
106             pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
107                                 JavaSource.getQuoted("The namespace of the pName argument must not match the target namespace '" +
108                                                      targetNamespace + "'."));
109             pMethod.addEndIf();
110         } else {
111             String JavaDoc targetNamespace = getTargetNamespace();
112             String JavaDoc namespaces = pController.getXMLFieldName() + "Namespaces";
113             pMethod.addIf("!", namespaces, ".contains(", pName, ".getNamespaceURI())");
114             pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
115                                 JavaSource.getQuoted("The namespace of the pName argument must not match the target namespace '" +
116                                                      targetNamespace + "'."));
117             pMethod.addEndIf();
118         }
119     }
120
121     public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
122         JavaMethod jm = pSource.newJavaMethod(pController.getXMLGetMethodName(), String JavaDoc.class, JavaSource.PUBLIC);
123         JavaComment jc = jm.newComment();
124         jc.addLine("<p>Returns the value of the 'anyAttribute' named <code>pName</code>.</p>");
125         jc.addLine("@return Attribute value or null, if the attribute is not set.");
126         jc.addLine("@throws NullPointerException The <code>pName</code> argument is null.");
127         if (!namespaceList.isAny()) {
128             jc.addLine("@throws IllegalArgumentException The namespace <code>pName.getNamespaceURI()</code> is invalid.");
129         }
130         Parameter pName = jm.addParam(QName JavaDoc.class, "pName");
131         if (!pSource.isInterface()) {
132             jm.addIf(pName, " == null");
133             jm.addThrowNew(NullPointerException JavaDoc.class, JavaSource.getQuoted("The pName argument must not be null."));
134             jm.addEndIf();
135             getValidNamespaceCheck(pController, jm, pName);
136             jm.addLine("return (", String JavaDoc.class, ") ", pController.getXMLFieldName(), ".get(", pName, ");");
137         }
138         return jm;
139     }
140
141     public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
142         JavaMethod jm = pSource.newJavaMethod(pController.getXMLSetMethodName(), void.class, JavaSource.PUBLIC);
143         JavaComment jc = jm.newComment();
144         jc.addLine("<p>Sets the 'anyAttribute' named <code>pName</code> to the value <code>pValue</code>.</p>");
145         if (!namespaceList.isAny()) {
146             jc.addLine("@throws IllegalArgumentException The namespace <code>pName.getNamespaceURI()</code> is invalid.");
147         }
148         jc.addLine("@throws NullPointerException Either of the arguments <code>pName</code> or <code>pValue</code> is null.");
149         Parameter pName = jm.addParam(QName JavaDoc.class, "pName");
150         Parameter pValue = jm.addParam(String JavaDoc.class, "pValue");
151         if (!pSource.isInterface()) {
152             jm.addIf(pName, " == null");
153             jm.addThrowNew(NullPointerException JavaDoc.class, JavaSource.getQuoted("The pName argument must not be null."));
154             jm.addEndIf();
155             jm.addIf(pValue, " == null");
156             jm.addThrowNew(NullPointerException JavaDoc.class, JavaSource.getQuoted("The pValue argument must not be null."));
157             jm.addEndIf();
158             getValidNamespaceCheck(pController, jm, pName);
159             jm.addLine(pController.getXMLFieldName(), ".put(", pName, ", ", pValue, ");");
160         }
161         return jm;
162     }
163
164     public JavaMethod getXMLUnsetMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
165         JavaMethod jm = pSource.newJavaMethod("un" + pController.getXMLSetMethodName(), boolean.class, JavaSource.PUBLIC);
166         JavaComment jc = jm.newComment();
167         jc.addLine("<p>Removes the 'anyAttribute' named <code>pName</code>.</p>");
168         if (!namespaceList.isAny()) {
169             jc.addLine("@throws IllegalArgumentException The namespace <code>pName.getNamespaceURI()</code> is invalid.");
170         }
171         jc.addLine("@throws NullPointerException Either of the arguments <code>pName</code> or <code>pValue</code> is null.");
172         jc.addLine("@return True, if the attribute was set, otherwise false.");
173         Parameter pName = jm.addParam(QName JavaDoc.class, "pName");
174         if (!pSource.isInterface()) {
175             jm.addIf(pName, " == null");
176             jm.addThrowNew(NullPointerException JavaDoc.class, JavaSource.getQuoted("The pName argument must not be null."));
177             jm.addEndIf();
178             getValidNamespaceCheck(pController, jm, pName);
179             jm.addLine("return ", pController.getXMLFieldName(), ".remove(", pName, ") != null;");
180         }
181         return jm;
182     }
183
184     public JavaMethod getXMLGetArrayMethod(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
185         JavaMethod jm = pSource.newJavaMethod(pController.getXMLGetMethodName() + "Array", WildcardAttribute[].class, JavaSource.PUBLIC);
186         JavaComment jc = jm.newComment();
187         jc.addLine("<p>Returns the array of 'anyAttributes'.</p>");
188         LocalJavaField size = jm.newJavaField(int.class);
189         size.addLine(pController.getXMLFieldName(), ".size()");
190         LocalJavaField result = jm.newJavaField(jm.getType());
191         result.addLine("new ", WildcardAttribute.class, "[", size, "]");
192         DirectAccessible iter = jm.addForCollection(new Object JavaDoc[]{pController.getXMLFieldName(), ".entrySet()"});
193         LocalJavaField entry = jm.newJavaField(Map.Entry JavaDoc.class);
194         entry.addLine("(", Map.Entry JavaDoc.class, ") ", iter, ".next()");
195         if (!pSource.isInterface()) {
196             jm.addLine(result, "[--", size, "] = new ", WildcardAttribute.class, "((",
197                        QName JavaDoc.class, ") ", entry, ".getKey(), (", String JavaDoc.class, ") ",
198                        entry, ".getValue());");
199             jm.addEndFor();
200             jm.addLine("return ", result, ";");
201         }
202         return jm;
203     }
204
205     public void generate(PropertySG pController, JavaSource pSource) throws SAXException JavaDoc {
206         super.generate(pController, pSource);
207         getXMLUnsetMethod(pController, pSource);
208         getXMLGetArrayMethod(pController, pSource);
209         if (!pSource.isInterface()) {
210             getNamespaces(pController, pSource);
211         }
212     }
213 }
214
Popular Tags