v1.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/OWSCommon.js
  7. */
  8. /**
  9. * Class: OpenLayers.Format.OWSCommon.v1
  10. * Common readers and writers for OWSCommon v1.X formats
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Format.XML>
  14. */
  15. OpenLayers.Format.OWSCommon.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
  16. /**
  17. * Property: regExes
  18. * Compiled regular expressions for manipulating strings.
  19. */
  20. regExes: {
  21. trimSpace: (/^\s*|\s*$/g),
  22. removeSpace: (/\s*/g),
  23. splitSpace: (/\s+/),
  24. trimComma: (/\s*,\s*/g)
  25. },
  26. /**
  27. * Method: read
  28. *
  29. * Parameters:
  30. * data - {DOMElement} An OWSCommon document element.
  31. * options - {Object} Options for the reader.
  32. *
  33. * Returns:
  34. * {Object} An object representing the OWSCommon document.
  35. */
  36. read: function(data, options) {
  37. options = OpenLayers.Util.applyDefaults(options, this.options);
  38. var ows = {};
  39. this.readChildNodes(data, ows);
  40. return ows;
  41. },
  42. /**
  43. * Property: readers
  44. * Contains public functions, grouped by namespace prefix, that will
  45. * be applied when a namespaced node is found matching the function
  46. * name. The function will be applied in the scope of this parser
  47. * with two arguments: the node being read and a context object passed
  48. * from the parent.
  49. */
  50. readers: {
  51. "ows": {
  52. "Exception": function(node, exceptionReport) {
  53. var exception = {
  54. code: node.getAttribute('exceptionCode'),
  55. locator: node.getAttribute('locator'),
  56. texts: []
  57. };
  58. exceptionReport.exceptions.push(exception);
  59. this.readChildNodes(node, exception);
  60. },
  61. "ExceptionText": function(node, exception) {
  62. var text = this.getChildValue(node);
  63. exception.texts.push(text);
  64. },
  65. "ServiceIdentification": function(node, obj) {
  66. obj.serviceIdentification = {};
  67. this.readChildNodes(node, obj.serviceIdentification);
  68. },
  69. "Title": function(node, obj) {
  70. obj.title = this.getChildValue(node);
  71. },
  72. "Abstract": function(node, serviceIdentification) {
  73. serviceIdentification["abstract"] = this.getChildValue(node);
  74. },
  75. "Keywords": function(node, serviceIdentification) {
  76. serviceIdentification.keywords = {};
  77. this.readChildNodes(node, serviceIdentification.keywords);
  78. },
  79. "Keyword": function(node, keywords) {
  80. keywords[this.getChildValue(node)] = true;
  81. },
  82. "ServiceType": function(node, serviceIdentification) {
  83. serviceIdentification.serviceType = {
  84. codeSpace: node.getAttribute('codeSpace'),
  85. value: this.getChildValue(node)};
  86. },
  87. "ServiceTypeVersion": function(node, serviceIdentification) {
  88. serviceIdentification.serviceTypeVersion = this.getChildValue(node);
  89. },
  90. "Fees": function(node, serviceIdentification) {
  91. serviceIdentification.fees = this.getChildValue(node);
  92. },
  93. "AccessConstraints": function(node, serviceIdentification) {
  94. serviceIdentification.accessConstraints =
  95. this.getChildValue(node);
  96. },
  97. "ServiceProvider": function(node, obj) {
  98. obj.serviceProvider = {};
  99. this.readChildNodes(node, obj.serviceProvider);
  100. },
  101. "ProviderName": function(node, serviceProvider) {
  102. serviceProvider.providerName = this.getChildValue(node);
  103. },
  104. "ProviderSite": function(node, serviceProvider) {
  105. serviceProvider.providerSite = this.getAttributeNS(node,
  106. this.namespaces.xlink, "href");
  107. },
  108. "ServiceContact": function(node, serviceProvider) {
  109. serviceProvider.serviceContact = {};
  110. this.readChildNodes(node, serviceProvider.serviceContact);
  111. },
  112. "IndividualName": function(node, serviceContact) {
  113. serviceContact.individualName = this.getChildValue(node);
  114. },
  115. "PositionName": function(node, serviceContact) {
  116. serviceContact.positionName = this.getChildValue(node);
  117. },
  118. "ContactInfo": function(node, serviceContact) {
  119. serviceContact.contactInfo = {};
  120. this.readChildNodes(node, serviceContact.contactInfo);
  121. },
  122. "Phone": function(node, contactInfo) {
  123. contactInfo.phone = {};
  124. this.readChildNodes(node, contactInfo.phone);
  125. },
  126. "Voice": function(node, phone) {
  127. phone.voice = this.getChildValue(node);
  128. },
  129. "Address": function(node, contactInfo) {
  130. contactInfo.address = {};
  131. this.readChildNodes(node, contactInfo.address);
  132. },
  133. "DeliveryPoint": function(node, address) {
  134. address.deliveryPoint = this.getChildValue(node);
  135. },
  136. "City": function(node, address) {
  137. address.city = this.getChildValue(node);
  138. },
  139. "AdministrativeArea": function(node, address) {
  140. address.administrativeArea = this.getChildValue(node);
  141. },
  142. "PostalCode": function(node, address) {
  143. address.postalCode = this.getChildValue(node);
  144. },
  145. "Country": function(node, address) {
  146. address.country = this.getChildValue(node);
  147. },
  148. "ElectronicMailAddress": function(node, address) {
  149. address.electronicMailAddress = this.getChildValue(node);
  150. },
  151. "Role": function(node, serviceContact) {
  152. serviceContact.role = this.getChildValue(node);
  153. },
  154. "OperationsMetadata": function(node, obj) {
  155. obj.operationsMetadata = {};
  156. this.readChildNodes(node, obj.operationsMetadata);
  157. },
  158. "Operation": function(node, operationsMetadata) {
  159. var name = node.getAttribute("name");
  160. operationsMetadata[name] = {};
  161. this.readChildNodes(node, operationsMetadata[name]);
  162. },
  163. "DCP": function(node, operation) {
  164. operation.dcp = {};
  165. this.readChildNodes(node, operation.dcp);
  166. },
  167. "HTTP": function(node, dcp) {
  168. dcp.http = {};
  169. this.readChildNodes(node, dcp.http);
  170. },
  171. "Get": function(node, http) {
  172. if (!http.get) {
  173. http.get = [];
  174. }
  175. var obj = {
  176. url: this.getAttributeNS(node, this.namespaces.xlink, "href")
  177. };
  178. this.readChildNodes(node, obj);
  179. http.get.push(obj);
  180. },
  181. "Post": function(node, http) {
  182. if (!http.post) {
  183. http.post = [];
  184. }
  185. var obj = {
  186. url: this.getAttributeNS(node, this.namespaces.xlink, "href")
  187. };
  188. this.readChildNodes(node, obj);
  189. http.post.push(obj);
  190. },
  191. "Parameter": function(node, operation) {
  192. if (!operation.parameters) {
  193. operation.parameters = {};
  194. }
  195. var name = node.getAttribute("name");
  196. operation.parameters[name] = {};
  197. this.readChildNodes(node, operation.parameters[name]);
  198. },
  199. "Constraint": function(node, obj) {
  200. if (!obj.constraints) {
  201. obj.constraints = {};
  202. }
  203. var name = node.getAttribute("name");
  204. obj.constraints[name] = {};
  205. this.readChildNodes(node, obj.constraints[name]);
  206. },
  207. "Value": function(node, allowedValues) {
  208. allowedValues[this.getChildValue(node)] = true;
  209. },
  210. "OutputFormat": function(node, obj) {
  211. obj.formats.push({value: this.getChildValue(node)});
  212. this.readChildNodes(node, obj);
  213. },
  214. "WGS84BoundingBox": function(node, obj) {
  215. var boundingBox = {};
  216. boundingBox.crs = node.getAttribute("crs");
  217. if (obj.BoundingBox) {
  218. obj.BoundingBox.push(boundingBox);
  219. } else {
  220. obj.projection = boundingBox.crs;
  221. boundingBox = obj;
  222. }
  223. this.readChildNodes(node, boundingBox);
  224. },
  225. "BoundingBox": function(node, obj) {
  226. // FIXME: We consider that BoundingBox is the same as WGS84BoundingBox
  227. // LowerCorner = "min_x min_y"
  228. // UpperCorner = "max_x max_y"
  229. // It should normally depend on the projection
  230. this.readers['ows']['WGS84BoundingBox'].apply(this, [node, obj]);
  231. },
  232. "LowerCorner": function(node, obj) {
  233. var str = this.getChildValue(node).replace(
  234. this.regExes.trimSpace, "");
  235. str = str.replace(this.regExes.trimComma, ",");
  236. var pointList = str.split(this.regExes.splitSpace);
  237. obj.left = pointList[0];
  238. obj.bottom = pointList[1];
  239. },
  240. "UpperCorner": function(node, obj) {
  241. var str = this.getChildValue(node).replace(
  242. this.regExes.trimSpace, "");
  243. str = str.replace(this.regExes.trimComma, ",");
  244. var pointList = str.split(this.regExes.splitSpace);
  245. obj.right = pointList[0];
  246. obj.top = pointList[1];
  247. obj.bounds = new OpenLayers.Bounds(obj.left, obj.bottom,
  248. obj.right, obj.top);
  249. delete obj.left;
  250. delete obj.bottom;
  251. delete obj.right;
  252. delete obj.top;
  253. },
  254. "Language": function(node, obj) {
  255. obj.language = this.getChildValue(node);
  256. }
  257. }
  258. },
  259. /**
  260. * Property: writers
  261. * As a compliment to the readers property, this structure contains public
  262. * writing functions grouped by namespace alias and named like the
  263. * node names they produce.
  264. */
  265. writers: {
  266. "ows": {
  267. "BoundingBox": function(options, nodeName) {
  268. var node = this.createElementNSPlus(nodeName || "ows:BoundingBox", {
  269. attributes: {
  270. crs: options.projection
  271. }
  272. });
  273. this.writeNode("ows:LowerCorner", options, node);
  274. this.writeNode("ows:UpperCorner", options, node);
  275. return node;
  276. },
  277. "LowerCorner": function(options) {
  278. var node = this.createElementNSPlus("ows:LowerCorner", {
  279. value: options.bounds.left + " " + options.bounds.bottom });
  280. return node;
  281. },
  282. "UpperCorner": function(options) {
  283. var node = this.createElementNSPlus("ows:UpperCorner", {
  284. value: options.bounds.right + " " + options.bounds.top });
  285. return node;
  286. },
  287. "Identifier": function(identifier) {
  288. var node = this.createElementNSPlus("ows:Identifier", {
  289. value: identifier });
  290. return node;
  291. },
  292. "Title": function(title) {
  293. var node = this.createElementNSPlus("ows:Title", {
  294. value: title });
  295. return node;
  296. },
  297. "Abstract": function(abstractValue) {
  298. var node = this.createElementNSPlus("ows:Abstract", {
  299. value: abstractValue });
  300. return node;
  301. },
  302. "OutputFormat": function(format) {
  303. var node = this.createElementNSPlus("ows:OutputFormat", {
  304. value: format });
  305. return node;
  306. }
  307. }
  308. },
  309. CLASS_NAME: "OpenLayers.Format.OWSCommon.v1"
  310. });