KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25
26 import org.apache.ws.jaxme.generator.Generator;
27 import org.apache.ws.jaxme.generator.sg.Context;
28 import org.apache.ws.jaxme.generator.sg.Facet;
29 import org.apache.ws.jaxme.generator.sg.GroupSG;
30 import org.apache.ws.jaxme.generator.sg.ObjectSG;
31 import org.apache.ws.jaxme.generator.sg.ObjectSGChain;
32 import org.apache.ws.jaxme.generator.sg.SGFactory;
33 import org.apache.ws.jaxme.generator.sg.SGFactoryChain;
34 import org.apache.ws.jaxme.generator.sg.SchemaSG;
35 import org.apache.ws.jaxme.generator.sg.SchemaSGChain;
36 import org.apache.ws.jaxme.generator.sg.TypeSG;
37 import org.apache.ws.jaxme.generator.sg.TypeSGChain;
38 import org.apache.ws.jaxme.logging.Logger;
39 import org.apache.ws.jaxme.logging.LoggerAccess;
40 import org.apache.ws.jaxme.xs.XSAny;
41 import org.apache.ws.jaxme.xs.XSElement;
42 import org.apache.ws.jaxme.xs.XSEnumeration;
43 import org.apache.ws.jaxme.xs.XSGroup;
44 import org.apache.ws.jaxme.xs.XSObjectFactory;
45 import org.apache.ws.jaxme.xs.XSParser;
46 import org.apache.ws.jaxme.xs.XSSchema;
47 import org.apache.ws.jaxme.xs.XSType;
48 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
49 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBXsObjectFactoryImpl;
50 import org.apache.ws.jaxme.xs.parser.XSContext;
51 import org.apache.ws.jaxme.xs.xml.XsObjectFactory;
52 import org.apache.ws.jaxme.xs.xml.XsQName;
53 import org.xml.sax.EntityResolver JavaDoc;
54 import org.xml.sax.SAXException JavaDoc;
55 import org.xml.sax.XMLReader JavaDoc;
56
57
58
59 /**
60  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
61  */

62 public class JAXBSGFactory implements SGFactoryChain {
63     private final static Logger log = LoggerAccess.getLogger(JAXBSGFactory.class);
64     private final Generator generator;
65     private SchemaSG schemaSG;
66     private Map JavaDoc groups = new HashMap JavaDoc();
67     private Map JavaDoc objects = new HashMap JavaDoc();
68     private Map JavaDoc types = new HashMap JavaDoc();
69     private List JavaDoc groupsByOrder = new ArrayList JavaDoc();
70     private List JavaDoc objectsByOrder = new ArrayList JavaDoc();
71     private List JavaDoc typesByOrder = new ArrayList JavaDoc();
72     
73     /** Creates a new instance for the given {@link Generator}.
74      */

75     public JAXBSGFactory(Generator pGenerator) {
76         generator = pGenerator;
77     }
78     
79     public void init(SGFactory pController) {
80     }
81     
82     public Generator getGenerator(SGFactory pController) {
83         return generator;
84     }
85     
86     public GroupSG getGroupSG(SGFactory pController, XSGroup pGroup) throws SAXException JavaDoc {
87         GroupSG result = (GroupSG) groups.get(pGroup);
88         if (result == null) {
89             result = pController.newGroupSG(pGroup);
90             groups.put(pGroup, result);
91             groupsByOrder.add(result);
92             result.init();
93         }
94         return result;
95     }
96     
97     public GroupSG newGroupSG(SGFactory pController, XSGroup pGroup) throws SAXException JavaDoc {
98         if (schemaSG == null) {
99             throw new IllegalStateException JavaDoc("A schema has not yet been created");
100         }
101         return new GroupSGImpl(new JAXBGroupSG(pController, schemaSG, pGroup));
102     }
103     
104     public GroupSG getGroupSG(SGFactory pController, XSGroup pGroup, Context pClassContext) throws SAXException JavaDoc {
105         GroupSG result = pController.newGroupSG(pGroup, pClassContext);
106         result.init();
107         groupsByOrder.add(result);
108         return result;
109     }
110     
111     public GroupSG newGroupSG(SGFactory pController, XSGroup pGroup, Context pClassContext) throws SAXException JavaDoc {
112         if (schemaSG == null) {
113             throw new IllegalStateException JavaDoc("A schema has not yet been created");
114         }
115         return new GroupSGImpl(new JAXBGroupSG(pController, schemaSG, pGroup, pClassContext));
116     }
117     
118     
119     public Object JavaDoc newObjectSG(SGFactory pController, XSElement pElement) throws SAXException JavaDoc {
120         if (schemaSG == null) {
121             throw new IllegalStateException JavaDoc("A schema has not yet been created.");
122         }
123         return new JAXBObjectSG(pController, schemaSG, pElement);
124     }
125     
126     public ObjectSG getObjectSG(SGFactory pController, XSElement pElement) throws SAXException JavaDoc {
127         ObjectSG result = (ObjectSG) objects.get(pElement);
128         if (result == null) {
129             ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pElement);
130             result = new ObjectSGImpl(chain);
131             objects.put(pElement, result);
132             objectsByOrder.add(result);
133             result.init();
134         }
135         return result;
136     }
137
138     public Object JavaDoc newObjectSG(SGFactory pController, XSAny pAny) {
139         if (schemaSG == null) {
140             throw new IllegalStateException JavaDoc("A schema has not yet been created.");
141         }
142         return new JAXBObjectSG(pController, schemaSG, pAny);
143     }
144
145     public ObjectSG getObjectSG(SGFactory pController, XSAny pAny, Context pContext) throws SAXException JavaDoc {
146         ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pAny);
147         ObjectSG result = new ObjectSGImpl(chain);
148         result.init();
149         return result;
150     }
151
152     public Object JavaDoc newObjectSG(SGFactory pController, XSElement pElement, Context pContext) throws SAXException JavaDoc {
153         if (schemaSG == null) {
154             throw new IllegalStateException JavaDoc("A schema has not yet been created.");
155         }
156         return new JAXBObjectSG(pController, schemaSG, pElement, pContext);
157     }
158     
159     public ObjectSG getObjectSG(SGFactory pController, XSElement pElement, Context pContext) throws SAXException JavaDoc {
160         ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pElement, pContext);
161         ObjectSG result = new ObjectSGImpl(chain);
162         result.init();
163         return result;
164     }
165     
166     public SchemaSG getSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException JavaDoc {
167         if (schemaSG == null) {
168             SchemaSGChain chain = (SchemaSGChain) pController.newSchemaSG(pSchema);
169             schemaSG = new SchemaSGImpl(chain);
170             schemaSG.init();
171         }
172         return schemaSG;
173     }
174     
175     public SchemaSG getSchemaSG(SGFactory pController) {
176         if (schemaSG == null) {
177             throw new IllegalStateException JavaDoc("A factory has not yet been created.");
178         }
179         return schemaSG;
180     }
181     
182     public Object JavaDoc newSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException JavaDoc {
183         return new JAXBSchemaSG(pController, pSchema);
184     }
185     
186     public TypeSG getTypeSG(SGFactory pController, XSType pType) throws SAXException JavaDoc {
187         final String JavaDoc mName = "getTypeSG(XSType)";
188         TypeSG result = (TypeSG) types.get(pType);
189         if (result == null) {
190             log.finest(mName, "->", pType.getName());
191             TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType);
192             result = new TypeSGImpl(chain);
193             types.put(pType, result);
194             typesByOrder.add(result);
195             result.init();
196             log.finest(mName, "<-", new Object JavaDoc[]{chain, result});
197         }
198         return result;
199     }
200     
201     public Object JavaDoc newTypeSG(SGFactory pController, XSType pType) throws SAXException JavaDoc {
202         if (schemaSG == null) {
203             throw new IllegalStateException JavaDoc("A schema has not yet been created");
204         }
205         return new JAXBTypeSG(pController, schemaSG, pType);
206     }
207     
208     public TypeSG getTypeSG(SGFactory pController, XSType pType, Context pClassContext, XsQName pName) throws SAXException JavaDoc {
209         final String JavaDoc mName = "getTypeSG(XSType,ClassContext)";
210         log.finest(mName, "->", new Object JavaDoc[]{pType, pClassContext});
211         TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType, pClassContext, pName);
212         TypeSG result = new TypeSGImpl(chain);
213         typesByOrder.add(result);
214         result.init();
215         log.finest(mName, "<-", new Object JavaDoc[]{chain, result});
216         return result;
217     }
218     
219     public Object JavaDoc newTypeSG(SGFactory pController, XSType pType, Context pClassContext, XsQName pName) throws SAXException JavaDoc {
220         if (schemaSG == null) {
221             throw new IllegalStateException JavaDoc("A schema has not yet been created");
222         }
223         return new JAXBTypeSG(pController, schemaSG, pType, pClassContext, pName);
224     }
225     
226     public Object JavaDoc newTypeSG(SGFactory pController, XSType pType, XsQName pName) throws SAXException JavaDoc {
227         if (schemaSG == null) {
228             throw new IllegalStateException JavaDoc("A schema has not yet been created");
229         }
230         return new JAXBTypeSG(pController, schemaSG, pType, pName);
231     }
232     
233     public TypeSG getTypeSG(SGFactory pController, XSType pType, XsQName pName) throws SAXException JavaDoc {
234         TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType, pName);
235         TypeSG result = new TypeSGImpl(chain);
236         typesByOrder.add(result);
237         result.init();
238         return result;
239     }
240     
241     public XSParser newXSParser(SGFactory pController) throws SAXException JavaDoc {
242         XSParser parser = new JAXBParser();
243         XSContext context = parser.getContext();
244         context.setXsObjectFactory(pController.newXsObjectFactory());
245         context.setXSObjectFactory(pController.newXSObjectFactory());
246         return parser;
247     }
248     
249     public Facet newFacet(SGFactory pController, XSType pType, XSEnumeration[] pEnumerations) throws SAXException JavaDoc {
250         return new FacetImpl(pType, pEnumerations);
251     }
252     
253     public TypeSG[] getTypes(SGFactory pController) {
254         return (TypeSG[]) typesByOrder.toArray(new TypeSG[typesByOrder.size()]);
255     }
256     
257     public GroupSG[] getGroups(SGFactory pController) {
258         return (GroupSG[]) groupsByOrder.toArray(new GroupSG[groupsByOrder.size()]);
259     }
260     
261     public ObjectSG[] getObjects(SGFactory pController) {
262         return (ObjectSG[]) objectsByOrder.toArray(new ObjectSG[objectsByOrder.size()]);
263     }
264     
265     public XsObjectFactory newXsObjectFactory(SGFactory pController) throws SAXException JavaDoc {
266         return new JAXBXsObjectFactoryImpl(){
267             public XMLReader JavaDoc newXMLReader(boolean pValidating) throws ParserConfigurationException JavaDoc, SAXException JavaDoc{
268                 XMLReader JavaDoc xr = super.newXMLReader(pValidating);
269                 EntityResolver JavaDoc entityResolver = generator.getEntityResolver();
270                 if (entityResolver != null) {
271                     xr.setEntityResolver(entityResolver);
272                 }
273                 return xr;
274             }
275         };
276     }
277     
278     public XSObjectFactory newXSObjectFactory(SGFactory pController) throws SAXException JavaDoc {
279         return JAXBParser.JAXB_OBJECT_FACTORY;
280     }
281 }
282
Popular Tags