svn-gvsig-desktop / trunk / libraries / libGPE / src / org / gvsig / xmlschema / som / impl / XSSchemaImpl.java @ 18770
History | View | Annotate | Download (12.2 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 org.gvsig.xmlschema.utils.QName; |
10 |
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.SchemaObjectsMapping; |
26 |
import org.gvsig.xmlschema.utils.SchemaTags; |
27 |
import org.gvsig.xmlschema.utils.SchemaUtils; |
28 |
import org.w3c.dom.Document; |
29 |
import org.w3c.dom.Element; |
30 |
import org.w3c.dom.NamedNodeMap; |
31 |
import org.w3c.dom.Node; |
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 164 2007-07-02 10:00:46Z jorpiell $
|
76 |
* $Log$
|
77 |
* Revision 1.4 2007/07/02 09:57:35 jorpiell
|
78 |
* The generated xsd schemas have to be valid
|
79 |
*
|
80 |
* Revision 1.3 2007/06/28 13:04:33 jorpiell
|
81 |
* The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
|
82 |
*
|
83 |
* Revision 1.2 2007/06/22 12:20:48 jorpiell
|
84 |
* The typeNotFoundException has been deleted. It never was thrown
|
85 |
*
|
86 |
* Revision 1.1 2007/06/14 16:15:03 jorpiell
|
87 |
* builds to create the jars generated and add the schema code to the libGPEProject
|
88 |
*
|
89 |
* Revision 1.1 2007/06/14 13:50:07 jorpiell
|
90 |
* The schema jar name has been changed
|
91 |
*
|
92 |
* Revision 1.9 2007/06/08 11:35:16 jorpiell
|
93 |
* IXSSchema interface updated
|
94 |
*
|
95 |
* Revision 1.8 2007/06/08 07:31:20 jorpiell
|
96 |
* Add the euroRoadS test
|
97 |
*
|
98 |
* Revision 1.7 2007/06/08 06:55:05 jorpiell
|
99 |
* Fixed some bugs
|
100 |
*
|
101 |
* Revision 1.6 2007/06/07 14:54:13 jorpiell
|
102 |
* Add the schema support
|
103 |
*
|
104 |
* Revision 1.5 2007/05/30 12:53:33 jorpiell
|
105 |
* Not used libraries deleted
|
106 |
*
|
107 |
* Revision 1.4 2007/05/30 12:50:53 jorpiell
|
108 |
* Refactoring of some duplicated methods
|
109 |
*
|
110 |
* Revision 1.3 2007/05/30 12:25:48 jorpiell
|
111 |
* Add the element collection
|
112 |
*
|
113 |
* Revision 1.2 2007/05/28 12:38:03 jorpiell
|
114 |
* Some bugs fixed
|
115 |
*
|
116 |
* Revision 1.1 2007/05/25 11:55:00 jorpiell
|
117 |
* First update
|
118 |
*
|
119 |
*
|
120 |
*/
|
121 |
/**
|
122 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
123 |
*/
|
124 |
public class XSSchemaImpl implements IXSSchema{ |
125 |
//XML DOM objects
|
126 |
private Document document = null; |
127 |
private Element element = null; |
128 |
//The target namespace
|
129 |
private String targetNamespace = null; |
130 |
//Mappings to save objects
|
131 |
private Map elements = null; |
132 |
private Map types = null; |
133 |
//XML DOM objects factory
|
134 |
private DOMObjectsFactory objectsFactory = null; |
135 |
//All the XML schema object mapping
|
136 |
private SchemaObjectsMapping objectsMapping = null; |
137 |
|
138 |
public XSSchemaImpl(Document document){ |
139 |
super();
|
140 |
this.document = document;
|
141 |
this.element = document.getDocumentElement();
|
142 |
targetNamespace = element.getAttribute(SchemaTags.TARGET_NAMESPACE).trim(); |
143 |
elements = new Hashtable(); |
144 |
types = new Hashtable(); |
145 |
initializeMapping(); |
146 |
} |
147 |
|
148 |
/**
|
149 |
* It initializes the mapping with the supported mappings
|
150 |
* @throws TypeNotFoundException
|
151 |
*/
|
152 |
private void initializeMapping(){ |
153 |
objectsMapping = new SchemaObjectsMapping(this); |
154 |
objectsMapping.addType(SchemaTags.SIMPLE_TYPE, XSSimpleTypeDefinitionImpl.class); |
155 |
objectsMapping.addType(SchemaTags.COMPLEX_TYPE, XSComplexTypeDefinitionImpl.class); |
156 |
objectsMapping.addType(SchemaTags.ELEMENT, XSElementDeclarationImpl.class); |
157 |
objectsMapping.addType(SchemaTags.CHOICE, XSChoiceImpl.class); |
158 |
objectsMapping.addType(SchemaTags.SEQUENCE, XSSequenceImpl.class); |
159 |
objectsMapping.addType(SchemaTags.GROUP, XSGroupImpl.class); |
160 |
objectsMapping.addType(SchemaTags.ALL, XSAllImpl.class); |
161 |
objectsMapping.addType(SchemaTags.EXTENSION, XSExtensionImpl.class); |
162 |
objectsMapping.addType(SchemaTags.RESTRICTION, XSRestrictionImpl.class); |
163 |
objectsMapping.addType(SchemaTags.SIMPLE_CONTENT, XSRestrictionImpl.class); |
164 |
objectsMapping.addType(SchemaTags.COMPLEX_CONTENT, XSComplexContentImpl.class); |
165 |
} |
166 |
|
167 |
/*
|
168 |
* (non-Javadoc)
|
169 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getDocument()
|
170 |
*/
|
171 |
public Document getDocument() { |
172 |
return document;
|
173 |
} |
174 |
|
175 |
/*
|
176 |
* (non-Javadoc)
|
177 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getElementDeclarations()
|
178 |
*/
|
179 |
public Collection getElementDeclarations(){ |
180 |
return new SchemaCollection(this,element,getElementMapping()); |
181 |
} |
182 |
|
183 |
/*
|
184 |
* (non-Javadoc)
|
185 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getElementDeclarationByName(java.lang.String, java.lang.String)
|
186 |
*/
|
187 |
public IXSElementDeclaration getElementDeclarationByName(String elementURI, String elementName){ |
188 |
elementName = SchemaUtils.getLocalName(elementName); |
189 |
Object obj = elements.get(elementName);
|
190 |
if (obj != null){ |
191 |
return (IXSElementDeclaration)obj;
|
192 |
} |
193 |
IXSNode element = getObjectByName(elementURI, elementName,getElementMapping()); |
194 |
if (element != null){ |
195 |
elements.put(elementName, element); |
196 |
return (IXSElementDeclaration)element;
|
197 |
} |
198 |
return null; |
199 |
} |
200 |
|
201 |
|
202 |
/**
|
203 |
* Search a Element by name in the Dom tree and returns the
|
204 |
* IXSNode that contains it
|
205 |
* @param targetNamespace
|
206 |
* Element targetNamespace
|
207 |
* @param elementName
|
208 |
* Element local name
|
209 |
* @param typesMapping
|
210 |
* Mapping to create the IXSNode
|
211 |
* @return
|
212 |
* the IXSNode
|
213 |
*/
|
214 |
private IXSNode getObjectByName(String targetNamespace, String elementName, SchemaObjectsMapping typesMapping){ |
215 |
QName qname = new QName(targetNamespace,elementName); |
216 |
Iterator it = typesMapping.getTypes().iterator();
|
217 |
while (it.hasNext()){
|
218 |
String type = (String)it.next(); |
219 |
Element childElement = SchemaUtils.searchChildByAttributeName(element, type, targetNamespace, elementName);
|
220 |
if (childElement != null){ |
221 |
QName newQname = new QName(getTargetNamespace(),childElement.getAttribute(SchemaTags.NAME)); |
222 |
if (SchemaUtils.matches(qname,newQname)){
|
223 |
return typesMapping.getNode(type,childElement);
|
224 |
} |
225 |
} |
226 |
|
227 |
} |
228 |
return null; |
229 |
} |
230 |
|
231 |
/**
|
232 |
* @return The elements mapping
|
233 |
* @throws TypeNotFoundException
|
234 |
*/
|
235 |
private SchemaObjectsMapping getElementMapping(){
|
236 |
SchemaObjectsMapping objectsTypeMapping = new SchemaObjectsMapping(this); |
237 |
objectsTypeMapping.addType(SchemaTags.ELEMENT, XSElementDeclarationImpl.class); |
238 |
return objectsTypeMapping;
|
239 |
} |
240 |
|
241 |
/*
|
242 |
* (non-Javadoc)
|
243 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getTypeDefinitions()
|
244 |
*/
|
245 |
public Collection getTypeDefinitions(){ |
246 |
return new SchemaCollection(this,element,getTypeMapping()); |
247 |
} |
248 |
|
249 |
/*
|
250 |
* (non-Javadoc)
|
251 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getTypeByName(java.lang.String, java.lang.String)
|
252 |
*/
|
253 |
public IXSTypeDefinition getTypeByName(String typeURI, String typeName){ |
254 |
typeName = SchemaUtils.getLocalName(typeName); |
255 |
Object obj = types.get(typeName);
|
256 |
if (obj != null){ |
257 |
return (IXSTypeDefinition)obj;
|
258 |
} |
259 |
IXSNode type = getObjectByName(typeURI, typeName, getTypeMapping()); |
260 |
if (type != null){ |
261 |
types.put(typeName, type); |
262 |
return (IXSTypeDefinition)type;
|
263 |
} |
264 |
return null; |
265 |
} |
266 |
|
267 |
/**
|
268 |
* @return The complex type mapping
|
269 |
* @throws TypeNotFoundException
|
270 |
*/
|
271 |
private SchemaObjectsMapping getTypeMapping(){
|
272 |
SchemaObjectsMapping objectsTypeMapping = new SchemaObjectsMapping(this); |
273 |
objectsTypeMapping.addType(SchemaTags.COMPLEX_TYPE, XSComplexTypeDefinitionImpl.class); |
274 |
objectsTypeMapping.addType(SchemaTags.SIMPLE_TYPE, XSSimpleTypeDefinitionImpl.class); |
275 |
return objectsTypeMapping;
|
276 |
} |
277 |
|
278 |
|
279 |
/*
|
280 |
* (non-Javadoc)
|
281 |
* @see org.gvsig.gpe.schema.som.IXSSchema#write(java.io.OutputStream)
|
282 |
*/
|
283 |
public void write(OutputStream os) throws SchemaWrittingException { |
284 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
285 |
Transformer transformer;
|
286 |
try {
|
287 |
transformer = tFactory.newTransformer(); |
288 |
DOMSource source = new DOMSource(document); |
289 |
StreamResult result = new StreamResult(os); |
290 |
transformer.transform(source, result); |
291 |
} catch (TransformerConfigurationException e) { |
292 |
throw new SchemaWrittingException(e); |
293 |
} catch (TransformerException e) { |
294 |
throw new SchemaWrittingException(e); |
295 |
} |
296 |
} |
297 |
|
298 |
/*
|
299 |
* (non-Javadoc)
|
300 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getNamespaceURI()
|
301 |
*/
|
302 |
public String getTargetNamespace() { |
303 |
return targetNamespace;
|
304 |
} |
305 |
|
306 |
/*
|
307 |
* (non-Javadoc)
|
308 |
* @see org.gvsig.xmlschema.som.IXSSchema#getTargetNamespacePrefix()
|
309 |
*/
|
310 |
public String getTargetNamespacePrefix() { |
311 |
return getNamespacePrefix(targetNamespace);
|
312 |
} |
313 |
|
314 |
/*
|
315 |
* (non-Javadoc)
|
316 |
* @see org.gvsig.xmlschema.som.IXSSchema#getNamespacePrefix(java.lang.String)
|
317 |
*/
|
318 |
public String getNamespacePrefix(String namespaceURI) { |
319 |
if (namespaceURI != null){ |
320 |
NamedNodeMap attributes = element.getAttributes(); |
321 |
for (int i=0 ; i<attributes.getLength() ; i++){ |
322 |
Node node = attributes.item(i); |
323 |
//String[] name = node.getNodeName().split(":");
|
324 |
String[] name = org.gvsig.gpe.utils.StringUtils.splitString(node.getNodeName(),":"); |
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 |
/**
|
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 |
* @see org.gvsig.gpe.schema.som.IXSSchema#addElement(java.lang.String, java.lang.String, java.lang.String)
|
346 |
*/
|
347 |
public IXSElementDeclaration addElement(String name, String type, String substitutionGroup) { |
348 |
String typeName = getTargetNamespacePrefix();
|
349 |
if (typeName != null){ |
350 |
type = typeName + ":" + type;
|
351 |
} |
352 |
Element element = getObjectsFactory().createElement(
|
353 |
this,
|
354 |
name, |
355 |
type, |
356 |
substitutionGroup); |
357 |
addChildElement(element); |
358 |
XSElementDeclarationImpl xsElement = new XSElementDeclarationImpl(this); |
359 |
xsElement.setElement(element); |
360 |
return xsElement;
|
361 |
} |
362 |
|
363 |
/*
|
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 |
return addElement(name, type, null); |
369 |
} |
370 |
|
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 |
|
408 |
|