Beispiel #1
0
  /**
   * Uses the XPath text() function to get values from <name> elements in received XML, then
   * collects those values as the value of a <names> element created here.
   *
   * <p>Demonstrates the following characteristics of the selectPath method:
   *
   * <p>- It supports expressions that include XPath function calls. - selectPath called from an
   * XmlCursor instance (instead of an XMLBeans type) places results (if any) into the cursor's
   * selection set.
   *
   * @param empDoc The incoming XML.
   * @return <code>true</code> if the XPath expression returned results; otherwise, <code>false
   *     </code>.
   */
  public boolean collectNames(XmlObject empDoc) {
    boolean hasResults = false;

    // Create a cursor with which to execute query expressions. The cursor
    // is inserted at the very beginning of the incoming XML, then moved to
    // the first element's START token.
    XmlCursor pathCursor = empDoc.newCursor();
    pathCursor.toFirstChild();

    // Execute the path expression, qualifying it with the namespace
    // declaration.
    pathCursor.selectPath(m_namespaceDeclaration + "$this//xq:employee/xq:name/text()");

    // If there are results, then go ahead and do stuff.
    if (pathCursor.getSelectionCount() > 0) {
      hasResults = true;

      // Create a new <names> element into which names from the XML
      // will be copied. Note that this element is in the default
      // namespace; it's not part of the schema.
      XmlObject namesElement = null;
      try {
        namesElement = XmlObject.Factory.parse("<names/>");
      } catch (XmlException e) {
        e.printStackTrace();
      }

      // Add a cursor the new element and put it between its START and END
      // tokens, where new values can be inserted.
      XmlCursor namesCursor = namesElement.newCursor();
      namesCursor.toFirstContentToken();
      namesCursor.toEndToken();

      // Loop through the selections, appending the incoming <name> element's
      // value to the new <name> element's value. (Of course, this could have
      // been done with a StringBuffer, but that wouldn't show the cursor in
      // use.)
      while (pathCursor.toNextSelection()) {
        namesCursor.insertChars(pathCursor.getTextValue());
        if (pathCursor.hasNextSelection()) {
          namesCursor.insertChars(", ");
        }
      }
      // Dispose of the cursors now that they're not needed.
      pathCursor.dispose();
      namesCursor.dispose();

      // Print the new element.
      System.out.println("\nNames collected by collectNames method: \n\n" + namesElement + "\n");
    }
    return hasResults;
  }
  @Test
  public void shouldCreateValidLevel1SubscribeRequest() throws OXFException, XmlException {
    String consumer = "http://*****:*****@xlink:href='urn:ogc:object:procedure:CITE:WeatherService:LGA'";

    ParameterContainer parameter = new ParameterContainer();
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_SES_URL, host);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_CONSUMER_REFERENCE_ADDRESS, consumer);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_FILTER_TOPIC, topicString);
    parameter.addParameterShell(
        ISESRequestBuilder.SUBSCRIBE_FILTER_MESSAGE_CONTENT_DIALECT, dialect);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_FILTER_MESSAGE_CONTENT, xpath);

    SESRequestBuilder_00 request = new SESRequestBuilder_00();
    String asText = request.buildSubscribeRequest(parameter);
    EnvelopeDocument envelope = EnvelopeDocument.Factory.parse(asText);

    Collection<XmlError> errors = XMLBeansParser.validate(envelope);
    Assert.assertTrue("SubscribeRequest is not valid: " + errors, errors.isEmpty());

    Body body = envelope.getEnvelope().getBody();
    XmlCursor cur = body.newCursor();
    cur.toFirstChild();
    XmlObject subscribeRequestObject = cur.getObject();
    cur.dispose();

    if (!(subscribeRequestObject instanceof Subscribe))
      throw new IllegalStateException("Body does not contain a Subscribe request.");

    Subscribe subscribeRequest = (Subscribe) subscribeRequestObject;
    Assert.assertTrue(
        subscribeRequest
            .getConsumerReference()
            .getAddress()
            .getStringValue()
            .trim()
            .equals(consumer.trim()));

    cur = subscribeRequest.getFilter().newCursor();
    cur.toFirstChild();
    do {
      XmlObject filterElement = cur.getObject();
      if (filterElement instanceof QueryExpressionType) {
        QueryExpressionType qet = (QueryExpressionType) filterElement;
        Assert.assertTrue(qet.getDialect().trim().equals(dialect.trim()));
        XmlCursor tmpCur = qet.newCursor();
        Assert.assertTrue(tmpCur.toFirstContentToken() == TokenType.TEXT);
        Assert.assertTrue(tmpCur.getChars().trim().equals(xpath.trim()));
      } else if (filterElement instanceof TopicExpressionType) {
        TopicExpressionType tet = (TopicExpressionType) filterElement;
        Assert.assertTrue(
            tet.getDialect().trim().equals(SESRequestBuilder_00.DEFAULT_TOPIC_DIALECT));
        XmlCursor tmpCur = tet.newCursor();
        Assert.assertTrue(tmpCur.toFirstContentToken() == TokenType.TEXT);
        Assert.assertTrue(tmpCur.getChars().trim().equals(topicString.trim()));
      }
    } while (cur.toNextSibling());
  }