v1.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. * @requires OpenLayers/Format/XLS.js
  7. * @requires OpenLayers/Format/GML/v3.js
  8. */
  9. /**
  10. * Class: OpenLayers.Format.XLS.v1
  11. * Superclass for XLS version 1 parsers. Only supports GeocodeRequest for now.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Format.XML>
  15. */
  16. OpenLayers.Format.XLS.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
  17. /**
  18. * Property: namespaces
  19. * {Object} Mapping of namespace aliases to namespace URIs.
  20. */
  21. namespaces: {
  22. xls: "http://www.opengis.net/xls",
  23. gml: "http://www.opengis.net/gml",
  24. xsi: "http://www.w3.org/2001/XMLSchema-instance"
  25. },
  26. /**
  27. * Property: regExes
  28. * Compiled regular expressions for manipulating strings.
  29. */
  30. regExes: {
  31. trimSpace: (/^\s*|\s*$/g),
  32. removeSpace: (/\s*/g),
  33. splitSpace: (/\s+/),
  34. trimComma: (/\s*,\s*/g)
  35. },
  36. /**
  37. * APIProperty: xy
  38. * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x)
  39. * Changing is not recommended, a new Format should be instantiated.
  40. */
  41. xy: true,
  42. /**
  43. * Property: defaultPrefix
  44. */
  45. defaultPrefix: "xls",
  46. /**
  47. * Property: schemaLocation
  48. * {String} Schema location for a particular minor version.
  49. */
  50. schemaLocation: null,
  51. /**
  52. * Constructor: OpenLayers.Format.XLS.v1
  53. * Instances of this class are not created directly. Use the
  54. * <OpenLayers.Format.XLS> constructor instead.
  55. *
  56. * Parameters:
  57. * options - {Object} An optional object whose properties will be set on
  58. * this instance.
  59. */
  60. /**
  61. * Method: read
  62. *
  63. * Parameters:
  64. * data - {DOMElement} An XLS document element.
  65. * options - {Object} Options for the reader.
  66. *
  67. * Returns:
  68. * {Object} An object representing the XLSResponse.
  69. */
  70. read: function(data, options) {
  71. options = OpenLayers.Util.applyDefaults(options, this.options);
  72. var xls = {};
  73. this.readChildNodes(data, xls);
  74. return xls;
  75. },
  76. /**
  77. * Property: readers
  78. * Contains public functions, grouped by namespace prefix, that will
  79. * be applied when a namespaced node is found matching the function
  80. * name. The function will be applied in the scope of this parser
  81. * with two arguments: the node being read and a context object passed
  82. * from the parent.
  83. */
  84. readers: {
  85. "xls": {
  86. "XLS": function(node, xls) {
  87. xls.version = node.getAttribute("version");
  88. this.readChildNodes(node, xls);
  89. },
  90. "Response": function(node, xls) {
  91. this.readChildNodes(node, xls);
  92. },
  93. "GeocodeResponse": function(node, xls) {
  94. xls.responseLists = [];
  95. this.readChildNodes(node, xls);
  96. },
  97. "GeocodeResponseList": function(node, xls) {
  98. var responseList = {
  99. features: [],
  100. numberOfGeocodedAddresses:
  101. parseInt(node.getAttribute("numberOfGeocodedAddresses"))
  102. };
  103. xls.responseLists.push(responseList);
  104. this.readChildNodes(node, responseList);
  105. },
  106. "GeocodedAddress": function(node, responseList) {
  107. var feature = new OpenLayers.Feature.Vector();
  108. responseList.features.push(feature);
  109. this.readChildNodes(node, feature);
  110. // post-process geometry
  111. feature.geometry = feature.components[0];
  112. },
  113. "GeocodeMatchCode": function(node, feature) {
  114. feature.attributes.matchCode = {
  115. accuracy: parseFloat(node.getAttribute("accuracy")),
  116. matchType: node.getAttribute("matchType")
  117. };
  118. },
  119. "Address": function(node, feature) {
  120. var address = {
  121. countryCode: node.getAttribute("countryCode"),
  122. addressee: node.getAttribute("addressee"),
  123. street: [],
  124. place: []
  125. };
  126. feature.attributes.address = address;
  127. this.readChildNodes(node, address);
  128. },
  129. "freeFormAddress": function(node, address) {
  130. address.freeFormAddress = this.getChildValue(node);
  131. },
  132. "StreetAddress": function(node, address) {
  133. this.readChildNodes(node, address);
  134. },
  135. "Building": function(node, address) {
  136. address.building = {
  137. 'number': node.getAttribute("number"),
  138. subdivision: node.getAttribute("subdivision"),
  139. buildingName: node.getAttribute("buildingName")
  140. };
  141. },
  142. "Street": function(node, address) {
  143. // only support the built-in primitive type for now
  144. address.street.push(this.getChildValue(node));
  145. },
  146. "Place": function(node, address) {
  147. // type is one of CountrySubdivision,
  148. // CountrySecondarySubdivision, Municipality or
  149. // MunicipalitySubdivision
  150. address.place[node.getAttribute("type")] =
  151. this.getChildValue(node);
  152. },
  153. "PostalCode": function(node, address) {
  154. address.postalCode = this.getChildValue(node);
  155. }
  156. },
  157. "gml": OpenLayers.Format.GML.v3.prototype.readers.gml
  158. },
  159. /**
  160. * Method: write
  161. *
  162. * Parameters:
  163. * request - {Object} An object representing the geocode request.
  164. *
  165. * Returns:
  166. * {DOMElement} The root of an XLS document.
  167. */
  168. write: function(request) {
  169. return this.writers.xls.XLS.apply(this, [request]);
  170. },
  171. /**
  172. * Property: writers
  173. * As a compliment to the readers property, this structure contains public
  174. * writing functions grouped by namespace alias and named like the
  175. * node names they produce.
  176. */
  177. writers: {
  178. "xls": {
  179. "XLS": function(request) {
  180. var root = this.createElementNSPlus(
  181. "xls:XLS",
  182. {attributes: {
  183. "version": this.VERSION,
  184. "xsi:schemaLocation": this.schemaLocation
  185. }}
  186. );
  187. this.writeNode("RequestHeader", request.header, root);
  188. this.writeNode("Request", request, root);
  189. return root;
  190. },
  191. "RequestHeader": function(header) {
  192. return this.createElementNSPlus("xls:RequestHeader");
  193. },
  194. "Request": function(request) {
  195. var node = this.createElementNSPlus("xls:Request", {
  196. attributes: {
  197. methodName: "GeocodeRequest",
  198. requestID: request.requestID || "",
  199. version: this.VERSION
  200. }
  201. });
  202. this.writeNode("GeocodeRequest", request.addresses, node);
  203. return node;
  204. },
  205. "GeocodeRequest": function(addresses) {
  206. var node = this.createElementNSPlus("xls:GeocodeRequest");
  207. for (var i=0, len=addresses.length; i<len; i++) {
  208. this.writeNode("Address", addresses[i], node);
  209. }
  210. return node;
  211. },
  212. "Address": function(address) {
  213. var node = this.createElementNSPlus("xls:Address", {
  214. attributes: {
  215. countryCode: address.countryCode
  216. }
  217. });
  218. if (address.freeFormAddress) {
  219. this.writeNode("freeFormAddress", address.freeFormAddress, node);
  220. } else {
  221. if (address.street) {
  222. this.writeNode("StreetAddress", address, node);
  223. }
  224. if (address.municipality) {
  225. this.writeNode("Municipality", address.municipality, node);
  226. }
  227. if (address.countrySubdivision) {
  228. this.writeNode("CountrySubdivision", address.countrySubdivision, node);
  229. }
  230. if (address.postalCode) {
  231. this.writeNode("PostalCode", address.postalCode, node);
  232. }
  233. }
  234. return node;
  235. },
  236. "freeFormAddress": function(freeFormAddress) {
  237. return this.createElementNSPlus("freeFormAddress",
  238. {value: freeFormAddress});
  239. },
  240. "StreetAddress": function(address) {
  241. var node = this.createElementNSPlus("xls:StreetAddress");
  242. if (address.building) {
  243. this.writeNode(node, "Building", address.building);
  244. }
  245. var street = address.street;
  246. if (!(OpenLayers.Util.isArray(street))) {
  247. street = [street];
  248. }
  249. for (var i=0, len=street.length; i < len; i++) {
  250. this.writeNode("Street", street[i], node);
  251. }
  252. return node;
  253. },
  254. "Building": function(building) {
  255. return this.createElementNSPlus("xls:Building", {
  256. attributes: {
  257. "number": building["number"],
  258. "subdivision": building.subdivision,
  259. "buildingName": building.buildingName
  260. }
  261. });
  262. },
  263. "Street": function(street) {
  264. return this.createElementNSPlus("xls:Street", {value: street});
  265. },
  266. "Municipality": function(municipality) {
  267. return this.createElementNSPlus("xls:Place", {
  268. attributes: {
  269. type: "Municipality"
  270. },
  271. value: municipality
  272. });
  273. },
  274. "CountrySubdivision": function(countrySubdivision) {
  275. return this.createElementNSPlus("xls:Place", {
  276. attributes: {
  277. type: "CountrySubdivision"
  278. },
  279. value: countrySubdivision
  280. });
  281. },
  282. "PostalCode": function(postalCode) {
  283. return this.createElementNSPlus("xls:PostalCode", {
  284. value: postalCode
  285. });
  286. }
  287. }
  288. },
  289. CLASS_NAME: "OpenLayers.Format.XLS.v1"
  290. });