root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / schemas / XMLSchemaManager.java @ 9729
History | View | Annotate | Download (8.57 KB)
1 |
package org.gvsig.remoteClient.gml.schemas; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.net.ConnectException; |
6 |
import java.net.MalformedURLException; |
7 |
import java.net.URL; |
8 |
import java.net.UnknownHostException; |
9 |
import java.util.StringTokenizer; |
10 |
|
11 |
import org.gvsig.exceptions.BaseException; |
12 |
import org.gvsig.remoteClient.gml.GMLTags; |
13 |
import org.gvsig.remoteClient.gml.factories.XMLSchemasFactory; |
14 |
import org.gvsig.remoteClient.gml.warnings.GMLWarningInfo; |
15 |
import org.gvsig.remoteClient.gml.warnings.GMLWarningMalformed; |
16 |
import org.gvsig.remoteClient.gml.warnings.GMLWarningNoSchema; |
17 |
import org.gvsig.remoteClient.gml.warnings.GMLWarningNotFound; |
18 |
import org.gvsig.remoteClient.utils.Utilities; |
19 |
import org.xmlpull.v1.XmlPullParserException; |
20 |
|
21 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
22 |
*
|
23 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
24 |
*
|
25 |
* This program is free software; you can redistribute it and/or
|
26 |
* modify it under the terms of the GNU General Public License
|
27 |
* as published by the Free Software Foundation; either version 2
|
28 |
* of the License, or (at your option) any later version.
|
29 |
*
|
30 |
* This program is distributed in the hope that it will be useful,
|
31 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
32 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
33 |
* GNU General Public License for more details.
|
34 |
*
|
35 |
* You should have received a copy of the GNU General Public License
|
36 |
* along with this program; if not, write to the Free Software
|
37 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
38 |
*
|
39 |
* For more information, contact:
|
40 |
*
|
41 |
* Generalitat Valenciana
|
42 |
* Conselleria d'Infraestructures i Transport
|
43 |
* Av. Blasco Ib??ez, 50
|
44 |
* 46010 VALENCIA
|
45 |
* SPAIN
|
46 |
*
|
47 |
* +34 963862235
|
48 |
* gvsig@gva.es
|
49 |
* www.gvsig.gva.es
|
50 |
*
|
51 |
* or
|
52 |
*
|
53 |
* IVER T.I. S.A
|
54 |
* Salamanca 50
|
55 |
* 46005 Valencia
|
56 |
* Spain
|
57 |
*
|
58 |
* +34 963163400
|
59 |
* dac@iver.es
|
60 |
*/
|
61 |
/* CVS MESSAGES:
|
62 |
*
|
63 |
* $Id: XMLSchemaManager.java 9729 2007-01-15 13:11:17Z csanchez $
|
64 |
* $Log$
|
65 |
* Revision 1.2 2007-01-15 13:11:00 csanchez
|
66 |
* Sistema de Warnings y Excepciones adaptado a BasicException
|
67 |
*
|
68 |
* Revision 1.1 2006/12/22 11:25:04 csanchez
|
69 |
* Nuevo parser GML 2.x para gml's sin esquema
|
70 |
*
|
71 |
*
|
72 |
*
|
73 |
* Revision 1.2 2006/08/30 10:47:36 jorpiell
|
74 |
* A?adido un File.separator en lugar de unas barras que impedia que funcioanra en linux
|
75 |
*
|
76 |
* Revision 1.1 2006/08/10 12:00:49 jorpiell
|
77 |
* Primer commit del driver de Gml
|
78 |
*
|
79 |
*
|
80 |
*/
|
81 |
/**
|
82 |
* This class represents a GML file header. It has
|
83 |
* methods to parses the GML file header and retrieve
|
84 |
* the namespaces and the attributes. If there is an schema
|
85 |
* on the GML file, it has to manage it. It has to retrieve
|
86 |
* the GML file version
|
87 |
*
|
88 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
89 |
* @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
|
90 |
*
|
91 |
*/
|
92 |
public class XMLSchemaManager { |
93 |
|
94 |
private File file = null; |
95 |
private String schema = null; |
96 |
private XMLSchemaParser nameSpaceParser = new XMLSchemaParser(); |
97 |
private String mainTag; |
98 |
private int no_schema = 1; |
99 |
|
100 |
public GMLWarningInfo warnings = null; |
101 |
|
102 |
public XMLSchemaManager(File file) { |
103 |
super();
|
104 |
this.file = file;
|
105 |
this.warnings= new GMLWarningInfo(); |
106 |
} |
107 |
|
108 |
/**
|
109 |
* Reader for the GML file
|
110 |
* It parses the GML header and returns the attributes
|
111 |
*
|
112 |
* @param parser
|
113 |
*
|
114 |
* @throws IOException
|
115 |
* @throws XmlPullParserException
|
116 |
*/
|
117 |
public void parse(XMLSchemaParser parser) throws XmlPullParserException, IOException{ |
118 |
//nextTag() --> Method from KXML library to get next full tag
|
119 |
parser.nextTag(); |
120 |
//It keeps the name of the start tag to compare with the close tag later
|
121 |
mainTag = parser.getName(); |
122 |
//If it has namespace before the ":" we keep the maintag without namespace
|
123 |
int pos = mainTag.indexOf(":"); |
124 |
if (pos > 0){ |
125 |
mainTag = mainTag.substring(mainTag.indexOf(":") + 1,mainTag.length()); |
126 |
} |
127 |
//It start to get all the attributes and values from the header tag
|
128 |
for (int i=0 ; i<parser.getAttributeCount() ; i++){ |
129 |
String attName = parser.getAttributeName(i);
|
130 |
String attValue = parser.getAttributeValue(i);
|
131 |
|
132 |
|
133 |
//it splits the attributes names at the both sides from ":"
|
134 |
String[] ns = attName.split(":"); |
135 |
|
136 |
//If it founds the 'xmlns' is a new namespace declaration and it has to parse it
|
137 |
if ((ns.length>1) && (ns[0].compareTo(GMLTags.XML_NAMESPACE)==0)){ |
138 |
parseNameSpace(ns[1],attValue);
|
139 |
} |
140 |
//If its the SCHEMA LOCATION attribute, it means that there are schema and it tries to parse it
|
141 |
if ((ns.length>1) && (ns[1].compareTo(GMLTags.XML_SCHEMA_LOCATION)==0)){ |
142 |
no_schema=0;
|
143 |
parseSchemaLocation(ns[0],attValue);
|
144 |
} |
145 |
} |
146 |
if (no_schema==1){ |
147 |
//Alert that th GML File hasn't schema but it tries to parse
|
148 |
warnings.setElement(new GMLWarningNoSchema());
|
149 |
} |
150 |
} |
151 |
|
152 |
/**
|
153 |
* It adds an XML namespace tag to the hashtable
|
154 |
* @param xmlnsName : Namespace
|
155 |
* @param xmlnsValue: URI
|
156 |
*/
|
157 |
private void parseNameSpace(String xmlnsName,String xmlnsValue){ |
158 |
XMLSchemasFactory.addType(xmlnsName,xmlnsValue); |
159 |
} |
160 |
|
161 |
/**
|
162 |
* Parses the schema location attribute
|
163 |
* XML attribute that contains the schema location info
|
164 |
*
|
165 |
* @param namespace
|
166 |
* @param schemas
|
167 |
**/
|
168 |
private void parseSchemaLocation(String namespace,String schemas){ |
169 |
// It take the name of the schemas file to open or downlad
|
170 |
StringTokenizer tokenizer = new StringTokenizer(schemas, " \t"); |
171 |
while (tokenizer.hasMoreTokens()){
|
172 |
String URI = tokenizer.nextToken(); |
173 |
if (!tokenizer.hasMoreTokens()){
|
174 |
//If it hasn't the name of the schemas file or dont find it,
|
175 |
//it exits, and tries to parse without schema
|
176 |
warnings.setElement(new GMLWarningNotFound(null,null)); |
177 |
} |
178 |
else
|
179 |
{ |
180 |
schema = tokenizer.nextToken(); |
181 |
//It add the schemaLocation to the hashtable
|
182 |
String name = null; |
183 |
try {
|
184 |
name = XMLSchemasFactory.addSchemaLocation(namespace,URI,schema);
|
185 |
} |
186 |
catch (BaseException e) {
|
187 |
// TODO Auto-generated catch block
|
188 |
warnings.setElement(new GMLWarningMalformed());
|
189 |
} |
190 |
//It parses the schema.
|
191 |
parseSchema(schema,name); |
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
/**
|
197 |
* Schema to parse
|
198 |
* It Downloads the schemas and parses them
|
199 |
* @param urlString
|
200 |
* @param nameSpace
|
201 |
*/
|
202 |
private void parseSchema(String urlString,String nameSpace){ |
203 |
//If it is a local file, it has to construct the absolute route
|
204 |
if (urlString.indexOf("http://") != 0){ |
205 |
File f = new File(urlString); |
206 |
if (!(f.isAbsolute())){
|
207 |
urlString = file.getParentFile().getAbsolutePath() + File.separator + urlString;
|
208 |
f = new File(urlString); |
209 |
} |
210 |
/****************************************
|
211 |
* CALL TO THE SCHEMA PARSER .XSD *
|
212 |
****************************************/
|
213 |
try {
|
214 |
nameSpaceParser.parse(f,nameSpace); |
215 |
} catch (IOException e) { |
216 |
//We can't open the file of the schema
|
217 |
warnings.setElement(new GMLWarningNotFound(f.getName(),e));
|
218 |
} catch (XmlPullParserException e) {
|
219 |
//We can't open the file of the schema
|
220 |
warnings.setElement(new GMLWarningNotFound(f.getName(),e));
|
221 |
} |
222 |
|
223 |
//Else it is an URL direction and it has to download it.
|
224 |
}else{
|
225 |
URL url;
|
226 |
try {
|
227 |
url = new URL(urlString); |
228 |
//Download the schema without cancel option.
|
229 |
File f = Utilities.downloadFile(url,"gml_schmema.xsd", null); |
230 |
/****************************************
|
231 |
* CALL TO THE SCHEMA PARSER .XSD *
|
232 |
****************************************/
|
233 |
nameSpaceParser.parse(f,nameSpace); |
234 |
} |
235 |
catch (MalformedURLException e) { |
236 |
//We can't open the file of the schema
|
237 |
warnings.setElement(new GMLWarningNotFound(urlString,e));
|
238 |
} |
239 |
catch (ConnectException e) { |
240 |
//We can't open the file of the schema
|
241 |
warnings.setElement(new GMLWarningNotFound(urlString,e));
|
242 |
} |
243 |
catch (UnknownHostException e) { |
244 |
//We can't open the file of the schema
|
245 |
warnings.setElement(new GMLWarningNotFound(urlString,e));
|
246 |
} |
247 |
catch (IOException e) { |
248 |
//We can't open the file of the schema
|
249 |
warnings.setElement(new GMLWarningNotFound(urlString,e));
|
250 |
} |
251 |
catch (XmlPullParserException e) {
|
252 |
//We can't open the file of the schema
|
253 |
warnings.setElement(new GMLWarningNotFound(urlString,e));
|
254 |
} |
255 |
} |
256 |
} |
257 |
|
258 |
/**
|
259 |
* @return Returns if there are schema.
|
260 |
*/
|
261 |
public boolean Schema() { |
262 |
if (schema==null) |
263 |
return false; |
264 |
else
|
265 |
return true; |
266 |
} |
267 |
/**
|
268 |
* @return Returns the targetNameSpace.
|
269 |
*/
|
270 |
public String getTargetNamespace() { |
271 |
return nameSpaceParser.getTargetNamespace();
|
272 |
} |
273 |
|
274 |
/**
|
275 |
* @return Returns the version.
|
276 |
* TODO: Manage the different versions
|
277 |
*/
|
278 |
public String getVersion() { |
279 |
return nameSpaceParser.getversion();
|
280 |
} |
281 |
} |