root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / GPEXmlSchemaMap.java @ 12175
History | View | Annotate | Download (6.51 KB)
1 |
package org.gvsig.gpe.xml; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.net.URI; |
6 |
import java.net.URISyntaxException; |
7 |
import java.net.URL; |
8 |
import java.util.StringTokenizer; |
9 |
|
10 |
import org.gvsig.gpe.GPEErrorHandler; |
11 |
import org.gvsig.gpe.gml.GMLTags; |
12 |
import org.gvsig.xmlschema.exceptions.SchemaCreationException; |
13 |
import org.gvsig.xmlschema.som.impl.XSSchemaDocumentImpl; |
14 |
import org.gvsig.xmlschema.utils.DownloadUtilities; |
15 |
import org.gvsig.xmlschema.warnings.SchemaLocationWarning; |
16 |
import org.xmlpull.v1.XmlPullParser; |
17 |
import org.xmlpull.v1.XmlPullParserException; |
18 |
|
19 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
20 |
*
|
21 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
22 |
*
|
23 |
* This program is free software; you can redistribute it and/or
|
24 |
* modify it under the terms of the GNU General Public License
|
25 |
* as published by the Free Software Foundation; either version 2
|
26 |
* of the License, or (at your option) any later version.
|
27 |
*
|
28 |
* This program is distributed in the hope that it will be useful,
|
29 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31 |
* GNU General Public License for more details.
|
32 |
*
|
33 |
* You should have received a copy of the GNU General Public License
|
34 |
* along with this program; if not, write to the Free Software
|
35 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
36 |
*
|
37 |
* For more information, contact:
|
38 |
*
|
39 |
* Generalitat Valenciana
|
40 |
* Conselleria d'Infraestructures i Transport
|
41 |
* Av. Blasco Ib??ez, 50
|
42 |
* 46010 VALENCIA
|
43 |
* SPAIN
|
44 |
*
|
45 |
* +34 963862235
|
46 |
* gvsig@gva.es
|
47 |
* www.gvsig.gva.es
|
48 |
*
|
49 |
* or
|
50 |
*
|
51 |
* IVER T.I. S.A
|
52 |
* Salamanca 50
|
53 |
* 46005 Valencia
|
54 |
* Spain
|
55 |
*
|
56 |
* +34 963163400
|
57 |
* dac@iver.es
|
58 |
*/
|
59 |
/* CVS MESSAGES:
|
60 |
*
|
61 |
* $Id: GPEXmlSchemaMap.java 12175 2007-06-14 16:15:05Z jorpiell $
|
62 |
* $Log$
|
63 |
* Revision 1.4 2007-06-14 16:15:05 jorpiell
|
64 |
* builds to create the jars generated and add the schema code to the libGPEProject
|
65 |
*
|
66 |
* Revision 1.3 2007/06/14 13:50:05 jorpiell
|
67 |
* The schema jar name has been changed
|
68 |
*
|
69 |
* Revision 1.2 2007/06/08 13:01:12 jorpiell
|
70 |
* Add the targetNamespace to the file
|
71 |
*
|
72 |
* Revision 1.1 2007/06/07 14:53:30 jorpiell
|
73 |
* Add the schema support
|
74 |
*
|
75 |
*
|
76 |
*/
|
77 |
/**
|
78 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
79 |
*/
|
80 |
public class GPEXmlSchemaMap extends XSSchemaDocumentImpl{ |
81 |
private XmlPullParser parser = null; |
82 |
private URI xmlURI = null; |
83 |
private GPEErrorHandler errorHandler = null; |
84 |
|
85 |
public GPEXmlSchemaMap(XmlPullParser parser, URI xmlURI, GPEErrorHandler errorHandler){ |
86 |
this.parser = parser;
|
87 |
this.xmlURI = xmlURI;
|
88 |
this.errorHandler = errorHandler;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* Parser the xml header
|
93 |
* @throws XmlPullParserException
|
94 |
* @throws IOException
|
95 |
*/
|
96 |
public void parse() throws XmlPullParserException, IOException{ |
97 |
while ((parser.getName() == null) && |
98 |
!(parser.getEventType() == XmlPullParser.END_DOCUMENT)){ |
99 |
parser.next(); |
100 |
} |
101 |
for (int i = 0 ; i < parser.getAttributeCount() ; i++){ |
102 |
String attName = parser.getAttributeName(i);
|
103 |
String attValue = parser.getAttributeValue(i);
|
104 |
|
105 |
//it splits the attributes names at the both sides from ":"
|
106 |
String[] ns = attName.split(":"); |
107 |
|
108 |
//if is the targetNamespace declaration
|
109 |
if ((ns.length == 1) && (ns[0].compareTo(GMLTags.XML_NAMESPACE)==0)){ |
110 |
setTargetNamespace(attValue); |
111 |
} |
112 |
|
113 |
//If it founds the 'xmlns' is a new namespace declaration and it has to parse it
|
114 |
if ((ns.length>1) && (ns[0].compareTo(GMLTags.XML_NAMESPACE)==0)){ |
115 |
parseNameSpace(ns[1],attValue);
|
116 |
} |
117 |
|
118 |
//If its the "SCHEMA LOCATION" attribute, it means that there are schema and it tries to parse it
|
119 |
if ((ns.length>1) && (ns[1].compareTo(GMLTags.XML_SCHEMA_LOCATION)==0)){ |
120 |
parseSchemaLocation(ns[0],attValue);
|
121 |
} |
122 |
} |
123 |
} |
124 |
|
125 |
/***********************************************
|
126 |
* <parseSchemaLocation>
|
127 |
* It downloads the schema's file and parse it
|
128 |
* @param xmlnsName : Alias
|
129 |
* @param xmlnsValue: URI
|
130 |
***********************************************/
|
131 |
private void parseSchemaLocation(String schemaAlias, String schemaURI){ |
132 |
//It take the name of the schemas file to open or downlad
|
133 |
StringTokenizer tokenizer = new StringTokenizer(schemaURI, " \t"); |
134 |
while (tokenizer.hasMoreTokens()){
|
135 |
String URI = tokenizer.nextToken(); |
136 |
if (!tokenizer.hasMoreTokens()){
|
137 |
//If it hasn't the name of the schemas file or dont find it,
|
138 |
//it exits, and tries to parse without schema
|
139 |
System.out.println("Error, esquema no encontrado.PARSEO SIN ESQUEMA "); |
140 |
}else{
|
141 |
String schemaLocation = tokenizer.nextToken();
|
142 |
//TODO This line must be replaced by the new downloader
|
143 |
try {
|
144 |
URI uri = getSchemaURI(schemaLocation);
|
145 |
addSchema(uri); |
146 |
} catch (SchemaCreationException e) {
|
147 |
errorHandler.addError(e); |
148 |
} catch (SchemaLocationWarning e) {
|
149 |
errorHandler.addWarning(e); |
150 |
} |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
/****************************************************************************
|
156 |
* <getSchemaFile>
|
157 |
* It downloads the schema if it's a remote schema
|
158 |
* else it tries to open a local file and return if it's succesfull
|
159 |
* @param String schema location
|
160 |
* @return Uri
|
161 |
* @throws SchemaLocationWarning
|
162 |
****************************************************************************/
|
163 |
private URI getSchemaURI(String schemaLocation) throws SchemaLocationWarning{ |
164 |
File f = null; |
165 |
//If it is a local file, it has to construct the absolute route
|
166 |
if (schemaLocation.indexOf("http://") != 0){ |
167 |
f = new File(schemaLocation); |
168 |
if (!(f.isAbsolute())){
|
169 |
schemaLocation = new File(xmlURI).getParentFile().getAbsolutePath() + File.separator + schemaLocation; |
170 |
f = new File(schemaLocation); |
171 |
} |
172 |
try {
|
173 |
return new URI(f.getAbsolutePath()); |
174 |
} catch (URISyntaxException e) { |
175 |
throw new SchemaLocationWarning(schemaLocation,e); |
176 |
} |
177 |
} |
178 |
//Else it is an URL direction and it has to download it.
|
179 |
else {
|
180 |
URL url;
|
181 |
try {
|
182 |
url = new URL(schemaLocation); |
183 |
//Download the schema without cancel option.
|
184 |
f = DownloadUtilities.downloadFile(url,"gml_schmema.xsd");
|
185 |
return new URI(f.getAbsolutePath()); |
186 |
} catch (Exception e) { |
187 |
throw new SchemaLocationWarning(schemaLocation,e); |
188 |
} |
189 |
} |
190 |
} |
191 |
|
192 |
/************************************************
|
193 |
* <parseNamespace>
|
194 |
* It adds an XML namespace tag to the hashtable
|
195 |
* @param xmlnsName : Namespace
|
196 |
* @param xmlnsValue: URI
|
197 |
************************************************/
|
198 |
private void parseNameSpace(String xmlnsName,String xmlnsValue){ |
199 |
addNamespacePrefix(xmlnsName, xmlnsValue); |
200 |
} |
201 |
} |