Project

General

Profile

Download (8.16 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6
/**
7
 * @requires OpenLayers/Format/XML.js
8
 * @requires OpenLayers/Format/OGCExceptionReport.js
9
 */
10
 
11
/**
12
 * Class: OpenLayers.Format.WFSDescribeFeatureType
13
 * Read WFS DescribeFeatureType response
14
 * 
15
 * Inherits from:
16
 *  - <OpenLayers.Format.XML>
17
 */
18
OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
19
    OpenLayers.Format.XML, {
20

    
21
    /**
22
     * Property: regExes
23
     * Compiled regular expressions for manipulating strings.
24
     */
25
    regExes: {
26
        trimSpace: (/^\s*|\s*$/g)
27
    },
28
    
29
    /**
30
     * Property: namespaces
31
     * {Object} Mapping of namespace aliases to namespace URIs.
32
     */
33
    namespaces: {
34
        xsd: "http://www.w3.org/2001/XMLSchema"
35
    },
36
    
37
    /**
38
     * Constructor: OpenLayers.Format.WFSDescribeFeatureType
39
     * Create a new parser for WFS DescribeFeatureType responses.
40
     *
41
     * Parameters:
42
     * options - {Object} An optional object whose properties will be set on
43
     *     this instance.
44
     */
45
    
46
    /**
47
     * Property: readers
48
     * Contains public functions, grouped by namespace prefix, that will
49
     *     be applied when a namespaced node is found matching the function
50
     *     name.  The function will be applied in the scope of this parser
51
     *     with two arguments: the node being read and a context object passed
52
     *     from the parent.
53
     */
54
    readers: {
55
        "xsd": {
56
            "schema": function(node, obj) {
57
                var complexTypes = [];
58
                var customTypes = {};
59
                var schema = {
60
                    complexTypes: complexTypes,
61
                    customTypes: customTypes
62
                };
63
                var i, len;
64
                
65
                this.readChildNodes(node, schema);
66

    
67
                var attributes = node.attributes;
68
                var attr, name;
69
                for(i=0, len=attributes.length; i<len; ++i) {
70
                    attr = attributes[i];
71
                    name = attr.name;
72
                    if(name.indexOf("xmlns") === 0) {
73
                        this.setNamespace(name.split(":")[1] || "", attr.value);
74
                    } else {
75
                        obj[name] = attr.value;
76
                    }
77
                }
78
                obj.featureTypes = complexTypes;                
79
                obj.targetPrefix = this.namespaceAlias[obj.targetNamespace];
80
                
81
                // map complexTypes to names of customTypes
82
                var complexType, customType;
83
                for(i=0, len=complexTypes.length; i<len; ++i) {
84
                    complexType = complexTypes[i];
85
                    customType = customTypes[complexType.typeName];
86
                    if(customTypes[complexType.typeName]) {
87
                        complexType.typeName = customType.name;
88
                    }
89
                }
90
            },
91
            "complexType": function(node, obj) {
92
                var complexType = {
93
                    // this is a temporary typeName, it will be overwritten by
94
                    // the schema reader with the metadata found in the
95
                    // customTypes hash
96
                    "typeName": node.getAttribute("name")
97
                };
98
                this.readChildNodes(node, complexType);
99
                obj.complexTypes.push(complexType);
100
            },
101
            "complexContent": function(node, obj) {
102
                this.readChildNodes(node, obj);
103
            },
104
            "extension": function(node, obj) {
105
                this.readChildNodes(node, obj);
106
            },
107
            "sequence": function(node, obj) {
108
                var sequence = {
109
                    elements: []
110
                };
111
                this.readChildNodes(node, sequence);
112
                obj.properties = sequence.elements;
113
            },
114
            "element": function(node, obj) {
115
                var type;
116
                if(obj.elements) {
117
                    var element = {};
118
                    var attributes = node.attributes;
119
                    var attr;
120
                    for(var i=0, len=attributes.length; i<len; ++i) {
121
                        attr = attributes[i];
122
                        element[attr.name] = attr.value;
123
                    }
124
                    
125
                    type = element.type;
126
                    if(!type) {
127
                        type = {};
128
                        this.readChildNodes(node, type);
129
                        element.restriction = type;
130
                        element.type = type.base;
131
                    }
132
                    var fullType = type.base || type;
133
                    element.localType = fullType.split(":").pop();
134
                    obj.elements.push(element);
135
                    this.readChildNodes(node, element);
136
                }
137
                
138
                if(obj.complexTypes) {
139
                    type = node.getAttribute("type");
140
                    var localType = type.split(":").pop();
141
                    obj.customTypes[localType] = {
142
                        "name": node.getAttribute("name"),
143
                        "type": type
144
                    };
145
                }
146
            },
147
            "annotation": function(node, obj) {
148
                obj.annotation = {};
149
                this.readChildNodes(node, obj.annotation);
150
            },
151
            "appinfo": function(node, obj) {
152
                if (!obj.appinfo) {
153
                    obj.appinfo = [];
154
                }
155
                obj.appinfo.push(this.getChildValue(node));
156
            },
157
            "documentation": function(node, obj) {
158
                if (!obj.documentation) {
159
                    obj.documentation = [];
160
                }
161
                var value = this.getChildValue(node);
162
                obj.documentation.push({
163
                    lang: node.getAttribute("xml:lang"),
164
                    textContent: value.replace(this.regExes.trimSpace, "")
165
                });
166
            },
167
            "simpleType": function(node, obj) {
168
                this.readChildNodes(node, obj);
169
            },
170
            "restriction": function(node, obj) {
171
                obj.base = node.getAttribute("base");
172
                this.readRestriction(node, obj);
173
            }
174
        }
175
    },
176
    
177
    /**
178
     * Method: readRestriction
179
     * Reads restriction defined in the child nodes of a restriction element
180
     * 
181
     * Parameters:
182
     * node - {DOMElement} the node to parse
183
     * obj - {Object} the object that receives the read result
184
     */
185
    readRestriction: function(node, obj) {
186
        var children = node.childNodes;
187
        var child, nodeName, value;
188
        for(var i=0, len=children.length; i<len; ++i) {
189
            child = children[i];
190
            if(child.nodeType == 1) {
191
                nodeName = child.nodeName.split(":").pop();
192
                value = child.getAttribute("value");
193
                if(!obj[nodeName]) {
194
                    obj[nodeName] = value;
195
                } else {
196
                    if(typeof obj[nodeName] == "string") {
197
                        obj[nodeName] = [obj[nodeName]];
198
                    }
199
                    obj[nodeName].push(value);
200
                }
201
            }
202
        }
203
    },
204
    
205
    /**
206
     * Method: read
207
     *
208
     * Parameters:
209
     * data - {DOMElement|String} A WFS DescribeFeatureType document.
210
     *
211
     * Returns:
212
     * {Object} An object representing the WFS DescribeFeatureType response.
213
     */
214
    read: function(data) {
215
        if(typeof data == "string") { 
216
            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
217
        }
218
        if(data && data.nodeType == 9) {
219
            data = data.documentElement;
220
        }
221
        var schema = {};
222
        if (data.nodeName.split(":").pop() === 'ExceptionReport') {
223
            // an exception must have occurred, so parse it
224
            var parser = new OpenLayers.Format.OGCExceptionReport();
225
            schema.error = parser.read(data);
226
        } else {
227
            this.readNode(data, schema);
228
        }
229
        return schema;
230
    },
231
    
232
    CLASS_NAME: "OpenLayers.Format.WFSDescribeFeatureType" 
233

    
234
});
(29-29/41)