Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / schemas / XMLSchemasManager.java @ 9531

History | View | Annotate | Download (5.76 KB)

1
package org.gvsig.remoteClient.gml.schemas;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.IOException;
6
import java.net.ConnectException;
7
import java.net.MalformedURLException;
8
import java.net.URL;
9
import java.net.UnknownHostException;
10
import java.util.Hashtable;
11
import java.util.StringTokenizer;
12

    
13
import org.gvsig.remoteClient.gml.GMLException;
14
import org.gvsig.remoteClient.gml.GMLTags;
15
import org.gvsig.remoteClient.gml.factories.XMLSchemasFactory;
16
import org.gvsig.remoteClient.utils.Utilities;
17
import org.kxml2.io.KXmlParser;
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: XMLSchemasManager.java 9531 2007-01-03 17:07:37Z  $
63
 * $Log$
64
 * Revision 1.2  2006-08-30 10:47:36  jorpiell
65
 * Añadido un File.separator en lugar de unas barras que impedia que funcioanra en linux
66
 *
67
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
68
 * Primer commit del driver de Gml
69
 *
70
 *
71
 */
72
/**
73
 * This class represents a GML file header. It has 
74
 * methods to parses the GML file header and retrieve
75
 * the namespaces and the attributes. If there is an schema
76
 * on the GML file, it has to manage it. It has to retrieve
77
 * the GML file version
78
 * 
79
 * @author Jorge Piera Llodr� (piera_jor@gva.es)
80
 */
81
public class XMLSchemasManager {
82
        private String version = null;
83
        private File file = null;
84
        private String targetNameSpace = null;
85
        
86
        public XMLSchemasManager(File file) {
87
                super();
88
                this.file = file;
89
        }
90
        
91
        /**
92
         * It parses the GML header and returns the attributes
93
         * @param parser
94
         * Reader for the GML file 
95
         * @throws IOException 
96
         * @throws XmlPullParserException 
97
         * @throws GMLException 
98
         */
99
        public void parse(XMLSchemaParser parser) throws XmlPullParserException, IOException, GMLException{
100
                parser.nextTag();
101
                
102
                for (int i=0 ; i<parser.getAttributeCount() ; i++){
103
                        String attName = parser.getAttributeName(i);
104
                        String attValue = parser.getAttributeValue(i);
105
                        if (attName.compareTo(GMLTags.VERSION)==0){
106
                                version = attValue;
107
                                break;
108
                        }
109
                        if (attName.compareTo(GMLTags.XML_TARGET_NAMESPACE)==0){
110
                                targetNameSpace = attValue;
111
                                break;
112
                        }
113
                        String[] ns = attName.split(":");
114
                        if ((ns.length>1) && (ns[0].compareTo(GMLTags.XML_NAMESPACE)==0)){
115
                                parseNameSpace(ns[1],attValue);                                
116
                        }
117
                        if ((ns.length>1) && (ns[1].compareTo(GMLTags.XML_SCHEMA_LOCATION)==0)){
118
                                parseSchemaLocation(ns[0],attValue);
119
                        }                        
120
                }        
121
                
122
        }
123
        
124
        /**
125
         * It parses an XML namespace tag
126
         * @param sNameSPace
127
         */
128
        private void parseNameSpace(String xmlnsName,String xmlnsValue){
129
                XMLSchemasFactory.addType(xmlnsName,xmlnsValue);                
130
        }
131
        
132
        /**
133
         * Parses the schema location attribute
134
         * @param schemas
135
         * XML attribute than contain the schema location info
136
         * @throws GMLException 
137
         */
138
        private void parseSchemaLocation(String namespace,String schemas) throws GMLException{
139
                StringTokenizer tokenizer = new StringTokenizer(schemas, " \t");
140
        while (tokenizer.hasMoreTokens()){
141
            String parent = tokenizer.nextToken();
142
            if (!tokenizer.hasMoreTokens()){
143
                throw new GMLException(GMLException.EXC_NO_SCHEMA);
144
            }
145
            String schema = tokenizer.nextToken();
146
            String name = XMLSchemasFactory.addSchemaLocation(namespace,parent,schema);
147
            parseSchema(schema,name);             
148
        }
149
        }        
150
        
151
        /**
152
         * It Downloads the schemas and parses them
153
         * @param urlString
154
         * Schema to parse
155
         * @return
156
         */
157
        private void parseSchema(String urlString,String nameSpace){
158
                if (urlString.indexOf("http://") != 0){
159
                        File f = new File(urlString);
160
                        if (!(f.isAbsolute())){
161
                                urlString = file.getParentFile().getAbsolutePath() + File.separator +  urlString;
162
                                f = new File(urlString);
163
                        }
164
                        new XMLSchemaParser().parse(f,nameSpace);
165
                }else{
166
                        URL url;
167
                        try {
168
                                url = new URL(urlString);
169
                                File f = Utilities.downloadFile(url,"gml_schmema.xsd", null);
170
                                if (f == null){
171
                                        return;
172
                                }else{
173
                                        new XMLSchemaParser().parse(f,nameSpace);
174
                                }  
175
                        }catch (MalformedURLException e1) {
176
                                        // TODO Auto-generated catch block
177
                                        e1.printStackTrace();                                
178
                        } catch (ConnectException e) {
179
                                // TODO Auto-generated catch block
180
                                e.printStackTrace();
181
                        } catch (UnknownHostException e) {
182
                                // TODO Auto-generated catch block
183
                                e.printStackTrace();
184
                        } catch (IOException e) {
185
                                // TODO Auto-generated catch block
186
                                e.printStackTrace();
187
                        }            
188
                }                
189
        }
190

    
191
        /**
192
         * @return Returns the targetNameSpace.
193
         */
194
        public String getTargetNameSpace() {
195
                return targetNameSpace;
196
        }
197

    
198
        /**
199
         * @return Returns the version.
200
         * TODO: Manage the different versions
201
         */
202
        public String getVersion() {
203
                if (version == null){
204
                        return "2.1.2";
205
                }
206
                return version;
207
        }
208

    
209
}