Пример #1
0
  /**
   * Exports a <code>GetRecordsResponse</code> instance to a <code>GetRecordsResponseDocument</code>
   * .
   *
   * @param response
   * @return DOM representation of the <code>GetRecordsResponse</code>
   * @throws XMLException if XML template could not be loaded
   */
  public static GetRecordsResultDocument export(GetRecordsResult response) throws XMLException {
    // 'version'-attribute
    String version = response.getRequest().getVersion();
    if (version == null || "".equals(version.trim())) {
      version = "2.0.0";
    }

    GetRecordsResultDocument responseDocument = new GetRecordsResultDocument(version);

    try {
      Element rootElement = responseDocument.getRootElement();
      Document doc = rootElement.getOwnerDocument();

      // set required namespaces
      Element recordRespRoot =
          response.getSearchResults().getRecords().getOwnerDocument().getDocumentElement();
      NamedNodeMap nnm = recordRespRoot.getAttributes();
      for (int i = 0; i < nnm.getLength(); i++) {
        Node node = nnm.item(i);
        if (node instanceof Attr) {
          rootElement.setAttribute(node.getNodeName(), node.getNodeValue());
        }
      }

      rootElement.setAttribute("version", version);
      String namespace = (version.equals("2.0.2") ? CSW202NS.toString() : CSWNS.toString());

      // 'RequestId'-element (optional)
      if (response.getRequest().getId() != null) {
        Element requestIdElement = doc.createElementNS(namespace, "csw:RequestId");
        requestIdElement.appendChild(doc.createTextNode(response.getRequest().getId()));
        rootElement.appendChild(requestIdElement);
      }

      // 'SearchStatus'-element (required)
      Element searchStatusElement = doc.createElementNS(namespace, "csw:SearchStatus");
      // 'status'-attribute (required)
      if (!version.equals("2.0.2")) {
        searchStatusElement.setAttribute("status", response.getSearchStatus().getStatus());
      }
      // 'timestamp'-attribute (optional)
      if (response.getSearchStatus().getTimestamp() != null) {
        Date date = response.getSearchStatus().getTimestamp();
        String time = TimeTools.getISOFormattedTime(date);
        searchStatusElement.setAttribute("timestamp", time);
      }
      rootElement.appendChild(searchStatusElement);

      // 'SeachResults'-element (required)
      Element searchResultsElement = doc.createElementNS(namespace, "csw:SearchResults");
      SearchResults results = response.getSearchResults();

      // 'resultSetId'-attribute (optional)
      if (results.getResultSetId() != null) {
        searchResultsElement.setAttribute("resultSetId", results.getResultSetId().toString());
      }
      // 'elementSet'-attribute (optional)
      if (results.getElementSet() != null) {
        searchResultsElement.setAttribute("elementSet", results.getElementSet().toString());
      }
      // 'recordSchema'-attribute (optional)
      if (results.getRecordSchema() != null) {
        searchResultsElement.setAttribute("recordSchema", results.getRecordSchema().toString());
      }
      // 'numberOfRecordsMatched'-attribute (required)
      searchResultsElement.setAttribute(
          "numberOfRecordsMatched", "" + results.getNumberOfRecordsMatched());
      // 'numberOfRecordsReturned'-attribute (required)
      searchResultsElement.setAttribute(
          "numberOfRecordsReturned", "" + results.getNumberOfRecordsReturned());
      // 'nextRecord'-attribute (required)
      searchResultsElement.setAttribute("nextRecord", "" + results.getNextRecord());
      // 'expires'-attribute (optional)
      if (results.getExpires() != null) {
        Date date = results.getExpires();
        String time = TimeTools.getISOFormattedTime(date);
        searchResultsElement.setAttribute("expires", time);
      }
      // append all children of the records container node
      NodeList nl = results.getRecords().getChildNodes();
      for (int i = 0; i < nl.getLength(); i++) {
        Node copy = doc.importNode(nl.item(i), true);
        searchResultsElement.appendChild(copy);
      }
      rootElement.appendChild(searchResultsElement);
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      throw new XMLException(e.getMessage());
    }
    return responseDocument;
  }
Пример #2
0
 public String getFilterExpression() {
   return TimeTools.getISOFormattedTime(wrappee);
 }