Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2065 / extensions / extWFS2 / src / org / gvsig / fmap / dal / store / wfs / WFSFeatureFiller.java @ 40276

History | View | Annotate | Download (7.02 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.fmap.dal.store.wfs;
23

    
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileNotFoundException;
27
import java.util.Iterator;
28

    
29
import org.gvsig.remoteclient.wfs.WFSFeature;
30
import org.gvsig.tools.dataTypes.DataType;
31
import org.gvsig.xmlschema.lib.api.XMLSchemaLocator;
32
import org.gvsig.xmlschema.lib.api.XMLSchemaManager;
33
import org.gvsig.xmlschema.lib.api.exceptions.SchemaCreationException;
34
import org.gvsig.xmlschema.lib.api.som.IXSComplexContent;
35
import org.gvsig.xmlschema.lib.api.som.IXSComplexTypeDefinition;
36
import org.gvsig.xmlschema.lib.api.som.IXSContentType;
37
import org.gvsig.xmlschema.lib.api.som.IXSElementDeclaration;
38
import org.gvsig.xmlschema.lib.api.som.IXSExtension;
39
import org.gvsig.xmlschema.lib.api.som.IXSGroup;
40
import org.gvsig.xmlschema.lib.api.som.IXSRestriction;
41
import org.gvsig.xmlschema.lib.api.som.IXSSchema;
42
import org.gvsig.xmlschema.lib.api.som.IXSSimpleContent;
43
import org.gvsig.xmlschema.lib.api.som.IXSSimpleTypeDefinition;
44
import org.gvsig.xmlschema.lib.api.som.IXSTypeDefinition;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48

    
49
/**
50
 * @author gvSIG Team
51
 * @version $Id$
52
 *
53
 */
54
public class WFSFeatureFiller {
55
   private static final Logger LOG = LoggerFactory.getLogger(WFSFeatureFiller.class);
56
    private XMLSchemaManager xmlSchemaManager = null;
57
    private static final String XMLSCHEMA_PARSER_PROVIDER_NAME = "xmlschema.providers.kxml";
58
    private WFSFeature feature;
59
    
60
    public WFSFeatureFiller(WFSFeature feature) {
61
        super();
62
        xmlSchemaManager =  XMLSchemaLocator.getXMLSchemaManager();
63
        this.feature = feature;
64
    }
65

    
66
    /**
67
     * @param describeFeatureTypeFile
68
     * @param feature
69
     * @throws FileNotFoundException 
70
     * @throws SchemaCreationException 
71
     */
72
    public void fill(File describeFeatureTypeFile) throws SchemaCreationException, FileNotFoundException {
73
        IXSSchema schema = xmlSchemaManager.parse(XMLSCHEMA_PARSER_PROVIDER_NAME, new FileInputStream(describeFeatureTypeFile));
74
        IXSElementDeclaration elementDeclaration = null;
75
        if (feature.getNamespace() == null){
76
            elementDeclaration = schema.getElementDeclarationByName(null, feature.getName()); 
77
        }else{
78
            elementDeclaration = schema.getElementDeclarationByName(feature.getNamespace().getLocation(), feature.getLocalName());
79
        }
80
        if (elementDeclaration == null){
81
            return;
82
        }
83
        IXSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
84
        if (typeDefinition == null){
85
            return;
86
        }
87
        if (typeDefinition instanceof IXSSimpleTypeDefinition){
88
            
89
        }else if (typeDefinition instanceof IXSComplexTypeDefinition){ 
90
            fill((IXSComplexTypeDefinition)typeDefinition);
91
        }
92
        feature.setCompleted(true);
93
    }
94

    
95
    /**
96
     * @param typeDefinition
97
     */
98
    private void fill(IXSComplexTypeDefinition complexTypeDefinition) {
99
        IXSContentType contentType = complexTypeDefinition.getContentType();
100
        if (contentType != null){
101
            fill((IXSContentType)contentType);
102
        }
103
        IXSGroup group = complexTypeDefinition.getGroup();
104
        if (group != null){
105
            fill((IXSGroup)contentType);
106
        }        
107
    }
108

    
109
    /**
110
     * @param complexTypeDefinition
111
     */
112
    private void fill(IXSGroup group) {
113
        Iterator it = group.getItems().iterator();
114
        while (it.hasNext()){
115
            Object item = it.next();
116
            if (item instanceof IXSElementDeclaration){
117
                IXSElementDeclaration elementDeclaration = (IXSElementDeclaration)item;       
118
                DataType dataType = null;
119
                if (elementDeclaration.getTypeDefinition() != null){
120
                    dataType = elementDeclaration.getTypeDefinition().getDataType();
121
                }
122
                
123
                if (dataType == null || elementDeclaration.getNodeName() == null) {
124
                    LOG.info("Feature attribute type not recognized: "
125
                        + elementDeclaration.toString(),
126
                        new Exception("Feature attribute type not recognized: "
127
                        + elementDeclaration.toString()));
128
                } else {
129
                    feature.addField(elementDeclaration.getNodeName(), elementDeclaration.getTypeName(), dataType);
130
                }
131
                       
132
            }else {
133
                LOG.info("Feature attribute not recognized: "
134
                    + (item == null ? "NULL" : item.getClass().getName()),
135
                        new Exception("Feature attribute not recognized"));
136
            }               
137
        }      
138
    }
139

    
140
    /**
141
     * @param complexTypeDefinition
142
     */
143
    private void fill(IXSContentType contentType) {
144
        if (contentType instanceof IXSSimpleContent){
145
           fill((IXSSimpleContent)contentType);
146
        }else if (contentType instanceof IXSComplexContent){
147
            fill((IXSComplexContent)contentType);
148
        }else if (contentType instanceof IXSGroup){
149
           fill((IXSGroup)contentType);
150
        }        
151
    }
152
    
153
    private void fill(IXSSimpleContent simpleContent) {
154
        IXSRestriction restriction = simpleContent.getRestriction();
155
        if (restriction != null){
156
            fill(restriction);
157
        }
158
        IXSExtension extension = simpleContent.getExtension();
159
        if (extension != null){
160
            fill(extension);
161
        }
162
    }
163
    
164
    private void fill(IXSComplexContent complexContent) {
165
        IXSRestriction restriction = complexContent.getRestriction();
166
        if (restriction != null){
167
            fill(restriction);
168
        }
169
        IXSExtension extension = complexContent.getExtension();
170
        if (extension != null){
171
            fill(extension);
172
        }
173
    }
174

    
175
    /**
176
     * @param extension
177
     */
178
    private void fill(IXSExtension extension) {
179
        IXSGroup group = extension.getGroup();
180
        if (group != null){
181
            fill(group);
182
        }        
183
    }
184

    
185
    /**
186
     * @param restriction
187
     */
188
    private void fill(IXSRestriction restriction) {
189
        IXSGroup group = restriction.getGroup();
190
        if (group != null){
191
            fill(group);
192
        }            
193
    }
194
}