root / branches / Mobile1.0 / org.gvsig.gpe / src / org / gvsig / xmlschema / som / impl / XSSchemaImpl.java @ 79
History | View | Annotate | Download (12.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.namespace.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.compat.CompatLocator; |
18 |
import org.gvsig.xmlschema.exceptions.SchemaWrittingException; |
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.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 |
import org.w3c.dom.NamedNodeMap; |
32 |
import org.w3c.dom.Node; |
33 |
|
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: XSSchemaImpl.java 164 2007-07-02 10:00:46Z jorpiell $
|
77 |
* $Log$
|
78 |
* 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 |
* 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 |
* The typeNotFoundException has been deleted. It never was thrown
|
86 |
*
|
87 |
* Revision 1.1 2007/06/14 16:15:03 jorpiell
|
88 |
* 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 |
public XSSchemaImpl(Document document){ |
140 |
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 |
private void initializeMapping(){ |
154 |
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 |
public Collection getElementDeclarations(){ |
181 |
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 |
public IXSElementDeclaration getElementDeclarationByName(String elementURI, String elementName){ |
189 |
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 |
private SchemaObjectsMapping getElementMapping(){
|
237 |
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 |
public Collection getTypeDefinitions(){ |
247 |
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 |
public IXSTypeDefinition getTypeByName(String typeURI, String typeName){ |
255 |
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 |
private SchemaObjectsMapping getTypeMapping(){
|
273 |
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 |
/*
|
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 |
//String[] name = org.gvsig.gpe.utils.StringUtils.splitString(node.getNodeName(),":");
|
326 |
String[] name = CompatLocator.getStringUtils().split(node.getNodeName(), ":"); |
327 |
if ((name.length == 2) && (name[0].equals(SchemaTags.XMLNS_NS))){ |
328 |
if (node.getNodeValue().equals(namespaceURI)){
|
329 |
return name[1]; |
330 |
} |
331 |
} |
332 |
} |
333 |
} |
334 |
return null; |
335 |
} |
336 |
|
337 |
/**
|
338 |
* Add a new child element
|
339 |
* @param childElement
|
340 |
*/
|
341 |
private void addChildElement(Element childElement) { |
342 |
element.appendChild(childElement); |
343 |
} |
344 |
|
345 |
/*
|
346 |
* (non-Javadoc)
|
347 |
* @see org.gvsig.gpe.schema.som.IXSSchema#addElement(java.lang.String, java.lang.String, java.lang.String)
|
348 |
*/
|
349 |
public IXSElementDeclaration addElement(String name, String type, String substitutionGroup) { |
350 |
String typeName = getTargetNamespacePrefix();
|
351 |
if (typeName != null){ |
352 |
type = typeName + ":" + type;
|
353 |
} |
354 |
Element element = getObjectsFactory().createElement(
|
355 |
this,
|
356 |
name, |
357 |
type, |
358 |
substitutionGroup); |
359 |
addChildElement(element); |
360 |
XSElementDeclarationImpl xsElement = new XSElementDeclarationImpl(this); |
361 |
xsElement.setElement(element); |
362 |
return xsElement;
|
363 |
} |
364 |
|
365 |
/*
|
366 |
* (non-Javadoc)
|
367 |
* @see org.gvsig.gpe.schema.som.IXSSchema#addElement(java.lang.String, java.lang.String)
|
368 |
*/
|
369 |
public IXSElementDeclaration addElement(String name, String type) { |
370 |
return addElement(name, type, null); |
371 |
} |
372 |
|
373 |
/*
|
374 |
* (non-Javadoc)
|
375 |
* @see org.gvsig.gpe.schema.som.IXSSchema#addComplexType(java.lang.String)
|
376 |
*/
|
377 |
public IXSComplexTypeDefinition addComplexType(String name, String type, |
378 |
String contentType, String conteTypeRestriction) { |
379 |
Element eComplexType = getObjectsFactory().createComplexType(
|
380 |
this,
|
381 |
name, |
382 |
type, |
383 |
contentType, |
384 |
conteTypeRestriction); |
385 |
addChildElement(eComplexType); |
386 |
XSComplexTypeDefinitionImpl complexType = new XSComplexTypeDefinitionImpl(this); |
387 |
complexType.setElement(eComplexType); |
388 |
return complexType;
|
389 |
} |
390 |
|
391 |
/**
|
392 |
* @return the DOM objects factory
|
393 |
*/
|
394 |
private DOMObjectsFactory getObjectsFactory() {
|
395 |
if (objectsFactory == null){ |
396 |
objectsFactory = DOMObjectsFactory.getInstance(); |
397 |
} |
398 |
return objectsFactory;
|
399 |
} |
400 |
|
401 |
/*
|
402 |
* (non-Javadoc)
|
403 |
* @see org.gvsig.gpe.schema.som.IXSSchema#getTypesMapping()
|
404 |
*/
|
405 |
public SchemaObjectsMapping getObjectsMapping() {
|
406 |
return objectsMapping;
|
407 |
} |
408 |
} |
409 |
|
410 |
|