Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / gml / GMLSchemaCreator.java @ 6433

History | View | Annotate | Download (5.95 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 6433 2006-07-19 12:30:09Z jorpiell $
55
 * $Log$
56
 * Revision 1.1  2006-07-19 12:29:39  jorpiell
57
 * A?adido el driver de GML
58
 *
59
 *
60
 */
61
/**
62
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
63
 */
64
public class GMLSchemaCreator {
65
        public final static String GML_SUBSTITUTION_GROUP = "gml:_Feature";
66
        public final static String GML_GEOMETRY = "the_geom";                
67
        public final static String NAMESPACE = "cit";
68
        public final static String TARGET_NAMESPACE = "http://www.gvsig.com/cit";
69
        
70
        private static String featureName = null;
71
        private static String featureType = null;
72
        
73
        private Writer writer = null;
74
        
75
        
76
        public GMLSchemaCreator(File m_File) throws IOException {
77
                super();                
78
                writer = new FileWriter(m_File);
79
                featureName = m_File.getName().replaceAll("\\.xsd","");
80
                featureType = featureName + "_Type";
81
                
82
        }
83
        
84
        public void createFile(LayerDefinition lyrDef) throws IOException{
85
                writer.write(getInitFile());
86
                writer.write(getInitComplexTypes());
87
        
88
                writer.write(getAttribute(GML_GEOMETRY,
89
                                GMLTypesConversor.gvSIGToSchemaShapeTypesConversion(lyrDef.getShapeType())));                
90
                FieldDescription[] fieldDescription = lyrDef.getFieldsDesc();
91
                for (int i=0 ; i<fieldDescription.length ; i++){
92
                        writer.write(getAttribute(fieldDescription[i]));
93
                }                
94
                
95
                writer.write(getEndComplexTypes());
96
                writer.write(getEndFile());
97
        }        
98
        
99
        /**
100
         * Writes the schema file
101
         * @throws IOException 
102
         *
103
         */
104
        public void writeFile() throws IOException {
105
                writer.close();                
106
        }
107
        
108
        /**
109
         * Creates the schema header File 
110
         * @return
111
         */
112
        private String getInitFile(){
113
                StringBuffer string = new StringBuffer();
114
                string.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
115
                string.append("<xs:schema targetNamespace=\"" + TARGET_NAMESPACE + "\" ");
116
                string.append("xmlns:" + NAMESPACE + "=\"" + TARGET_NAMESPACE + "\" ");
117
                string.append("xmlns:gml=\"http://www.opengis.net/gml\" ");
118
                string.append("xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ");
119
                string.append("elementFormDefault=\"qualified\" ");
120
                string.append("attributeFormDefault=\"unqualified\" version=\"1.0\">");
121
                string.append("<xs:import namespace=\"http://www.opengis.net/gml\" ");
122
                string.append("schemaLocation=\"http://schemas.opengeospatial.net/gml/2.1.2/feature.xsd\"/");
123
                string.append(">");
124
                return string.toString();
125
        }
126
        
127

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

    
195
        /**
196
         * @return Returns the featureName.
197
         */
198
        public static String getFeatureName() {
199
                return featureName;
200
        }
201

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

    
210
    
211

    
212

    
213
        
214

    
215
}