KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > ino > api4j > TaminoAPI4JSG


1 /*
2  * Copyright 2003, 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  
18 package org.apache.ws.jaxme.generator.ino.api4j;
19
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.ws.jaxme.JMElement;
24 import org.apache.ws.jaxme.generator.SchemaReader;
25 import org.apache.ws.jaxme.generator.sg.AttributeSG;
26 import org.apache.ws.jaxme.generator.sg.AttributeSGChain;
27 import org.apache.ws.jaxme.generator.sg.ObjectSG;
28 import org.apache.ws.jaxme.generator.sg.ObjectSGChain;
29 import org.apache.ws.jaxme.generator.sg.SGFactory;
30 import org.apache.ws.jaxme.generator.sg.SGFactoryChain;
31 import org.apache.ws.jaxme.generator.sg.SchemaSGChain;
32 import org.apache.ws.jaxme.generator.sg.TypeSG;
33 import org.apache.ws.jaxme.generator.sg.impl.AttributeSGImpl;
34 import org.apache.ws.jaxme.generator.sg.impl.JaxMeSchemaReader;
35 import org.apache.ws.jaxme.generator.sg.impl.ObjectSGChainImpl;
36 import org.apache.ws.jaxme.generator.sg.impl.SGFactoryChainImpl;
37 import org.apache.ws.jaxme.js.JavaQName;
38 import org.apache.ws.jaxme.js.JavaQNameImpl;
39 import org.apache.ws.jaxme.js.JavaSource;
40 import org.apache.ws.jaxme.pm.ino.InoObject;
41 import org.apache.ws.jaxme.pm.ino.InoResponseHandler;
42 import org.apache.ws.jaxme.xs.XSAnnotation;
43 import org.apache.ws.jaxme.xs.XSAttribute;
44 import org.apache.ws.jaxme.xs.XSElement;
45 import org.apache.ws.jaxme.xs.XSObject;
46 import org.apache.ws.jaxme.xs.XSSchema;
47 import org.apache.ws.jaxme.xs.XSType;
48 import org.apache.ws.jaxme.xs.jaxb.JAXBAttribute;
49 import org.apache.ws.jaxme.xs.jaxb.JAXBGlobalBindings;
50 import org.apache.ws.jaxme.xs.jaxb.JAXBJavadoc;
51 import org.apache.ws.jaxme.xs.jaxb.JAXBProperty;
52 import org.apache.ws.jaxme.xs.jaxb.JAXBSchemaBindings;
53 import org.apache.ws.jaxme.xs.jaxb.JAXBType;
54 import org.apache.ws.jaxme.xs.parser.XsObjectCreator;
55 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
56 import org.apache.ws.jaxme.xs.types.XSString;
57 import org.apache.ws.jaxme.xs.xml.XsESchema;
58 import org.apache.ws.jaxme.xs.xml.XsObject;
59 import org.apache.ws.jaxme.xs.xml.XsObjectFactory;
60 import org.apache.ws.jaxme.xs.xml.XsQName;
61 import org.apache.ws.jaxme.xs.xml.impl.XsObjectImpl;
62 import org.xml.sax.Attributes JavaDoc;
63 import org.xml.sax.Locator JavaDoc;
64 import org.xml.sax.SAXException JavaDoc;
65 import org.xml.sax.helpers.AttributesImpl JavaDoc;
66
67 /**
68  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
69  */

70 public class TaminoAPI4JSG extends SGFactoryChainImpl {
71     public static final String JavaDoc TAMINOAPI4J_SCHEMA_URI = "http://ws.apache.org/jaxme/namespaces/jaxme2/TaminoAPI4J";
72     private static final JavaQName INO_OBJECT_TYPE = JavaQNameImpl.getInstance(InoObject.class);
73     private static final JavaQName JMELEMENT_TYPE = JavaQNameImpl.getInstance(JMElement.class);
74
75     public class RaDetails extends XsObjectImpl {
76         private String JavaDoc collection, jndiReference;
77         RaDetails(XsObject pParent) {
78             super(pParent);
79         }
80         public void setCollection(String JavaDoc pCollection) {
81             collection = pCollection;
82         }
83         public String JavaDoc getCollection() {
84             String JavaDoc p = "taminoapi4j.collection";
85             return sgFactory.getGenerator().getProperty(p, collection);
86         }
87         public void setJndiReference(String JavaDoc pReference) {
88             jndiReference = pReference;
89         }
90         public String JavaDoc getJndiReference() {
91             String JavaDoc p = "taminoapi4j.jndiReference";
92             return sgFactory.getGenerator().getProperty(p, jndiReference);
93         }
94     }
95
96     private RaDetails raDetails;
97     private SGFactory sgFactory;
98
99     public TaminoAPI4JSG(SGFactoryChain o) {
100         super(o);
101     }
102
103     public void setRaDetails(RaDetails pDetails) {
104         raDetails = pDetails;
105     }
106     public RaDetails getRaDetails() {
107         return raDetails;
108     }
109     
110     public void init(SGFactory pFactory) {
111         super.init(pFactory);
112         sgFactory = pFactory;
113         SchemaReader schemaReader = pFactory.getGenerator().getSchemaReader();
114         if (schemaReader instanceof JaxMeSchemaReader) {
115             JaxMeSchemaReader jaxmeSchemaReader = (JaxMeSchemaReader) schemaReader;
116             jaxmeSchemaReader.addXsObjectCreator(new XsObjectCreator(){
117                 public XsObject newBean(XsObject pParent, Locator JavaDoc pLocator, XsQName pQName) throws SAXException {
118                     if (pParent instanceof JAXBGlobalBindings) {
119                         if (TAMINOAPI4J_SCHEMA_URI.equals(pQName.getNamespaceURI())) {
120                             if ("raDetails".equals(pQName.getLocalName())) {
121                                 if (getRaDetails() != null) {
122                                     throw new LocSAXException("An element named " + pQName + " has already been specified.",
123                                                               pLocator);
124                                 }
125                                 RaDetails details = new RaDetails(pParent);
126                                 setRaDetails(details);
127                                 return details;
128                             } else {
129                                 throw new LocSAXException("Invalid element name: " + pQName, pLocator);
130                             }
131                         }
132                     }
133                     return null;
134                 }
135             });
136         } else {
137             throw new IllegalStateException JavaDoc("The schema reader " + schemaReader.getClass().getName() +
138                     " is not an instance of " + JaxMeSchemaReader.class.getName());
139         }
140     }
141
142     public Object JavaDoc newSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException {
143         SchemaSGChain chain = (SchemaSGChain) super.newSchemaSG(pController, pSchema);
144         chain = new TaminoAPI4JSchemaSG(chain, this);
145         return chain;
146     }
147
148     private class InoJAXBProperty implements JAXBProperty {
149         private final String JavaDoc name;
150         public InoJAXBProperty(String JavaDoc pName) {
151             name = pName;
152         }
153         public String JavaDoc getName() { return name; }
154         public String JavaDoc getCollectionType() { return null; }
155         public Boolean JavaDoc isFixedAttributeAsConstantProperty() { return null; }
156         public Boolean JavaDoc isGenerateIsSetMethod() { return null; }
157         public Boolean JavaDoc isEnableFailFastCheck() { return null; }
158         public JAXBJavadoc getJavadoc() { return null; }
159         public BaseType getBaseType() { return null; }
160         public XsESchema getXsESchema() { return null; }
161         public boolean isTopLevelObject() { return false; }
162         public XsObject getParentObject() { return null; }
163         public XsObjectFactory getObjectFactory() { return null; }
164         public Locator JavaDoc getLocator() { return null; }
165         public void validate() throws SAXException {
166         }
167     }
168
169     private class InoAttribute implements JAXBAttribute {
170         private final XsQName qName;
171         private final XSType type;
172         private final XSType parent;
173         private final JAXBProperty jaxbProperty;
174
175         public InoAttribute(XSType pParent, XsQName pName, XSType pType, String JavaDoc pPropertyName) {
176             qName = pName;
177             type = pType;
178             parent = pParent;
179             jaxbProperty = new InoJAXBProperty(pPropertyName);
180         }
181         public boolean isGlobal() { return true; }
182         public XsQName getName() { return qName; }
183         public XSType getType() { return type; }
184         public boolean isOptional() { return true; }
185         public XSAnnotation[] getAnnotations() { return new XSAnnotation[0]; }
186         public String JavaDoc getDefault() { return null; }
187         public String JavaDoc getFixed() { return null; }
188         public Attributes JavaDoc getOpenAttributes() { return new AttributesImpl JavaDoc(); }
189         public XSSchema getXSSchema() { return parent.getXSSchema(); }
190         public boolean isTopLevelObject() { return true; }
191         public XSObject getParentObject() { return null; }
192         public Locator JavaDoc getLocator() { return parent.getLocator(); }
193         public void validate() throws SAXException {
194         }
195         public JAXBSchemaBindings getJAXBSchemaBindings() {
196             return ((JAXBType) parent).getJAXBSchemaBindings();
197         }
198         public JAXBProperty getJAXBProperty() {
199             return jaxbProperty;
200         }
201     }
202
203     protected void addAttribute(TypeSG pComplexTypeSG, XSType pParent,
204                                 XsQName pName, XSType pType, String JavaDoc pPropertyName)
205             throws SAXException {
206         AttributeSG[] attrs = pComplexTypeSG.getComplexTypeSG().getAttributes();
207         for (int i = 0; i < attrs.length; i++) {
208             AttributeSG attr = attrs[i];
209             if (attr.isWildcard()) {
210                 continue;
211             }
212             XsQName qName = attr.getName();
213             if (qName.equals(pName)) {
214                 return;
215             }
216         }
217
218         XSAttribute attribute = new InoAttribute(pParent, pName, pType, pPropertyName);
219         AttributeSGChain chain = (AttributeSGChain) pComplexTypeSG.getComplexTypeSG().newAttributeSG(attribute);
220         AttributeSG attributeSG = new AttributeSGImpl(chain);
221         pComplexTypeSG.getComplexTypeSG().addAttributeSG(attributeSG);
222     }
223
224     public Object JavaDoc newObjectSG(SGFactory pFactory, XSElement pElement) throws SAXException {
225         ObjectSGChain chain = (ObjectSGChain) super.newObjectSG(pFactory, pElement);
226         chain = new ObjectSGChainImpl(chain){
227             public JavaSource getXMLImplementation(ObjectSG pController) throws SAXException {
228                 JavaSource result = super.getXMLImplementation(pController);
229                 JavaQName[] interfaces = result.getImplements();
230                 boolean done = false;
231                 result.clearImplements();
232                 for (int i = 0; i < interfaces.length; i++) {
233                     JavaQName interfaceName = interfaces[i];
234                     if (interfaceName.equals(JMELEMENT_TYPE) ||
235                         interfaceName.equals(INO_OBJECT_TYPE)) {
236                         if (!done) {
237                             result.addImplements(INO_OBJECT_TYPE);
238                             done = true;
239                         }
240                     } else {
241                         result.addImplements(interfaceName);
242                     }
243                 }
244                 if (!done) {
245                     result.addImplements(INO_OBJECT_TYPE);
246                 }
247                 return result;
248             }
249         };
250         return chain;
251     }
252
253     private Set JavaDoc elementNames = new HashSet JavaDoc();
254     public ObjectSG getObjectSG(SGFactory pFactory, XSElement pElement) throws SAXException {
255         ObjectSG result = super.getObjectSG(pFactory, pElement);
256         if (result.getTypeSG().isComplex()) {
257             XsQName elementName = pElement.getName();
258             if (!elementNames.contains(elementName)) {
259                 elementNames.add(elementName);
260                 // Does the element have an ino:id attribute? If not, create it
261
XsQName qName = new XsQName(InoResponseHandler.INO_RESPONSE2_URI, "id", "ino");
262                 XSType stringType = XSString.getInstance();
263                 addAttribute(result.getTypeSG(), pElement.getType(), qName, stringType, "inoId");
264                 // Does the element have an ino:docname attribute? If not, create it
265
qName = new XsQName(InoResponseHandler.INO_RESPONSE2_URI, "docname", "ino");
266                 addAttribute(result.getTypeSG(), pElement.getType(), qName, stringType, "inoDocname");
267             }
268         }
269         return result;
270     }
271 }
272
Popular Tags