Example #1
0
  private void processElement(SchemaParticle sp, XmlCursor xmlc, boolean mixed) {
    // cast as schema local element
    SchemaLocalElement element = (SchemaLocalElement) sp;

    // Add comment about type
    addElementTypeAndRestricionsComment(element, xmlc);

    // / ^ -> <elemenname></elem>^
    if (_soapEnc) xmlc.insertElement(element.getName().getLocalPart()); // soap
    // encoded?
    // drop
    // namespaces.
    else xmlc.insertElement(element.getName().getLocalPart(), element.getName().getNamespaceURI());
    // / -> <elem>^</elem>
    // processAttributes( sp.getType(), xmlc );

    xmlc.toPrevToken();
    // -> <elem>stuff^</elem>

    String[] values = null;
    if (multiValues != null) values = multiValues.get(element.getName());
    if (values != null) xmlc.insertChars(StringUtils.join(values, ","));
    else if (sp.isDefault()) xmlc.insertChars(sp.getDefaultText());
    else createSampleForType(element.getType(), xmlc);
    // -> <elem>stuff</elem>^
    xmlc.toNextToken();
  }
Example #2
0
  /**
   * Cursor position Before: <theElement>^</theElement> After: <theElement><lots of
   * stuff/>^</theElement>
   */
  public void createSampleForType(SchemaType stype, XmlCursor xmlc) {
    _exampleContent =
        SoapUI.getSettings().getBoolean(WsdlSettings.XML_GENERATION_TYPE_EXAMPLE_VALUE);
    _typeComment = SoapUI.getSettings().getBoolean(WsdlSettings.XML_GENERATION_TYPE_COMMENT_TYPE);
    _skipComments = SoapUI.getSettings().getBoolean(WsdlSettings.XML_GENERATION_SKIP_COMMENTS);

    QName nm = stype.getName();
    if (nm == null && stype.getContainerField() != null) nm = stype.getContainerField().getName();

    if (nm != null && excludedTypes.contains(nm)) {
      if (!_skipComments) xmlc.insertComment("Ignoring type [" + nm + "]");
      return;
    }

    if (_typeStack.contains(stype)) return;

    _typeStack.add(stype);

    try {
      if (stype.isSimpleType() || stype.isURType()) {
        processSimpleType(stype, xmlc);
        return;
      }

      // complex Type
      // <theElement>^</theElement>
      processAttributes(stype, xmlc);

      // <theElement attri1="string">^</theElement>
      switch (stype.getContentType()) {
        case SchemaType.NOT_COMPLEX_TYPE:
        case SchemaType.EMPTY_CONTENT:
          // noop
          break;
        case SchemaType.SIMPLE_CONTENT:
          {
            processSimpleType(stype, xmlc);
          }
          break;
        case SchemaType.MIXED_CONTENT:
          xmlc.insertChars(pick(WORDS) + " ");
          if (stype.getContentModel() != null) {
            processParticle(stype.getContentModel(), xmlc, true);
          }
          xmlc.insertChars(pick(WORDS));
          break;
        case SchemaType.ELEMENT_CONTENT:
          if (stype.getContentModel() != null) {
            processParticle(stype.getContentModel(), xmlc, false);
          }
          break;
      }
    } finally {
      _typeStack.remove(_typeStack.size() - 1);
    }
  }
Example #3
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;
  }
Example #4
0
 private void processSequence(SchemaParticle sp, XmlCursor xmlc, boolean mixed) {
   SchemaParticle[] spc = sp.getParticleChildren();
   for (int i = 0; i < spc.length; i++) {
     // / <parent>maybestuff^</parent>
     processParticle(spc[i], xmlc, mixed);
     // <parent>maybestuff...morestuff^</parent>
     if (mixed && i < spc.length - 1) xmlc.insertChars(pick(WORDS));
   }
 }
Example #5
0
  private void processSimpleType(SchemaType stype, XmlCursor xmlc) {
    if (_soapEnc) {
      QName typeName = stype.getName();
      if (typeName != null) {
        xmlc.insertAttributeWithValue(XSI_TYPE, formatQName(xmlc, typeName));
      }
    }

    String sample = sampleDataForSimpleType(stype);
    xmlc.insertChars(sample);
  }
Example #6
0
  private void processAll(SchemaParticle sp, XmlCursor xmlc, boolean mixed) {
    SchemaParticle[] spc = sp.getParticleChildren();
    if (!_skipComments)
      xmlc.insertComment(
          "You may enter the following " + String.valueOf(spc.length) + " items in any order");

    for (int i = 0; i < spc.length; i++) {
      processParticle(spc[i], xmlc, mixed);
      if (mixed && i < spc.length - 1) xmlc.insertChars(pick(WORDS));
    }
  }