コード例 #1
0
ファイル: XMLFactory.java プロジェクト: wilden/deegree2-base
  /**
   * Exports a <code>GetRecordById</code> instance to a <code>GetRecordByIdDocument</code>.
   *
   * @param request
   * @return DOM representation of the <code>GetRecordById</code>
   * @throws XMLException if XML template could not be loaded
   */
  public static GetRecordByIdDocument export(GetRecordById request) throws XMLException {

    GetRecordByIdDocument getRecordByIdDoc = new GetRecordByIdDocument();
    try {
      getRecordByIdDoc.createEmptyDocument();
    } catch (Exception e) {
      throw new XMLException(e.getMessage());
    }
    Element rootElement = getRecordByIdDoc.getRootElement();
    Document doc = rootElement.getOwnerDocument();

    // 'version'-attribute
    rootElement.setAttribute("version", request.getVersion());

    String[] ids = request.getIds();
    for (int i = 0; i < ids.length; i++) {
      Element idElement = doc.createElementNS(CSWNS.toString(), "csw:Id");
      idElement.appendChild(doc.createTextNode(ids[i]));
      rootElement.appendChild(idElement);
    }

    String elementSetName = request.getElementSetName();
    if (elementSetName != null) {
      Element esnElement = doc.createElementNS(CSWNS.toString(), "csw:ElementSetName");
      esnElement.appendChild(doc.createTextNode(elementSetName));
      rootElement.appendChild(esnElement);
    }

    return getRecordByIdDoc;
  }
コード例 #2
0
ファイル: XMLFactory.java プロジェクト: wilden/deegree2-base
  /**
   * Exports a <code>DescribeRecordResponse</code> instance to a <code>
   * DescribeRecordResponseDocument</code>.
   *
   * @param response
   * @return DOM representation of the <code>DescribeRecordResponse</code>
   * @throws XMLException if XML template could not be loaded
   */
  public static DescribeRecordResultDocument export(DescribeRecordResult response)
      throws XMLException {

    DescribeRecordResultDocument responseDocument = new DescribeRecordResultDocument();

    String ns =
        response.getRequest().getVersion().equals("2.0.2") ? CSW202NS.toString() : CSWNS.toString();

    try {
      responseDocument.createEmptyDocument(response.getRequest().getVersion());
      Element rootElement = responseDocument.getRootElement();
      Document doc = rootElement.getOwnerDocument();

      // 'SchemaComponent'-elements (required)
      SchemaComponent[] components = response.getSchemaComponents();
      for (int i = 0; i < components.length; i++) {
        Element schemaComponentElement = doc.createElementNS(ns, "csw:SchemaComponent");

        // 'targetNamespace'-attribute (required)
        schemaComponentElement.setAttribute(
            "targetNamespace", components[i].getTargetNamespace().toString());

        // 'parentSchema'-attribute (optional)
        if (components[i].getParentSchema() != null) {
          schemaComponentElement.setAttribute(
              "parentSchema", components[i].getParentSchema().toString());
        }

        // 'schemaLanguage'-attribute (required)
        schemaComponentElement.setAttribute(
            "schemaLanguage", components[i].getSchemaLanguage().toString());

        XMLTools.insertNodeInto(components[i].getSchema().getRootElement(), schemaComponentElement);
        rootElement.appendChild(schemaComponentElement);
      }
    } catch (Exception e) {
      LOG.logError(e.getMessage(), e);
      throw new XMLException(e.getMessage());
    }
    return responseDocument;
  }
コード例 #3
0
ファイル: XMLFactory.java プロジェクト: wilden/deegree2-base
  /**
   * 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;
  }
コード例 #4
0
ファイル: XMLFactory.java プロジェクト: wilden/deegree2-base
  /**
   * Exports a <code>GetRecords</code> instance to a <code>GetRecordsDocument</code>.
   *
   * @param request
   * @return DOM representation of the <code>GetRecords</code>
   * @throws XMLException if some elements could not be appended
   * @throws OGCWebServiceException if an error occurred while creating the xml-representation of
   *     the GetRecords bean.
   */
  public static GetRecordsDocument export(GetRecords request)
      throws XMLException, OGCWebServiceException {

    GetRecordsDocument getRecordsDocument = null;

    getRecordsDocument = new GetRecordsDocument();
    try {
      getRecordsDocument.createEmptyDocument();
    } catch (Exception e) {
      throw new XMLException(e.getMessage());
    }
    Element rootElement = getRecordsDocument.getRootElement();
    Document doc = rootElement.getOwnerDocument();

    // 'version'-attribute
    rootElement.setAttribute("version", request.getVersion());

    // 'resultType'-attribute
    rootElement.setAttribute("resultType", request.getResultTypeAsString());

    // 'outputFormat'-attribute
    rootElement.setAttribute("outputFormat", request.getOutputFormat());

    // 'outputSchema'-attribute
    rootElement.setAttribute("outputSchema", request.getOutputSchema());

    // 'startPosition'-attribute
    rootElement.setAttribute("startPosition", "" + request.getStartPosition());

    // 'maxRecords'-attribute
    rootElement.setAttribute("maxRecords", "" + request.getMaxRecords());

    // '<csw:DistributedSearch>'-element
    if (request.getHopCount() != -1) {
      Element distributedSearchElement =
          doc.createElementNS(CSWNS.toString(), "csw:DistributedSearch");

      // 'hopCount'-attribute
      distributedSearchElement.setAttribute("hopCount", "" + request.getHopCount());
      rootElement.appendChild(distributedSearchElement);
    }

    // '<csw:ResponseHandler>'-elements (optional)
    URI responseHandler = request.getResponseHandler();
    if (responseHandler != null) {
      Element responseHandlerElement = doc.createElementNS(CSWNS.toString(), "csw:ResponseHandler");
      responseHandlerElement.appendChild(doc.createTextNode(responseHandler.toASCIIString()));
      rootElement.appendChild(responseHandlerElement);
    }

    // '<csw:Query>'-elements (required)
    Query query = request.getQuery();
    if (query != null) {
      LOG.logDebug("Adding the csw:Query element to the csw:GetRecords document");
      Element queryElement = doc.createElementNS(CSWNS.toString(), "csw:Query");

      // 'typeName'-attribute
      // Testing for the list of typenames.
      List<QualifiedName> typeNames = query.getTypeNamesAsList();
      Map<String, QualifiedName> aliases =
          new HashMap<String, QualifiedName>(query.getDeclaredTypeNameVariables());
      if (typeNames.size() > 0) {
        appendTypeNamesAttribute(rootElement, queryElement, typeNames, aliases);
      } else {

        String s = StringTools.arrayToString(query.getTypeNames(), ',');
        queryElement.setAttribute("typeNames", s);
      }

      // '<csw:ElementSetName>'-element (optional)
      if (query.getElementSetName() != null) {
        Element elementSetNameElement = doc.createElementNS(CSWNS.toString(), "csw:ElementSetName");
        List<QualifiedName> elementSetNameTypeNamesList = query.getElementSetNameTypeNamesList();
        if (query.getElementSetNameVariables() != null
            && query.getElementSetNameVariables().size() > 0) {
          throw new OGCWebServiceException(
              "The elementSetName element in a csw:GetRecords request may not refrerence variables (aka. aliases), aborting request");
        }
        if (elementSetNameTypeNamesList.size() > 0) {
          appendTypeNamesAttribute(
              rootElement, elementSetNameElement, elementSetNameTypeNamesList, null);
        }
        elementSetNameElement.appendChild(doc.createTextNode(query.getElementSetName()));
        queryElement.appendChild(elementSetNameElement);
      }

      // '<csw:ElementName>'-elements (optional)
      if (query.getElementNamesAsPropertyPaths() != null) {
        List<PropertyPath> elementNames = query.getElementNamesAsPropertyPaths();
        for (int j = 0; j < elementNames.size(); j++) {
          Element elementNameElement = doc.createElementNS(CSWNS.toString(), "csw:ElementName");
          elementNameElement.appendChild(doc.createTextNode(elementNames.get(j).getAsString()));
          queryElement.appendChild(elementNameElement);
        }
      }

      // '<csw:Constraint>'-element (optional)
      if (query.getContraint() != null) {
        Element constraintElement = doc.createElementNS(CSWNS.toString(), "csw:Constraint");
        constraintElement.setAttribute("version", "1.1.0");
        org.deegree.model.filterencoding.XMLFactory.appendFilter(
            constraintElement, query.getContraint());
        queryElement.appendChild(constraintElement);
      }

      // '<ogc:SortBy>'-element (optional)
      SortProperty[] sortProperties = query.getSortProperties();
      if (sortProperties != null && sortProperties.length != 0) {
        Element sortByElement = doc.createElementNS(OGCNS.toString(), "ogc:SortBy");

        // '<ogc:SortProperty>'-elements
        for (int j = 0; j < sortProperties.length; j++) {
          Element sortPropertiesElement = doc.createElementNS(OGCNS.toString(), "ogc:SortProperty");

          // '<ogc:PropertyName>'-element (required)
          Element propertyNameElement = doc.createElementNS(OGCNS.toString(), "ogc:PropertyName");
          appendPropertyPath(propertyNameElement, sortProperties[j].getSortProperty());

          // '<ogc:SortOrder>'-element (optional)
          Element sortOrderElement = doc.createElementNS(OGCNS.toString(), "ogc:SortOrder");
          Node tn = doc.createTextNode(sortProperties[j].getSortOrder() ? "ASC" : "DESC");
          sortOrderElement.appendChild(tn);

          sortPropertiesElement.appendChild(propertyNameElement);
          sortPropertiesElement.appendChild(sortOrderElement);
          sortByElement.appendChild(sortPropertiesElement);
        }
        queryElement.appendChild(sortByElement);
      }
      rootElement.appendChild(queryElement);
    }
    return getRecordsDocument;
  }