Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / parser / GPEXmlSchemaDocument.java @ 19580

History | View | Annotate | Download (6.97 KB)

1
package org.gvsig.gpe.xml.parser;
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.GPEDefaults;
11
import org.gvsig.gpe.gml.utils.GMLTags;
12
import org.gvsig.gpe.parser.GPEErrorHandler;
13
import org.gvsig.xmlschema.exceptions.SchemaCreationException;
14
import org.gvsig.xmlschema.som.impl.XSSchemaDocumentImpl;
15
import org.gvsig.xmlschema.utils.DownloadUtilities;
16
import org.gvsig.xmlschema.warnings.SchemaLocationWarning;
17
import org.xmlpull.v1.XmlPullParser;
18
import org.xmlpull.v1.XmlPullParserException;
19

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