Example #1
0
  @Override
  public Element copy() {
    Element copy = new Element(getLocalName(), getNamespaceURI(), getNamespacePrefix());

    for (int i = 0; i < getAttributeCount(); ++i) {
      copy.addAttribute(getAttribute(i).copy());
    }

    for (int i = 0; i < getChildCount(); ++i) {
      copy.appendChild(getChild(i).copy());
    }

    return copy;
  }
  /** Parses the XML via StAX. */
  static Document buildDocument(XMLStreamReader xmlReader) throws XMLStreamException, IOException {
    Document doc = null;

    Stack<Element> elements = new Stack<Element>();
    while (xmlReader.hasNext()) {
      int eventType = xmlReader.next();
      switch (eventType) {
        case XMLEvent.START_DOCUMENT:
          doc = null;
          break;

        case XMLEvent.START_ELEMENT:
          // StartElement startElement = event.asStartElement();

          Element subElement;
          if (null != xmlReader.getPrefix()) {
            subElement =
                new Element(
                    xmlReader.getLocalName(), xmlReader.getNamespaceURI(), xmlReader.getPrefix());
          } else {
            subElement = new Element(xmlReader.getLocalName(), xmlReader.getNamespaceURI());
          }

          if (elements.isEmpty()) {
            doc = new Document(subElement);
          } else {
            elements.peek().appendChild(subElement);
          }

          for (int i = 0; i < xmlReader.getAttributeCount(); ++i) {
            if (null != xmlReader.getAttributePrefix(i)) {
              subElement.addAttribute(
                  new org.eclipse.stardust.engine.core.struct.sxml.Attribute(
                      xmlReader.getAttributeLocalName(i),
                      xmlReader.getAttributeNamespace(i),
                      xmlReader.getAttributePrefix(i),
                      xmlReader.getAttributeValue(i)));
            } else {
              subElement.addAttribute(
                  new org.eclipse.stardust.engine.core.struct.sxml.Attribute(
                      xmlReader.getAttributeLocalName(i),
                      xmlReader.getAttributeNamespace(i),
                      xmlReader.getAttributeValue(i)));
            }
          }

          elements.push(subElement);
          break;

        case XMLEvent.CHARACTERS:
          org.eclipse.stardust.engine.core.struct.sxml.Text text =
              new org.eclipse.stardust.engine.core.struct.sxml.Text(xmlReader.getText());
          if (!elements.isEmpty()) {
            elements.peek().appendChild(text);
          } else {
            trace.info("Ignoring text placed outside of any element: " + xmlReader.getText());
          }
          break;

        case XMLEvent.SPACE:
          trace.debug("Ignoring whitespace inside XML.");
          break;

        case XMLEvent.END_ELEMENT:
          elements.pop();
          break;

        case XMLEvent.END_DOCUMENT:
          break;

        default:
          trace.error("Unsupported StAX event type: " + eventType);
      }
    }

    return doc;
  }