root / org.gvsig.xmlschema / library / trunk / org.gvsig.xmlschema / org.gvsig.xmlschema.prov / org.gvsig.xmlschema.prov.kxml / src / main / java / org / gvsig / xmlschema / prov / kxml / model / XSSchema.java @ 250
History | View | Annotate | Download (14.7 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.xmlschema.prov.kxml.model; |
23 |
|
24 |
import java.io.IOException; |
25 |
import java.io.InputStream; |
26 |
import java.io.OutputStream; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Collection; |
29 |
import java.util.Hashtable; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import java.util.Set; |
33 |
|
34 |
import org.xmlpull.v1.XmlPullParserException; |
35 |
|
36 |
import org.gvsig.xmlpull.lib.api.XmlPullLocator; |
37 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamReader; |
38 |
import org.gvsig.xmlpull.lib.api.stream.XmlStreamException; |
39 |
import org.gvsig.xmlschema.lib.api.XMLSchemaLocator; |
40 |
import org.gvsig.xmlschema.lib.api.XMLSchemaManager; |
41 |
import org.gvsig.xmlschema.lib.api.exceptions.SchemaWrittingException; |
42 |
import org.gvsig.xmlschema.lib.api.som.IXSComplexTypeDefinition; |
43 |
import org.gvsig.xmlschema.lib.api.som.IXSDocument; |
44 |
import org.gvsig.xmlschema.lib.api.som.IXSElement; |
45 |
import org.gvsig.xmlschema.lib.api.som.IXSElementDeclaration; |
46 |
import org.gvsig.xmlschema.lib.api.som.IXSSchema; |
47 |
import org.gvsig.xmlschema.lib.api.som.IXSTypeDefinition; |
48 |
import org.gvsig.xmlschema.lib.api.utils.SchemaObjectsMapping; |
49 |
import org.gvsig.xmlschema.lib.api.utils.SchemaTags; |
50 |
import org.gvsig.xmlschema.prov.kxml.xsd.type.GMLTypes; |
51 |
import org.gvsig.xmlschema.prov.kxml.xsd.type.XSDTypes; |
52 |
|
53 |
|
54 |
/**
|
55 |
* @author gvSIG Team
|
56 |
* @version $Id$
|
57 |
*
|
58 |
*/
|
59 |
public class XSSchema implements IXSSchema{ |
60 |
private String targetNameSpace = null; |
61 |
private String version = null; |
62 |
private Hashtable types = new Hashtable(); |
63 |
private List elements = new ArrayList(); |
64 |
private XMLSchemaManager schemaManager;
|
65 |
|
66 |
public XSSchema(InputStream is) throws IOException { |
67 |
schemaManager = XMLSchemaLocator.getXMLSchemaManager(); |
68 |
IXmlStreamReader xmlStreamReader = XmlPullLocator.getXMLManager().createStreamReader("text/xml", is);
|
69 |
parse(xmlStreamReader); |
70 |
} |
71 |
|
72 |
|
73 |
/********************************************************************************
|
74 |
* FUNCTION PARSE(FILE,NAMESPACE) PARSING THE XML SCHEMA ".xsd" *
|
75 |
* *
|
76 |
* -gml version *
|
77 |
* -elementFormDefault {qualified,non-qualified} (not implemented yet) *
|
78 |
* -namespaces "xmlns" (nor implemented,only in gml file) *
|
79 |
* -targetNamespace (the name of this schema) *
|
80 |
* -imports (not implemented) *
|
81 |
* -features -> "elements"="types" *
|
82 |
* *
|
83 |
* @param f (file ".xsd" to parse) *
|
84 |
* @param nameSpace (Schemas name) *
|
85 |
* @throws IOException *
|
86 |
* @throws XmlPullParserException *
|
87 |
* *
|
88 |
********************************************************************************/
|
89 |
private void parse(IXmlStreamReader xmlStreamReader) throws IOException{ |
90 |
int tag;
|
91 |
|
92 |
while (xmlStreamReader.getEventType() != IXmlStreamReader.START_ELEMENT){
|
93 |
xmlStreamReader.next(); |
94 |
if (xmlStreamReader.getEventType() == IXmlStreamReader.END_DOCUMENT ) {
|
95 |
return;
|
96 |
} |
97 |
} |
98 |
|
99 |
parseHeader(xmlStreamReader); |
100 |
|
101 |
tag = xmlStreamReader.nextTag(); |
102 |
|
103 |
while(tag != IXmlStreamReader.END_DOCUMENT)
|
104 |
{ |
105 |
switch(tag)
|
106 |
{ |
107 |
case IXmlStreamReader.START_ELEMENT:
|
108 |
|
109 |
/************************
|
110 |
* Etiqueta <import> *
|
111 |
************************/
|
112 |
//imports elements from other schemas (other schema ".xsd" files)
|
113 |
|
114 |
/****************************
|
115 |
* Etiqueta <complexType> *
|
116 |
****************************/
|
117 |
if (SchemaTags.COMPLEX_TYPE.equals(xmlStreamReader.getName().getLocalPart())){
|
118 |
|
119 |
for (int i=0 ; i<xmlStreamReader.getAttributeCount() ; i++){ |
120 |
/********************
|
121 |
* Atributo <name> *
|
122 |
********************/
|
123 |
if (SchemaTags.NAME.equals(xmlStreamReader.getAttributeName(i).getLocalPart())){
|
124 |
// inserts a new complex type inside the namespace
|
125 |
XSComplexTypeDefinition complexTypeDefinition = |
126 |
new XSComplexTypeDefinition(this, null, xmlStreamReader.getAttributeValue(i), xmlStreamReader); |
127 |
addType(complexTypeDefinition); |
128 |
} |
129 |
|
130 |
} |
131 |
} |
132 |
/****************************
|
133 |
* Etiqueta <simpleType> *
|
134 |
****************************/
|
135 |
// SIMPLE TYPE elements like enumerations not implemented
|
136 |
else if (SchemaTags.SIMPLE_TYPE.equals(xmlStreamReader.getName().getLocalPart())){ |
137 |
parseSimpleType(xmlStreamReader); |
138 |
} |
139 |
/************************
|
140 |
* Etiqueta <element> *
|
141 |
************************/
|
142 |
else if (SchemaTags.ELEMENT.equals(xmlStreamReader.getName().getLocalPart())){ |
143 |
XSElement element = new XSElement(this, xmlStreamReader); |
144 |
elements.add(element); |
145 |
} |
146 |
break;
|
147 |
case IXmlStreamReader.END_ELEMENT:
|
148 |
break;
|
149 |
//Show the Text on the screen
|
150 |
case IXmlStreamReader.CHARACTERS:
|
151 |
|
152 |
break;
|
153 |
} |
154 |
tag = xmlStreamReader.next(); |
155 |
} |
156 |
//require(KXmlParser.END_DOCUMENT, null, null);
|
157 |
} |
158 |
|
159 |
private void parseHeader(IXmlStreamReader xmlStreamReader) throws XmlStreamException { |
160 |
/************************
|
161 |
* Etiqueta <schema> *
|
162 |
************************/
|
163 |
//Searching the init tag "schema"
|
164 |
//xmlStreamReader.require(KXmlParser.START_TAG, null, SchemaTags.SCHEMA_ROOT);
|
165 |
for (int i=0 ; i<xmlStreamReader.getAttributeCount() ; i++){ |
166 |
//getting attributes and values
|
167 |
String attName = xmlStreamReader.getAttributeName(i).getLocalPart();
|
168 |
String attValue = xmlStreamReader.getAttributeValue(i);
|
169 |
|
170 |
/********************************
|
171 |
* Atributo <targetNamespace> *
|
172 |
********************************/
|
173 |
//Target Namespace (URI)
|
174 |
//this is the namespace of all components in the schema
|
175 |
//setTargetNamespace();
|
176 |
if (SchemaTags.TARGET_NAMESPACE.equals(attName)){
|
177 |
targetNameSpace = attValue; |
178 |
} |
179 |
|
180 |
/************************************
|
181 |
* Atributo <elementFormDefault> *
|
182 |
************************************/
|
183 |
//Qualified--> Los elementos del espacio de nombres de destino deben cualificarse
|
184 |
//con el prefijo del espacio de nombres.
|
185 |
//Unqualified--> No es necesario que los elementos del
|
186 |
//espacio de nombres de destino est?n cualificados con el prefijo del espacio de nombres.
|
187 |
//(Espacion_de_Nombres:Elemento)
|
188 |
//elementFormDefault();
|
189 |
if (SchemaTags.ELEMENTFORMDEFAULT.equals(attName)){
|
190 |
|
191 |
} |
192 |
|
193 |
/************************************
|
194 |
* Atributo <attributeFormDefault> *
|
195 |
************************************/
|
196 |
//Lo mismo que el anterior pero con los atributos...
|
197 |
//(Espacio_de_Nombres:Atributo)
|
198 |
//attributeFormDefault();
|
199 |
if (SchemaTags.ATTRIBUTEFORMDEFAULT.equals(attName)){
|
200 |
|
201 |
} |
202 |
|
203 |
/************************
|
204 |
* Atributo <Version> *
|
205 |
************************/
|
206 |
//Gets gml version to parse by the right parser.
|
207 |
//getversion();
|
208 |
if (SchemaTags.VERSION.equals(attName)){
|
209 |
version = attValue; |
210 |
} |
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
/****************************************************
|
216 |
* FUNCTION PARSE SIMPLE TYPE() *
|
217 |
* *
|
218 |
* Parse simple types and its restrictions *
|
219 |
* *
|
220 |
****************************************************/
|
221 |
private void parseSimpleType(IXmlStreamReader xmlStreamReader) throws IOException{ |
222 |
int currentTag;
|
223 |
boolean end = false; |
224 |
currentTag = xmlStreamReader.getEventType(); |
225 |
|
226 |
String typeName = null; |
227 |
String typeValue = null; |
228 |
|
229 |
for (int i=0 ; i<xmlStreamReader.getAttributeCount() ; i++){ |
230 |
if (SchemaTags.NAME.equals(xmlStreamReader.getName().getLocalPart())){
|
231 |
typeName = xmlStreamReader.getAttributeValue(i); |
232 |
} |
233 |
} |
234 |
|
235 |
while (!end){
|
236 |
switch(currentTag){
|
237 |
case IXmlStreamReader.START_ELEMENT:
|
238 |
if (SchemaTags.RESTRICTION.equals(xmlStreamReader.getName().getLocalPart())){
|
239 |
for (int i=0 ; i<xmlStreamReader.getAttributeCount() ; i++){ |
240 |
if (SchemaTags.BASE.equals(xmlStreamReader.getName().getLocalPart())){
|
241 |
typeValue = xmlStreamReader.getAttributeValue(i); |
242 |
} |
243 |
} |
244 |
} |
245 |
//Falta parsear los tipos enumerados
|
246 |
break;
|
247 |
case IXmlStreamReader.END_ELEMENT:
|
248 |
if (SchemaTags.SIMPLE_TYPE.equals(xmlStreamReader.getName().getLocalPart())){
|
249 |
end = true;
|
250 |
} |
251 |
break;
|
252 |
case IXmlStreamReader.CHARACTERS:
|
253 |
break;
|
254 |
} |
255 |
if (!end){
|
256 |
currentTag = xmlStreamReader.next(); |
257 |
} |
258 |
} |
259 |
if ((typeName != null) && (typeValue != null)){ |
260 |
this.addType(new XSimpleTypeDefinition(this, typeName, typeValue)); |
261 |
} |
262 |
} |
263 |
|
264 |
void addType(IXSTypeDefinition typeDefinition){
|
265 |
types.put(typeDefinition.getTypeName().toUpperCase(), typeDefinition); |
266 |
} |
267 |
|
268 |
public IXSComplexTypeDefinition addComplexType(String name, String type, |
269 |
String contentType, String conteTypeRestriction) { |
270 |
// TODO Auto-generated method stub
|
271 |
return null; |
272 |
} |
273 |
|
274 |
public IXSElementDeclaration addElement(String name, String type, |
275 |
String substitutionGroup) {
|
276 |
// TODO Auto-generated method stub
|
277 |
return null; |
278 |
} |
279 |
|
280 |
public IXSElementDeclaration addElement(String name, String type) { |
281 |
// TODO Auto-generated method stub
|
282 |
return null; |
283 |
} |
284 |
|
285 |
public IXSDocument getDocument() {
|
286 |
// TODO Auto-generated method stub
|
287 |
return null; |
288 |
} |
289 |
|
290 |
public IXSElementDeclaration getElementDeclarationByName(
|
291 |
String targetNamespace, String elementName) { |
292 |
Iterator it = elements.iterator();
|
293 |
while (it.hasNext()){
|
294 |
IXSElementDeclaration elementDeclaration = (IXSElementDeclaration)it.next(); |
295 |
if (elementName.equals(elementDeclaration.getQName().getLocalPart())){
|
296 |
return elementDeclaration;
|
297 |
} |
298 |
} |
299 |
return null; |
300 |
} |
301 |
|
302 |
public Collection getElementDeclarations() { |
303 |
return elements;
|
304 |
} |
305 |
|
306 |
public String getNamespacePrefix(String namespaceURI) { |
307 |
// TODO Auto-generated method stub
|
308 |
return null; |
309 |
} |
310 |
|
311 |
public SchemaObjectsMapping getObjectsMapping() {
|
312 |
// TODO Auto-generated method stub
|
313 |
return null; |
314 |
} |
315 |
|
316 |
public String getTargetNamespace() { |
317 |
// TODO Auto-generated method stub
|
318 |
return null; |
319 |
} |
320 |
|
321 |
public String getTargetNamespacePrefix() { |
322 |
// TODO Auto-generated method stub
|
323 |
return null; |
324 |
} |
325 |
|
326 |
public IXSTypeDefinition getTypeByName(String targetNamespace, |
327 |
String typeName) {
|
328 |
IXSTypeDefinition xmlType = (IXSTypeDefinition)types.get(typeName.toUpperCase()); |
329 |
if (xmlType == null){ |
330 |
xmlType = schemaManager.getTypeDefinition(typeName); |
331 |
} |
332 |
if (xmlType == null){ |
333 |
xmlType = getTypeWithOutNameSpace(typeName); |
334 |
} |
335 |
return xmlType;
|
336 |
} |
337 |
|
338 |
/**
|
339 |
* This method is used to solve some mistakes. It doesn't
|
340 |
* consider the namespace
|
341 |
* @param type
|
342 |
* @return
|
343 |
*/
|
344 |
private IXSTypeDefinition getTypeWithOutNameSpace(String typeName){ |
345 |
Set keys = types.keySet();
|
346 |
Iterator it = keys.iterator();
|
347 |
String[] typeParts = typeName.split(":"); |
348 |
String typeAux = typeName;
|
349 |
if (typeParts.length > 1){ |
350 |
typeAux = typeParts[1];
|
351 |
} |
352 |
while(it.hasNext()){
|
353 |
String key = (String)it.next(); |
354 |
String[] parts = key.split(":"); |
355 |
if (parts.length == 1){ |
356 |
if (parts[0].compareTo(typeAux.toUpperCase())==0){ |
357 |
return (IXSTypeDefinition)types.get(key);
|
358 |
} |
359 |
}else if (parts.length > 1){ |
360 |
if (parts[parts.length-1].compareTo(typeAux.toUpperCase())==0){ |
361 |
return (IXSTypeDefinition)types.get(key);
|
362 |
} |
363 |
} |
364 |
} |
365 |
return null; |
366 |
} |
367 |
|
368 |
public Collection getTypeDefinitions() { |
369 |
// TODO Auto-generated method stub
|
370 |
return null; |
371 |
} |
372 |
|
373 |
public void write(OutputStream os) throws SchemaWrittingException { |
374 |
// TODO Auto-generated method stub
|
375 |
|
376 |
} |
377 |
|
378 |
} |