Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_909 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / gml / GMLSchemaCreator.java @ 11150

History | View | Annotate | Download (6.15 KB)

1
package com.iver.cit.gvsig.fmap.edition.writers.gml;
2

    
3
import java.io.File;
4
import java.io.FileWriter;
5
import java.io.IOException;
6
import java.io.Writer;
7

    
8
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
9
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
10
import com.iver.cit.gvsig.fmap.drivers.gml.GMLTypesConversor;
11

    
12
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
13
 *
14
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
29
 *
30
 * For more information, contact:
31
 *
32
 *  Generalitat Valenciana
33
 *   Conselleria d'Infraestructures i Transport
34
 *   Av. Blasco Ib??ez, 50
35
 *   46010 VALENCIA
36
 *   SPAIN
37
 *
38
 *      +34 963862235
39
 *   gvsig@gva.es
40
 *      www.gvsig.gva.es
41
 *
42
 *    or
43
 *
44
 *   IVER T.I. S.A
45
 *   Salamanca 50
46
 *   46005 Valencia
47
 *   Spain
48
 *
49
 *   +34 963163400
50
 *   dac@iver.es
51
 */
52
/* CVS MESSAGES:
53
 *
54
 * $Id: GMLSchemaCreator.java 11150 2007-04-12 08:35:23Z  $
55
 * $Log$
56
 * Revision 1.3  2006-07-24 07:36:40  jorpiell
57
 * Se han hecho un cambio en los nombres de los metodos para clarificar
58
 *
59
 * Revision 1.2  2006/07/21 08:56:59  jorpiell
60
 * Se ha modificado la versi?n del esquema
61
 *
62
 * Revision 1.1  2006/07/19 12:29:39  jorpiell
63
 * A?adido el driver de GML
64
 *
65
 *
66
 */
67
/**
68
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
69
 */
70
public class GMLSchemaCreator {
71
        public final static String GML_SUBSTITUTION_GROUP = "gml:_Feature";
72
        public final static String GML_GEOMETRY = "the_geom";                
73
        public final static String NAMESPACE = "cit";
74
        public final static String TARGET_NAMESPACE = "http://www.gvsig.com/cit";
75
        public final static String GML_VERSION = "2.1.2";
76
        
77
        private static String featureName = null;
78
        private static String featureType = null;
79
        
80
        private Writer writer = null;
81
        
82
        
83
        public GMLSchemaCreator(File m_File) throws IOException {
84
                super();                
85
                writer = new FileWriter(m_File);
86
                featureName = m_File.getName().replaceAll("\\.xsd","");
87
                featureType = featureName + "_Type";
88
                
89
        }
90
        
91
        public void createFile(LayerDefinition lyrDef) throws IOException{
92
                writer.write(getInitFile());
93
                writer.write(getInitComplexTypes());
94
        
95
                writer.write(getAttribute(GML_GEOMETRY,
96
                                GMLTypesConversor.gvSIGToGMLTypes(lyrDef.getShapeType())));                
97
                FieldDescription[] fieldDescription = lyrDef.getFieldsDesc();
98
                for (int i=0 ; i<fieldDescription.length ; i++){
99
                        writer.write(getAttribute(fieldDescription[i]));
100
                }                
101
                
102
                writer.write(getEndComplexTypes());
103
                writer.write(getEndFile());
104
        }        
105
        
106
        /**
107
         * Writes the schema file
108
         * @throws IOException 
109
         *
110
         */
111
        public void writeFile() throws IOException {
112
                writer.close();                
113
        }
114
        
115
        /**
116
         * Creates the schema header File 
117
         * @return
118
         */
119
        private String getInitFile(){
120
                StringBuffer string = new StringBuffer();
121
                string.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
122
                string.append("<xs:schema targetNamespace=\"" + TARGET_NAMESPACE + "\" ");
123
                string.append("xmlns:" + NAMESPACE + "=\"" + TARGET_NAMESPACE + "\" ");
124
                string.append("xmlns:gml=\"http://www.opengis.net/gml\" ");
125
                string.append("xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ");
126
                string.append("elementFormDefault=\"qualified\" ");
127
                string.append("attributeFormDefault=\"unqualified\" version=\"" + GML_VERSION + "\">");
128
                string.append("<xs:import namespace=\"http://www.opengis.net/gml\" ");
129
                string.append("schemaLocation=\"feature.xsd\"/");
130
                string.append(">");
131
                return string.toString();
132
        }
133
        
134

    
135
        /**
136
         * Creates the GML File last line
137
         * @return
138
         */
139
        private String getEndFile(){
140
                StringBuffer string = new StringBuffer();
141
                string.append("</xs:schema>");
142
                return string.toString();
143
        }
144
        
145
        /**
146
         * Creates the complex types header
147
         * @return
148
         */
149
        private String getInitComplexTypes(){
150
                StringBuffer string = new StringBuffer();
151
                string.append("<xs:complexType xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ");
152
                string.append("name=\"" + getFeatureType() + "\">");
153
                string.append("<xs:complexContent>");
154
        string.append("<xs:extension base=\"gml:AbstractFeatureType\">");
155
        string.append("<xs:sequence>");
156
                return string.toString();                
157
        }
158
        
159
        /**
160
         * Creates the complex types end tag
161
         * @return
162
         */
163
        private String getEndComplexTypes(){
164
                StringBuffer string = new StringBuffer();
165
                string.append("</xs:sequence>");
166
                string.append("</xs:extension>");
167
                string.append("</xs:complexContent>");
168
                string.append("</xs:complexType>");
169
                string.append("<xs:element name=\"" + getFeatureName() + "\" ");
170
                string.append("type=\"" + NAMESPACE + ":" + getFeatureType() + "\" ");
171
                string.append("substitutionGroup=\"" + GML_SUBSTITUTION_GROUP + "\"/>");
172
                return string.toString();                
173
        }
174
        
175
        /**
176
         * Creates the attribute description
177
         * @param fieldDescription
178
         * @return
179
         */
180
        private String getAttribute(FieldDescription fieldDescription){
181
                return getAttribute(fieldDescription.getFieldName(),
182
                                GMLTypesConversor.gvSIGToXlinkTypes(fieldDescription.getFieldType()));
183
        }        
184
        
185
        /**
186
         * Creates the attribute description
187
         * @param attributeName
188
         * Attribute name
189
         * @param attributeType
190
         * Attribute Type
191
         * @return
192
         */
193
        private String getAttribute(String attributeName, String attributeType){
194
                StringBuffer string = new StringBuffer();
195
                string.append("<xs:element name=\"" + attributeName + "\" ");
196
                string.append("minOccurs=\"0\" ");
197
                string.append("nillable=\"true\" ");
198
                string.append("type=\"" + attributeType + "\"/>");
199
                return string.toString();
200
        }
201

    
202
        /**
203
         * @return Returns the featureName.
204
         */
205
        public static String getFeatureName() {
206
                return featureName;
207
        }
208

    
209
        /**
210
         * @return Returns the featureType.
211
         */
212
        public static String getFeatureType() {
213
                return featureType;
214
        }
215
                   
216

    
217
    
218

    
219

    
220
        
221

    
222
}