VersionedOGC.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/XML.js
  7. * @requires OpenLayers/Format/OGCExceptionReport.js
  8. */
  9. /**
  10. * Class: OpenLayers.Format.XML.VersionedOGC
  11. * Base class for versioned formats, i.e. a format which supports multiple
  12. * versions.
  13. *
  14. * To enable checking if parsing succeeded, you will need to define a property
  15. * called errorProperty on the parser you want to check. The parser will then
  16. * check the returned object to see if that property is present. If it is, it
  17. * assumes the parsing was successful. If it is not present (or is null), it will
  18. * pass the document through an OGCExceptionReport parser.
  19. *
  20. * If errorProperty is undefined for the parser, this error checking mechanism
  21. * will be disabled.
  22. *
  23. *
  24. *
  25. * Inherits from:
  26. * - <OpenLayers.Format.XML>
  27. */
  28. OpenLayers.Format.XML.VersionedOGC = OpenLayers.Class(OpenLayers.Format.XML, {
  29. /**
  30. * APIProperty: defaultVersion
  31. * {String} Version number to assume if none found.
  32. */
  33. defaultVersion: null,
  34. /**
  35. * APIProperty: version
  36. * {String} Specify a version string if one is known.
  37. */
  38. version: null,
  39. /**
  40. * APIProperty: profile
  41. * {String} If provided, use a custom profile.
  42. */
  43. profile: null,
  44. /**
  45. * APIProperty: allowFallback
  46. * {Boolean} If a profiled parser cannot be found for the returned version,
  47. * use a non-profiled parser as the fallback. Application code using this
  48. * should take into account that the return object structure might be
  49. * missing the specifics of the profile. Defaults to false.
  50. */
  51. allowFallback: false,
  52. /**
  53. * Property: name
  54. * {String} The name of this parser, this is the part of the CLASS_NAME
  55. * except for "OpenLayers.Format."
  56. */
  57. name: null,
  58. /**
  59. * APIProperty: stringifyOutput
  60. * {Boolean} If true, write will return a string otherwise a DOMElement.
  61. * Default is false.
  62. */
  63. stringifyOutput: false,
  64. /**
  65. * Property: parser
  66. * {Object} Instance of the versioned parser. Cached for multiple read and
  67. * write calls of the same version.
  68. */
  69. parser: null,
  70. /**
  71. * Constructor: OpenLayers.Format.XML.VersionedOGC.
  72. * Constructor.
  73. *
  74. * Parameters:
  75. * options - {Object} Optional object whose properties will be set on
  76. * the object.
  77. */
  78. initialize: function(options) {
  79. OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
  80. var className = this.CLASS_NAME;
  81. this.name = className.substring(className.lastIndexOf(".")+1);
  82. },
  83. /**
  84. * Method: getVersion
  85. * Returns the version to use. Subclasses can override this function
  86. * if a different version detection is needed.
  87. *
  88. * Parameters:
  89. * root - {DOMElement}
  90. * options - {Object} Optional configuration object.
  91. *
  92. * Returns:
  93. * {String} The version to use.
  94. */
  95. getVersion: function(root, options) {
  96. var version;
  97. // read
  98. if (root) {
  99. version = this.version;
  100. if(!version) {
  101. version = root.getAttribute("version");
  102. if(!version) {
  103. version = this.defaultVersion;
  104. }
  105. }
  106. } else { // write
  107. version = (options && options.version) ||
  108. this.version || this.defaultVersion;
  109. }
  110. return version;
  111. },
  112. /**
  113. * Method: getParser
  114. * Get an instance of the cached parser if available, otherwise create one.
  115. *
  116. * Parameters:
  117. * version - {String}
  118. *
  119. * Returns:
  120. * {<OpenLayers.Format>}
  121. */
  122. getParser: function(version) {
  123. version = version || this.defaultVersion;
  124. var profile = this.profile ? "_" + this.profile : "";
  125. if(!this.parser || this.parser.VERSION != version) {
  126. var format = OpenLayers.Format[this.name][
  127. "v" + version.replace(/\./g, "_") + profile
  128. ];
  129. if(!format) {
  130. if (profile !== "" && this.allowFallback) {
  131. // fallback to the non-profiled version of the parser
  132. profile = "";
  133. format = OpenLayers.Format[this.name][
  134. "v" + version.replace(/\./g, "_")
  135. ];
  136. }
  137. if (!format) {
  138. throw "Can't find a " + this.name + " parser for version " +
  139. version + profile;
  140. }
  141. }
  142. this.parser = new format(this.options);
  143. }
  144. return this.parser;
  145. },
  146. /**
  147. * APIMethod: write
  148. * Write a document.
  149. *
  150. * Parameters:
  151. * obj - {Object} An object representing the document.
  152. * options - {Object} Optional configuration object.
  153. *
  154. * Returns:
  155. * {String} The document as a string
  156. */
  157. write: function(obj, options) {
  158. var version = this.getVersion(null, options);
  159. this.parser = this.getParser(version);
  160. var root = this.parser.write(obj, options);
  161. if (this.stringifyOutput === false) {
  162. return root;
  163. } else {
  164. return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
  165. }
  166. },
  167. /**
  168. * APIMethod: read
  169. * Read a doc and return an object representing the document.
  170. *
  171. * Parameters:
  172. * data - {String | DOMElement} Data to read.
  173. * options - {Object} Options for the reader.
  174. *
  175. * Returns:
  176. * {Object} An object representing the document.
  177. */
  178. read: function(data, options) {
  179. if(typeof data == "string") {
  180. data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
  181. }
  182. var root = data.documentElement;
  183. var version = this.getVersion(root);
  184. this.parser = this.getParser(version); // Select the parser
  185. var obj = this.parser.read(data, options); // Parse the data
  186. var errorProperty = this.parser.errorProperty || null;
  187. if (errorProperty !== null && obj[errorProperty] === undefined) {
  188. // an error must have happened, so parse it and report back
  189. var format = new OpenLayers.Format.OGCExceptionReport();
  190. obj.error = format.read(data);
  191. }
  192. obj.version = version;
  193. return obj;
  194. },
  195. CLASS_NAME: "OpenLayers.Format.XML.VersionedOGC"
  196. });