v2_0_2.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/Protocol/CSW.js
  7. * @requires OpenLayers/Format/CSWGetRecords/v2_0_2.js
  8. */
  9. /**
  10. * Class: OpenLayers.Protocol.CSW.v2_0_2
  11. * CS-W (Catalogue services for the Web) version 2.0.2 protocol.
  12. *
  13. * Inherits from:
  14. * - <OpenLayers.Protocol>
  15. */
  16. OpenLayers.Protocol.CSW.v2_0_2 = OpenLayers.Class(OpenLayers.Protocol, {
  17. /**
  18. * Property: formatOptions
  19. * {Object} Optional options for the format. If a format is not provided,
  20. * this property can be used to extend the default format options.
  21. */
  22. formatOptions: null,
  23. /**
  24. * Constructor: OpenLayers.Protocol.CSW.v2_0_2
  25. * A class for CSW version 2.0.2 protocol management.
  26. *
  27. * Parameters:
  28. * options - {Object} Optional object whose properties will be set on the
  29. * instance.
  30. */
  31. initialize: function(options) {
  32. OpenLayers.Protocol.prototype.initialize.apply(this, [options]);
  33. if(!options.format) {
  34. this.format = new OpenLayers.Format.CSWGetRecords.v2_0_2(OpenLayers.Util.extend({
  35. }, this.formatOptions));
  36. }
  37. },
  38. /**
  39. * APIMethod: destroy
  40. * Clean up the protocol.
  41. */
  42. destroy: function() {
  43. if(this.options && !this.options.format) {
  44. this.format.destroy();
  45. }
  46. this.format = null;
  47. OpenLayers.Protocol.prototype.destroy.apply(this);
  48. },
  49. /**
  50. * Method: read
  51. * Construct a request for reading new records from the Catalogue.
  52. */
  53. read: function(options) {
  54. options = OpenLayers.Util.extend({}, options);
  55. OpenLayers.Util.applyDefaults(options, this.options || {});
  56. var response = new OpenLayers.Protocol.Response({requestType: "read"});
  57. var data = this.format.write(options.params || options);
  58. response.priv = OpenLayers.Request.POST({
  59. url: options.url,
  60. callback: this.createCallback(this.handleRead, response, options),
  61. params: options.params,
  62. headers: options.headers,
  63. data: data
  64. });
  65. return response;
  66. },
  67. /**
  68. * Method: handleRead
  69. * Deal with response from the read request.
  70. *
  71. * Parameters:
  72. * response - {<OpenLayers.Protocol.Response>} The response object to pass
  73. * to the user callback.
  74. * This response is given a code property, and optionally a data property.
  75. * The latter represents the CSW records as returned by the call to
  76. * the CSW format read method.
  77. * options - {Object} The user options passed to the read call.
  78. */
  79. handleRead: function(response, options) {
  80. if(options.callback) {
  81. var request = response.priv;
  82. if(request.status >= 200 && request.status < 300) {
  83. // success
  84. response.data = this.parseData(request);
  85. response.code = OpenLayers.Protocol.Response.SUCCESS;
  86. } else {
  87. // failure
  88. response.code = OpenLayers.Protocol.Response.FAILURE;
  89. }
  90. options.callback.call(options.scope, response);
  91. }
  92. },
  93. /**
  94. * Method: parseData
  95. * Read HTTP response body and return records
  96. *
  97. * Parameters:
  98. * request - {XMLHttpRequest} The request object
  99. *
  100. * Returns:
  101. * {Object} The CSW records as returned by the call to the format read method.
  102. */
  103. parseData: function(request) {
  104. var doc = request.responseXML;
  105. if(!doc || !doc.documentElement) {
  106. doc = request.responseText;
  107. }
  108. if(!doc || doc.length <= 0) {
  109. return null;
  110. }
  111. return this.format.read(doc);
  112. },
  113. CLASS_NAME: "OpenLayers.Protocol.CSW.v2_0_2"
  114. });