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