]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/WFSDescribeFeatureType.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / WFSDescribeFeatureType.js
1 /**
2  * @requires OpenLayers/Format/XML.js
3  *
4  * Class: OpenLayers.Format.WFSDescribeFeatureType
5  * Read WFS DescribeFeatureType response
6  * 
7  * Inherits from:
8  *  - <OpenLayers.Format.XML>
9  */
10 OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(
11     OpenLayers.Format.XML, {
12     
13     /**
14      * Property: namespaces
15      * {Object} Mapping of namespace aliases to namespace URIs.
16      */
17     namespaces: {
18         xsd: "http://www.w3.org/2001/XMLSchema"
19     },
20     
21     /**
22      * Constructor: OpenLayers.Format.WFSDescribeFeatureType
23      * Create a new parser for WFS DescribeFeatureType responses.
24      *
25      * Parameters:
26      * options - {Object} An optional object whose properties will be set on
27      *     this instance.
28      */
29     initialize: function(options) {
30         OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
31     },
32     
33     /**
34      * Property: readers
35      * Contains public functions, grouped by namespace prefix, that will
36      *     be applied when a namespaced node is found matching the function
37      *     name.  The function will be applied in the scope of this parser
38      *     with two arguments: the node being read and a context object passed
39      *     from the parent.
40      */
41     readers: {
42         "xsd": {
43             "schema": function(node, obj) {
44                 var complexTypes = [];
45                 var customTypes = {};
46                 var schema = {
47                     complexTypes: complexTypes,
48                     customTypes: customTypes
49                 };
50                 
51                 this.readChildNodes(node, schema);
52
53                 var attributes = node.attributes;
54                 var attr, name;
55                 for(var i=0, len=attributes.length; i<len; ++i) {
56                     attr = attributes[i];
57                     name = attr.name;
58                     if(name.indexOf("xmlns") == 0) {
59                         this.setNamespace(name.split(":")[1] || "", attr.value);
60                     } else {
61                         obj[name] = attr.value;
62                     }
63                 }
64                 obj.featureTypes = complexTypes;                
65                 obj.targetPrefix = this.namespaceAlias[obj.targetNamespace];
66                 
67                 // map complexTypes to names of customTypes
68                 var complexType, customType;
69                 for(var i=0, len=complexTypes.length; i<len; ++i) {
70                     complexType = complexTypes[i];
71                     customType = customTypes[complexType.typeName];
72                     if(customTypes[complexType.typeName]) {
73                         complexType.typeName = customType.name;
74                     }
75                 }
76             },
77             "complexType": function(node, obj) {
78                 var complexType = {
79                     // this is a temporary typeName, it will be overwritten by
80                     // the schema reader with the metadata found in the
81                     // customTypes hash
82                     "typeName": node.getAttribute("name")
83                 };
84                 this.readChildNodes(node, complexType);
85                 obj.complexTypes.push(complexType);
86             },
87             "complexContent": function(node, obj) {
88                 this.readChildNodes(node, obj);
89             },
90             "extension": function(node, obj) {
91                 this.readChildNodes(node, obj);
92             },
93             "sequence": function(node, obj) {
94                 var sequence = {
95                     elements: []
96                 };
97                 this.readChildNodes(node, sequence);
98                 obj.properties = sequence.elements;
99             },
100             "element": function(node, obj) {
101                 if(obj.elements) {
102                     var element = {};
103                     var attributes = node.attributes;
104                     var attr;
105                     for(var i=0, len=attributes.length; i<len; ++i) {
106                         attr = attributes[i];
107                         element[attr.name] = attr.value;
108                     }
109                     
110                     var type = element.type;
111                     if(!type) {
112                         type = {};
113                         this.readChildNodes(node, type);
114                         element.restriction = type;
115                         element.type = type.base;
116                     }
117                     var fullType = type.base || type;
118                     element.localType = fullType.split(":").pop();
119                     obj.elements.push(element);
120                 }
121                 
122                 if(obj.complexTypes) {
123                     var type = node.getAttribute("type");
124                     var localType = type.split(":").pop();
125                     obj.customTypes[localType] = {
126                         "name": node.getAttribute("name"),
127                         "type": type
128                     };
129                 }
130             },
131             "simpleType": function(node, obj) {
132                 this.readChildNodes(node, obj);
133             },
134             "restriction": function(node, obj) {
135                 obj.base = node.getAttribute("base");
136                 this.readRestriction(node, obj);
137             }
138         }
139     },
140     
141     /**
142      * Method: readRestriction
143      * Reads restriction defined in the child nodes of a restriction element
144      * 
145      * Parameters:
146      * node {DOMElement} - the node to parse
147      * obj {Object} - the object that receives the read result
148      */
149     readRestriction: function(node, obj) {
150         var children = node.childNodes;
151         var child, nodeName, value;
152         for(var i=0, len=children.length; i<len; ++i) {
153             child = children[i];
154             if(child.nodeType == 1) {
155                 nodeName = child.nodeName.split(":").pop();
156                 value = child.getAttribute("value");
157                 if(!obj[nodeName]) {
158                     obj[nodeName] = value;
159                 } else {
160                     if(typeof obj[nodeName] == "string") {
161                         obj[nodeName] = [obj[nodeName]];
162                     }
163                     obj[nodeName].push(value);
164                 }
165             }
166         }
167     },
168     
169     /**
170      * Method: read
171      *
172      * Parameters:
173      * data - {DOMElement|String} A WFS DescribeFeatureType document.
174      *
175      * Returns:
176      * {Object} An object representing the WFS DescribeFeatureType response.
177      */
178     read: function(data) {
179         if(typeof data == "string") { 
180             data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
181         }
182         if(data && data.nodeType == 9) {
183             data = data.documentElement;
184         }
185         var schema = {};
186         this.readNode(data, schema);
187         
188         return schema;
189     },
190     
191     CLASS_NAME: "OpenLayers.Format.WFSDescribeFeatureType" 
192
193 });