root / trunk / libraries / libGPE / src / org / gvsig / xmlschema / som / impl / XSSchemaImpl.java @ 12439
History | View | Annotate | Download (12.1 KB)
1 | 12175 | jorpiell | package org.gvsig.xmlschema.som.impl; |
---|---|---|---|
2 | |||
3 | import java.io.OutputStream; |
||
4 | import java.util.Collection; |
||
5 | import java.util.Hashtable; |
||
6 | import java.util.Iterator; |
||
7 | import java.util.Map; |
||
8 | |||
9 | 12391 | jorpiell | import javax.xml.namespace.QName; |
10 | 12175 | jorpiell | import javax.xml.transform.Transformer; |
11 | import javax.xml.transform.TransformerConfigurationException; |
||
12 | import javax.xml.transform.TransformerException; |
||
13 | import javax.xml.transform.TransformerFactory; |
||
14 | import javax.xml.transform.dom.DOMSource; |
||
15 | import javax.xml.transform.stream.StreamResult; |
||
16 | |||
17 | import org.gvsig.xmlschema.exceptions.SchemaWrittingException; |
||
18 | import org.gvsig.xmlschema.som.IXSComplexTypeDefinition; |
||
19 | import org.gvsig.xmlschema.som.IXSElementDeclaration; |
||
20 | import org.gvsig.xmlschema.som.IXSNode; |
||
21 | import org.gvsig.xmlschema.som.IXSSchema; |
||
22 | import org.gvsig.xmlschema.som.IXSTypeDefinition; |
||
23 | import org.gvsig.xmlschema.utils.DOMObjectsFactory; |
||
24 | import org.gvsig.xmlschema.utils.SchemaCollection; |
||
25 | import org.gvsig.xmlschema.utils.SchemaDefaults; |
||
26 | import org.gvsig.xmlschema.utils.SchemaObjectsMapping; |
||
27 | import org.gvsig.xmlschema.utils.SchemaTags; |
||
28 | import org.gvsig.xmlschema.utils.SchemaUtils; |
||
29 | import org.w3c.dom.Document; |
||
30 | import org.w3c.dom.Element; |
||
31 | 12439 | jorpiell | import org.w3c.dom.NamedNodeMap; |
32 | import org.w3c.dom.Node; |
||
33 | 12175 | jorpiell | |
34 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
35 | *
|
||
36 | * Copyright (C) 2 IVER T.I. and Generalitat Valenciana.
|
||
37 | *
|
||
38 | * This program is free software; you can redistribute it and/or
|
||
39 | * modify it under the terms of the GNU General Public License
|
||
40 | * as published by the Free Software Foundation; either version 2
|
||
41 | * of the License, or (at your option) any later version.
|
||
42 | *
|
||
43 | * This program is distributed in the hope that it will be useful,
|
||
44 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
45 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
46 | * GNU General Public License for more details.
|
||
47 | *
|
||
48 | * You should have received a copy of the GNU General Public License
|
||
49 | * along with this program; if not, write to the Free Software
|
||
50 | * Foundation, Inc., 9 Temple Place - Suite , Boston, MA 2111-1,USA.
|
||
51 | *
|
||
52 | * For more information, contact:
|
||
53 | *
|
||
54 | * Generalitat Valenciana
|
||
55 | * Conselleria d'Infraestructures i Transport
|
||
56 | * Av. Blasco Ib??ez,
|
||
57 | * 1 VALENCIA
|
||
58 | * SPAIN
|
||
59 | *
|
||
60 | * +
|
||
61 | * gvsig@gva.es
|
||
62 | * www.gvsig.gva.es
|
||
63 | *
|
||
64 | * or
|
||
65 | *
|
||
66 | * IVER T.I. S.A
|
||
67 | * Salamanca
|
||
68 | * Valencia
|
||
69 | * Spain
|
||
70 | *
|
||
71 | * + 1
|
||
72 | * dac@iver.es
|
||
73 | */
|
||
74 | /* CVS MESSAGES:
|
||
75 | *
|
||
76 | * $Id$
|
||
77 | * $Log$
|
||
78 | 12439 | jorpiell | * Revision 1.4 2007-07-02 09:57:35 jorpiell
|
79 | * The generated xsd schemas have to be valid
|
||
80 | *
|
||
81 | * Revision 1.3 2007/06/28 13:04:33 jorpiell
|
||
82 | 12391 | jorpiell | * The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
|
83 | *
|
||
84 | * Revision 1.2 2007/06/22 12:20:48 jorpiell
|
||
85 | 12311 | jorpiell | * The typeNotFoundException has been deleted. It never was thrown
|
86 | *
|
||
87 | * Revision 1.1 2007/06/14 16:15:03 jorpiell
|
||
88 | 12175 | jorpiell | * builds to create the jars generated and add the schema code to the libGPEProject
|
89 | *
|
||
90 | * Revision 1.1 2007/06/14 13:50:07 jorpiell
|
||
91 | * The schema jar name has been changed
|
||
92 | *
|
||
93 | * Revision 1.9 2007/06/08 11:35:16 jorpiell
|
||
94 | * IXSSchema interface updated
|
||
95 | *
|
||
96 | * Revision 1.8 2007/06/08 07:31:20 jorpiell
|
||
97 | * Add the euroRoadS test
|
||
98 | *
|
||
99 | * Revision 1.7 2007/06/08 06:55:05 jorpiell
|
||
100 | * Fixed some bugs
|
||
101 | *
|
||
102 | * Revision 1.6 2007/06/07 14:54:13 jorpiell
|
||
103 | * Add the schema support
|
||
104 | *
|
||
105 | * Revision 1.5 2007/05/30 12:53:33 jorpiell
|
||
106 | * Not used libraries deleted
|
||
107 | *
|
||
108 | * Revision 1.4 2007/05/30 12:50:53 jorpiell
|
||
109 | * Refactoring of some duplicated methods
|
||
110 | *
|
||
111 | * Revision 1.3 2007/05/30 12:25:48 jorpiell
|
||
112 | * Add the element collection
|
||
113 | *
|
||
114 | * Revision 1.2 2007/05/28 12:38:03 jorpiell
|
||
115 | * Some bugs fixed
|
||
116 | *
|
||
117 | * Revision 1.1 2007/05/25 11:55:00 jorpiell
|
||
118 | * First update
|
||
119 | *
|
||
120 | *
|
||
121 | */
|
||
122 | /**
|
||
123 | * @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
||
124 | */
|
||
125 | public class XSSchemaImpl implements IXSSchema{ |
||
126 | //XML DOM objects
|
||
127 | private Document document = null; |
||
128 | private Element element = null; |
||
129 | //The target namespace
|
||
130 | private String targetNamespace = null; |
||
131 | //Mappings to save objects
|
||
132 | private Map elements = null; |
||
133 | private Map types = null; |
||
134 | //XML DOM objects factory
|
||
135 | private DOMObjectsFactory objectsFactory = null; |
||
136 | //All the XML schema object mapping
|
||
137 | private SchemaObjectsMapping objectsMapping = null; |
||
138 | |||
139 | 12311 | jorpiell | public XSSchemaImpl(Document document){ |
140 | 12175 | jorpiell | super();
|
141 | this.document = document;
|
||
142 | this.element = document.getDocumentElement();
|
||
143 | targetNamespace = element.getAttribute(SchemaTags.TARGET_NAMESPACE).trim(); |
||
144 | elements = new Hashtable(); |
||
145 | types = new Hashtable(); |
||
146 | initializeMapping(); |
||
147 | } |
||
148 | |||
149 | /**
|
||
150 | * It initializes the mapping with the supported mappings
|
||
151 | * @throws TypeNotFoundException
|
||
152 | */
|
||
153 | 12311 | jorpiell | private void initializeMapping(){ |
154 | 12175 | jorpiell | objectsMapping = new SchemaObjectsMapping(this); |
155 | objectsMapping.addType(SchemaTags.SIMPLE_TYPE, XSSimpleTypeDefinitionImpl.class); |
||
156 | objectsMapping.addType(SchemaTags.COMPLEX_TYPE, XSComplexTypeDefinitionImpl.class); |
||
157 | objectsMapping.addType(SchemaTags.ELEMENT, XSElementDeclarationImpl.class); |
||
158 | objectsMapping.addType(SchemaTags.CHOICE, XSChoiceImpl.class); |
||
159 | objectsMapping.addType(SchemaTags.SEQUENCE, XSSequenceImpl.class); |
||
160 | objectsMapping.addType(SchemaTags.GROUP, XSGroupImpl.class); |
||
161 | objectsMapping.addType(SchemaTags.ALL, XSAllImpl.class); |
||
162 | objectsMapping.addType(SchemaTags.EXTENSION, XSExtensionImpl.class); |
||
163 | objectsMapping.addType(SchemaTags.RESTRICTION, XSRestrictionImpl.class); |
||
164 | objectsMapping.addType(SchemaTags.SIMPLE_CONTENT, XSRestrictionImpl.class); |
||
165 | objectsMapping.addType(SchemaTags.COMPLEX_CONTENT, XSComplexContentImpl.class); |
||
166 | } |
||
167 | |||
168 | /*
|
||
169 | * (non-Javadoc)
|
||
170 | * @see org.gvsig.gpe.schema.som.IXSSchema#getDocument()
|
||
171 | */
|
||
172 | public Document getDocument() { |
||
173 | return document;
|
||
174 | } |
||
175 | |||
176 | /*
|
||
177 | * (non-Javadoc)
|
||
178 | * @see org.gvsig.gpe.schema.som.IXSSchema#getElementDeclarations()
|
||
179 | */
|
||
180 | 12311 | jorpiell | public Collection getElementDeclarations(){ |
181 | 12175 | jorpiell | return new SchemaCollection(this,element,getElementMapping()); |
182 | } |
||
183 | |||
184 | /*
|
||
185 | * (non-Javadoc)
|
||
186 | * @see org.gvsig.gpe.schema.som.IXSSchema#getElementDeclarationByName(java.lang.String, java.lang.String)
|
||
187 | */
|
||
188 | 12311 | jorpiell | public IXSElementDeclaration getElementDeclarationByName(String elementURI, String elementName){ |
189 | 12175 | jorpiell | elementName = SchemaUtils.getLocalName(elementName); |
190 | Object obj = elements.get(elementName);
|
||
191 | if (obj != null){ |
||
192 | return (IXSElementDeclaration)obj;
|
||
193 | } |
||
194 | IXSNode element = getObjectByName(elementURI, elementName,getElementMapping()); |
||
195 | if (element != null){ |
||
196 | elements.put(elementName, element); |
||
197 | return (IXSElementDeclaration)element;
|
||
198 | } |
||
199 | return null; |
||
200 | } |
||
201 | |||
202 | |||
203 | /**
|
||
204 | * Search a Element by name in the Dom tree and returns the
|
||
205 | * IXSNode that contains it
|
||
206 | * @param targetNamespace
|
||
207 | * Element targetNamespace
|
||
208 | * @param elementName
|
||
209 | * Element local name
|
||
210 | * @param typesMapping
|
||
211 | * Mapping to create the IXSNode
|
||
212 | * @return
|
||
213 | * the IXSNode
|
||
214 | */
|
||
215 | private IXSNode getObjectByName(String targetNamespace, String elementName, SchemaObjectsMapping typesMapping){ |
||
216 | QName qname = new QName(targetNamespace,elementName); |
||
217 | Iterator it = typesMapping.getTypes().iterator();
|
||
218 | while (it.hasNext()){
|
||
219 | String type = (String)it.next(); |
||
220 | Element childElement = SchemaUtils.searchChildByAttributeName(element, type, targetNamespace, elementName);
|
||
221 | if (childElement != null){ |
||
222 | QName newQname = new QName(getTargetNamespace(),childElement.getAttribute(SchemaTags.NAME)); |
||
223 | if (SchemaUtils.matches(qname,newQname)){
|
||
224 | return typesMapping.getNode(type,childElement);
|
||
225 | } |
||
226 | } |
||
227 | |||
228 | } |
||
229 | return null; |
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | * @return The elements mapping
|
||
234 | * @throws TypeNotFoundException
|
||
235 | */
|
||
236 | 12311 | jorpiell | private SchemaObjectsMapping getElementMapping(){
|
237 | 12175 | jorpiell | SchemaObjectsMapping objectsTypeMapping = new SchemaObjectsMapping(this); |
238 | objectsTypeMapping.addType(SchemaTags.ELEMENT, XSElementDeclarationImpl.class); |
||
239 | return objectsTypeMapping;
|
||
240 | } |
||
241 | |||
242 | /*
|
||
243 | * (non-Javadoc)
|
||
244 | * @see org.gvsig.gpe.schema.som.IXSSchema#getTypeDefinitions()
|
||
245 | */
|
||
246 | 12311 | jorpiell | public Collection getTypeDefinitions(){ |
247 | 12175 | jorpiell | return new SchemaCollection(this,element,getTypeMapping()); |
248 | } |
||
249 | |||
250 | /*
|
||
251 | * (non-Javadoc)
|
||
252 | * @see org.gvsig.gpe.schema.som.IXSSchema#getTypeByName(java.lang.String, java.lang.String)
|
||
253 | */
|
||
254 | 12311 | jorpiell | public IXSTypeDefinition getTypeByName(String typeURI, String typeName){ |
255 | 12175 | jorpiell | typeName = SchemaUtils.getLocalName(typeName); |
256 | Object obj = types.get(typeName);
|
||
257 | if (obj != null){ |
||
258 | return (IXSTypeDefinition)obj;
|
||
259 | } |
||
260 | IXSNode type = getObjectByName(typeURI, typeName, getTypeMapping()); |
||
261 | if (type != null){ |
||
262 | types.put(typeName, type); |
||
263 | return (IXSTypeDefinition)type;
|
||
264 | } |
||
265 | return null; |
||
266 | } |
||
267 | |||
268 | /**
|
||
269 | * @return The complex type mapping
|
||
270 | * @throws TypeNotFoundException
|
||
271 | */
|
||
272 | 12311 | jorpiell | private SchemaObjectsMapping getTypeMapping(){
|
273 | 12175 | jorpiell | SchemaObjectsMapping objectsTypeMapping = new SchemaObjectsMapping(this); |
274 | objectsTypeMapping.addType(SchemaTags.COMPLEX_TYPE, XSComplexTypeDefinitionImpl.class); |
||
275 | objectsTypeMapping.addType(SchemaTags.SIMPLE_TYPE, XSSimpleTypeDefinitionImpl.class); |
||
276 | return objectsTypeMapping;
|
||
277 | } |
||
278 | |||
279 | |||
280 | /*
|
||
281 | * (non-Javadoc)
|
||
282 | * @see org.gvsig.gpe.schema.som.IXSSchema#write(java.io.OutputStream)
|
||
283 | */
|
||
284 | public void write(OutputStream os) throws SchemaWrittingException { |
||
285 | TransformerFactory tFactory = TransformerFactory.newInstance(); |
||
286 | Transformer transformer;
|
||
287 | try {
|
||
288 | transformer = tFactory.newTransformer(); |
||
289 | DOMSource source = new DOMSource(document); |
||
290 | StreamResult result = new StreamResult(os); |
||
291 | transformer.transform(source, result); |
||
292 | } catch (TransformerConfigurationException e) { |
||
293 | throw new SchemaWrittingException(e); |
||
294 | } catch (TransformerException e) { |
||
295 | throw new SchemaWrittingException(e); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /*
|
||
300 | * (non-Javadoc)
|
||
301 | * @see org.gvsig.gpe.schema.som.IXSSchema#getNamespaceURI()
|
||
302 | */
|
||
303 | public String getTargetNamespace() { |
||
304 | return targetNamespace;
|
||
305 | } |
||
306 | |||
307 | 12439 | jorpiell | /*
|
308 | * (non-Javadoc)
|
||
309 | * @see org.gvsig.xmlschema.som.IXSSchema#getTargetNamespacePrefix()
|
||
310 | */
|
||
311 | public String getTargetNamespacePrefix() { |
||
312 | return getNamespacePrefix(targetNamespace);
|
||
313 | } |
||
314 | |||
315 | /*
|
||
316 | * (non-Javadoc)
|
||
317 | * @see org.gvsig.xmlschema.som.IXSSchema#getNamespacePrefix(java.lang.String)
|
||
318 | */
|
||
319 | public String getNamespacePrefix(String namespaceURI) { |
||
320 | if (namespaceURI != null){ |
||
321 | NamedNodeMap attributes = element.getAttributes(); |
||
322 | for (int i=0 ; i<attributes.getLength() ; i++){ |
||
323 | Node node = attributes.item(i); |
||
324 | String[] name = node.getNodeName().split(":"); |
||
325 | if ((name.length == 2) && (name[0].equals(SchemaTags.XMLNS_NS))){ |
||
326 | if (node.getNodeValue().equals(namespaceURI)){
|
||
327 | return name[1]; |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | return null; |
||
333 | } |
||
334 | |||
335 | 12175 | jorpiell | /**
|
336 | * Add a new child element
|
||
337 | * @param childElement
|
||
338 | */
|
||
339 | private void addChildElement(Element childElement) { |
||
340 | element.appendChild(childElement); |
||
341 | } |
||
342 | |||
343 | /*
|
||
344 | * (non-Javadoc)
|
||
345 | 12439 | jorpiell | * @see org.gvsig.gpe.schema.som.IXSSchema#addElement(java.lang.String, java.lang.String, java.lang.String)
|
346 | 12175 | jorpiell | */
|
347 | 12439 | jorpiell | public IXSElementDeclaration addElement(String name, String type, String substitutionGroup) { |
348 | String typeName = getTargetNamespacePrefix();
|
||
349 | if (typeName != null){ |
||
350 | type = typeName + ":" + type;
|
||
351 | } |
||
352 | 12175 | jorpiell | Element element = getObjectsFactory().createElement(
|
353 | this,
|
||
354 | name, |
||
355 | type, |
||
356 | 12439 | jorpiell | substitutionGroup); |
357 | 12175 | jorpiell | addChildElement(element); |
358 | XSElementDeclarationImpl xsElement = new XSElementDeclarationImpl(this); |
||
359 | xsElement.setElement(element); |
||
360 | return xsElement;
|
||
361 | } |
||
362 | 12439 | jorpiell | |
363 | 12175 | jorpiell | /*
|
364 | * (non-Javadoc)
|
||
365 | * @see org.gvsig.gpe.schema.som.IXSSchema#addElement(java.lang.String, java.lang.String)
|
||
366 | */
|
||
367 | public IXSElementDeclaration addElement(String name, String type) { |
||
368 | 12439 | jorpiell | return addElement(name, type, null); |
369 | } |
||
370 | 12175 | jorpiell | |
371 | /*
|
||
372 | * (non-Javadoc)
|
||
373 | * @see org.gvsig.gpe.schema.som.IXSSchema#addComplexType(java.lang.String)
|
||
374 | */
|
||
375 | public IXSComplexTypeDefinition addComplexType(String name, String type, |
||
376 | String contentType, String conteTypeRestriction) { |
||
377 | Element eComplexType = getObjectsFactory().createComplexType(
|
||
378 | this,
|
||
379 | name, |
||
380 | type, |
||
381 | contentType, |
||
382 | conteTypeRestriction); |
||
383 | addChildElement(eComplexType); |
||
384 | XSComplexTypeDefinitionImpl complexType = new XSComplexTypeDefinitionImpl(this); |
||
385 | complexType.setElement(eComplexType); |
||
386 | return complexType;
|
||
387 | } |
||
388 | |||
389 | /**
|
||
390 | * @return the DOM objects factory
|
||
391 | */
|
||
392 | private DOMObjectsFactory getObjectsFactory() {
|
||
393 | if (objectsFactory == null){ |
||
394 | objectsFactory = DOMObjectsFactory.getInstance(); |
||
395 | } |
||
396 | return objectsFactory;
|
||
397 | } |
||
398 | |||
399 | /*
|
||
400 | * (non-Javadoc)
|
||
401 | * @see org.gvsig.gpe.schema.som.IXSSchema#getTypesMapping()
|
||
402 | */
|
||
403 | public SchemaObjectsMapping getObjectsMapping() {
|
||
404 | return objectsMapping;
|
||
405 | } |
||
406 | } |
||
407 |