Project

General

Profile

Download (10.9 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/XLS.js
8
 * @requires OpenLayers/Format/GML/v3.js
9
 */
10

    
11
/**
12
 * Class: OpenLayers.Format.XLS.v1
13
 * Superclass for XLS version 1 parsers. Only supports GeocodeRequest for now.
14
 *
15
 * Inherits from:
16
 *  - <OpenLayers.Format.XML>
17
 */
18
OpenLayers.Format.XLS.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
19
    
20
    /**
21
     * Property: namespaces
22
     * {Object} Mapping of namespace aliases to namespace URIs.
23
     */
24
    namespaces: {
25
        xls: "http://www.opengis.net/xls",
26
        gml: "http://www.opengis.net/gml",
27
        xsi: "http://www.w3.org/2001/XMLSchema-instance"
28
    },
29

    
30
    /**
31
     * Property: regExes
32
     * Compiled regular expressions for manipulating strings.
33
     */
34
    regExes: {
35
        trimSpace: (/^\s*|\s*$/g),
36
        removeSpace: (/\s*/g),
37
        splitSpace: (/\s+/),
38
        trimComma: (/\s*,\s*/g)
39
    },
40

    
41
    /**
42
     * APIProperty: xy
43
     * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x)
44
     * Changing is not recommended, a new Format should be instantiated.
45
     */
46
    xy: true,
47
    
48
    /**
49
     * Property: defaultPrefix
50
     */
51
    defaultPrefix: "xls",
52

    
53
    /**
54
     * Property: schemaLocation
55
     * {String} Schema location for a particular minor version.
56
     */
57
    schemaLocation: null,
58
    
59
    /**
60
     * Constructor: OpenLayers.Format.XLS.v1
61
     * Instances of this class are not created directly.  Use the
62
     *     <OpenLayers.Format.XLS> constructor instead.
63
     *
64
     * Parameters:
65
     * options - {Object} An optional object whose properties will be set on
66
     *     this instance.
67
     */
68
    
69
    /**
70
     * Method: read
71
     *
72
     * Parameters:
73
     * data - {DOMElement} An XLS document element.
74
     * options - {Object} Options for the reader.
75
     *
76
     * Returns:
77
     * {Object} An object representing the XLSResponse.
78
     */
79
    read: function(data, options) {
80
        options = OpenLayers.Util.applyDefaults(options, this.options);
81
        var xls = {};
82
        this.readChildNodes(data, xls);
83
        return xls;
84
    },
85
    
86
    /**
87
     * Property: readers
88
     * Contains public functions, grouped by namespace prefix, that will
89
     *     be applied when a namespaced node is found matching the function
90
     *     name.  The function will be applied in the scope of this parser
91
     *     with two arguments: the node being read and a context object passed
92
     *     from the parent.
93
     */
94
    readers: {
95
        "xls": {
96
            "XLS": function(node, xls) {
97
                xls.version = node.getAttribute("version");
98
                this.readChildNodes(node, xls);
99
            },
100
            "Response": function(node, xls) {
101
               this.readChildNodes(node, xls);
102
            },
103
            "GeocodeResponse": function(node, xls) {
104
               xls.responseLists = [];
105
               this.readChildNodes(node, xls);
106
            },
107
            "GeocodeResponseList": function(node, xls) {
108
                var responseList = {
109
                    features: [], 
110
                    numberOfGeocodedAddresses: 
111
                        parseInt(node.getAttribute("numberOfGeocodedAddresses"))
112
                };
113
                xls.responseLists.push(responseList);
114
                this.readChildNodes(node, responseList);
115
            },
116
            "GeocodedAddress": function(node, responseList) {
117
                var feature = new OpenLayers.Feature.Vector();
118
                responseList.features.push(feature);
119
                this.readChildNodes(node, feature);
120
                // post-process geometry
121
                feature.geometry = feature.components[0];
122
            },
123
            "GeocodeMatchCode": function(node, feature) {
124
                feature.attributes.matchCode = {
125
                    accuracy: parseFloat(node.getAttribute("accuracy")),
126
                    matchType: node.getAttribute("matchType")
127
                };
128
            },
129
            "Address": function(node, feature) {
130
                var address = {
131
                    countryCode: node.getAttribute("countryCode"),
132
                    addressee: node.getAttribute("addressee"),
133
                    street: [],
134
                    place: []
135
                };
136
                feature.attributes.address = address;
137
                this.readChildNodes(node, address);
138
            },
139
            "freeFormAddress": function(node, address) {
140
                address.freeFormAddress = this.getChildValue(node);
141
            },
142
            "StreetAddress": function(node, address) {
143
                this.readChildNodes(node, address);
144
            },
145
            "Building": function(node, address) {
146
                address.building = {
147
                    'number': node.getAttribute("number"),
148
                    subdivision: node.getAttribute("subdivision"),
149
                    buildingName: node.getAttribute("buildingName")
150
                };
151
            },
152
            "Street": function(node, address) {
153
                // only support the built-in primitive type for now
154
                address.street.push(this.getChildValue(node));
155
            },
156
            "Place": function(node, address) {
157
                // type is one of CountrySubdivision, 
158
                // CountrySecondarySubdivision, Municipality or
159
                // MunicipalitySubdivision
160
                address.place[node.getAttribute("type")] = 
161
                    this.getChildValue(node);
162
            },
163
            "PostalCode": function(node, address) {
164
                address.postalCode = this.getChildValue(node);
165
            }
166
        },
167
        "gml": OpenLayers.Format.GML.v3.prototype.readers.gml
168
    },
169
    
170
    /**
171
     * Method: write
172
     *
173
     * Parameters:
174
     * request - {Object} An object representing the geocode request.
175
     *
176
     * Returns:
177
     * {DOMElement} The root of an XLS document.
178
     */
179
    write: function(request) {
180
        return this.writers.xls.XLS.apply(this, [request]);
181
    },
182
    
183
    /**
184
     * Property: writers
185
     * As a compliment to the readers property, this structure contains public
186
     *     writing functions grouped by namespace alias and named like the
187
     *     node names they produce.
188
     */
189
    writers: {
190
        "xls": {
191
            "XLS": function(request) {
192
                var root = this.createElementNSPlus(
193
                    "xls:XLS",
194
                    {attributes: {
195
                        "version": this.VERSION,
196
                        "xsi:schemaLocation": this.schemaLocation
197
                    }}
198
                );
199
                this.writeNode("RequestHeader", request.header, root);
200
                this.writeNode("Request", request, root);
201
                return root;
202
            },
203
            "RequestHeader": function(header) {
204
                return this.createElementNSPlus("xls:RequestHeader");
205
            },
206
            "Request": function(request) {
207
                var node = this.createElementNSPlus("xls:Request", {
208
                    attributes: {
209
                        methodName: "GeocodeRequest",
210
                        requestID: request.requestID || "",
211
                        version: this.VERSION
212
                    }
213
                });
214
                this.writeNode("GeocodeRequest", request.addresses, node);
215
                return node;
216
            },
217
            "GeocodeRequest": function(addresses) {
218
                var node = this.createElementNSPlus("xls:GeocodeRequest");
219
                for (var i=0, len=addresses.length; i<len; i++) {
220
                    this.writeNode("Address", addresses[i], node);
221
                }
222
                return node;
223
            },
224
            "Address": function(address) {
225
                var node = this.createElementNSPlus("xls:Address", {
226
                    attributes: {
227
                        countryCode: address.countryCode
228
                    }
229
                });
230
                if (address.freeFormAddress) {
231
                    this.writeNode("freeFormAddress", address.freeFormAddress, node);
232
                } else {
233
                    if (address.street) {
234
                        this.writeNode("StreetAddress", address, node);
235
                    }
236
                    if (address.municipality) {
237
                        this.writeNode("Municipality", address.municipality, node);
238
                    }
239
                    if (address.countrySubdivision) {
240
                        this.writeNode("CountrySubdivision", address.countrySubdivision, node);
241
                    }
242
                    if (address.postalCode) {
243
                        this.writeNode("PostalCode", address.postalCode, node);
244
                    }
245
                }
246
                return node;
247
            },
248
            "freeFormAddress": function(freeFormAddress) {
249
                return this.createElementNSPlus("freeFormAddress", 
250
                    {value: freeFormAddress});
251
            },
252
            "StreetAddress": function(address) {
253
                var node = this.createElementNSPlus("xls:StreetAddress");
254
                if (address.building) {
255
                    this.writeNode(node, "Building", address.building);
256
                }
257
                var street = address.street;
258
                if (!(OpenLayers.Util.isArray(street))) {
259
                    street = [street];
260
                }
261
                for (var i=0, len=street.length; i < len; i++) {
262
                    this.writeNode("Street", street[i], node);
263
                }
264
                return node;
265
            },
266
            "Building": function(building) {
267
                return this.createElementNSPlus("xls:Building", {
268
                    attributes: {
269
                        "number": building["number"],
270
                        "subdivision": building.subdivision,
271
                        "buildingName": building.buildingName
272
                    }
273
                });
274
            },
275
            "Street": function(street) {
276
                return this.createElementNSPlus("xls:Street", {value: street});
277
            },
278
            "Municipality": function(municipality) {
279
                return this.createElementNSPlus("xls:Place", {
280
                    attributes: {
281
                        type: "Municipality"
282
                    },
283
                    value: municipality
284
                });
285
            },
286
            "CountrySubdivision": function(countrySubdivision) {
287
                return this.createElementNSPlus("xls:Place", {
288
                    attributes: {
289
                        type: "CountrySubdivision"
290
                    },
291
                    value: countrySubdivision
292
                });
293
            },
294
            "PostalCode": function(postalCode) {
295
                return this.createElementNSPlus("xls:PostalCode", {
296
                    value: postalCode
297
                });
298
            }
299
        }
300
    },
301
    
302
    CLASS_NAME: "OpenLayers.Format.XLS.v1" 
303

    
304
});
(1-1/2)