Пример #1
0
  @SuppressWarnings("unchecked")
  protected ExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
    ExtensionElement extensionElement = new ExtensionElement();
    extensionElement.setName(xtr.getLocalName());
    if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
      extensionElement.setNamespace(xtr.getNamespaceURI());
    }
    if (StringUtils.isNotEmpty(xtr.getPrefix())) {
      extensionElement.setNamespacePrefix(xtr.getPrefix());
    }

    BpmnXMLUtil.addCustomAttributes(xtr, extensionElement, defaultElementAttributes);

    boolean readyWithExtensionElement = false;
    while (readyWithExtensionElement == false && xtr.hasNext()) {
      xtr.next();
      if (xtr.isCharacters()) {
        if (StringUtils.isNotEmpty(xtr.getText().trim())) {
          extensionElement.setElementText(xtr.getText().trim());
        }
      } else if (xtr.isStartElement()) {
        ExtensionElement childExtensionElement = parseExtensionElement(xtr);
        extensionElement.addChildElement(childExtensionElement);
      } else if (xtr.isEndElement()
          && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
        readyWithExtensionElement = true;
      }
    }
    return extensionElement;
  }
Пример #2
0
  public void testDOMSource() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    InputSource source = new InputSource(new StringReader(xml));
    Document doc = builder.parse(source);

    // Fails when using DOMWrappingReader
    XMLStreamReader reader = getInputFactory().createXMLStreamReader(new DOMSource(doc));

    reader.next(); // root
    assertEquals(0, reader.getAttributeCount());
    assertEquals(1, reader.getNamespaceCount());
    assertEquals("http://testnamespace/", reader.getNamespaceURI());
    assertEquals("ns2", reader.getPrefix());
    assertEquals("root", reader.getLocalName());

    reader.next(); // arg0
    reader.next(); // obj

    assertEquals("obj", reader.getLocalName());
    assertEquals(
        "ns2:mycomplextype",
        reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "type"));
    assertEquals("http://testnamespace/", reader.getNamespaceURI("ns2"));
    assertEquals("http://testnamespace/", reader.getNamespaceContext().getNamespaceURI("ns2"));

    assertEquals("ns2", reader.getNamespaceContext().getPrefix("http://testnamespace/"));
  }
Пример #3
0
 public String getPrefix() {
   if (state == STATE_START_DOCUMENT || state == STATE_END_DOCUMENT) {
     throw new IllegalStateException();
   } else {
     return parent.getPrefix();
   }
 }
Пример #4
0
 /**
  * Method that can be used to verify that the current element pointed to by the stream reader has
  * no prefix.
  */
 protected static void assertNoElemPrefix(XMLStreamReader sr) throws XMLStreamException {
   String prefix = sr.getPrefix();
   if (prefix != ELEM_NO_PREFIX) {
     fail(
         "Element that does not have a prefix should be indicated with <"
             + ELEM_NO_PREFIX
             + ">, not <"
             + prefix
             + ">");
   }
 }
 public String getPrefix() {
   return streamReader.getPrefix();
 }
Пример #6
0
 /**
  * Method getPrefix.
  *
  * @return Returns String.
  * @throws OMException
  */
 public String getPrefix() throws OMException {
   return parser.getPrefix();
 }
Пример #7
0
 public void parse(XMLStreamReader input) throws XMLStreamException, XNIException {
   if (input.hasNext()) {
     int eventType = input.getEventType();
     if (eventType != XMLStreamConstants.START_DOCUMENT
         && eventType != XMLStreamConstants.START_ELEMENT) {
       throw new XMLStreamException();
     }
     fLocationWrapper.setLocation(input.getLocation());
     fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
     boolean first = true;
     loop:
     while (input.hasNext()) {
       if (!first) {
         eventType = input.next();
       } else {
         first = false;
       }
       switch (eventType) {
         case XMLStreamConstants.START_ELEMENT:
           ++fDepth;
           fLocationWrapper.setLocation(input.getLocation());
           fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
           fillQName(
               fElementQName, input.getNamespaceURI(), input.getLocalName(), input.getPrefix());
           fillXMLAttributes(input);
           fillDeclaredPrefixes(input);
           addNamespaceDeclarations();
           fNamespaceContext.pushContext();
           fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
           break;
         case XMLStreamConstants.END_ELEMENT:
           fLocationWrapper.setLocation(input.getLocation());
           fNamespaceContext.setNamespaceContext(input.getNamespaceContext());
           fillQName(
               fElementQName, input.getNamespaceURI(), input.getLocalName(), input.getPrefix());
           fillDeclaredPrefixes(input);
           fSchemaDOMParser.endElement(fElementQName, null);
           fNamespaceContext.popContext();
           --fDepth;
           if (fDepth <= 0) {
             break loop;
           }
           break;
         case XMLStreamConstants.CHARACTERS:
           fTempString.setValues(
               input.getTextCharacters(), input.getTextStart(), input.getTextLength());
           fSchemaDOMParser.characters(fTempString, null);
           break;
         case XMLStreamConstants.SPACE:
           fTempString.setValues(
               input.getTextCharacters(), input.getTextStart(), input.getTextLength());
           fSchemaDOMParser.ignorableWhitespace(fTempString, null);
           break;
         case XMLStreamConstants.CDATA:
           fSchemaDOMParser.startCDATA(null);
           fTempString.setValues(
               input.getTextCharacters(), input.getTextStart(), input.getTextLength());
           fSchemaDOMParser.characters(fTempString, null);
           fSchemaDOMParser.endCDATA(null);
           break;
         case XMLStreamConstants.PROCESSING_INSTRUCTION:
           fillProcessingInstruction(input.getPIData());
           fSchemaDOMParser.processingInstruction(input.getPITarget(), fTempString, null);
           break;
         case XMLStreamConstants.DTD:
           /* There shouldn't be a DTD in the schema */
           break;
         case XMLStreamConstants.ENTITY_REFERENCE:
           /* Not needed for schemas */
           break;
         case XMLStreamConstants.COMMENT:
           /* No point in sending comments */
           break;
         case XMLStreamConstants.START_DOCUMENT:
           ++fDepth;
           /* We automatically call startDocument before the loop */
           break;
         case XMLStreamConstants.END_DOCUMENT:
           /* We automatically call endDocument after the loop */
           break;
       }
     }
     fLocationWrapper.setLocation(null);
     fNamespaceContext.setNamespaceContext(null);
     fSchemaDOMParser.endDocument(null);
   }
 }
Пример #8
0
 public String getPrefix() {
   return reader.getPrefix();
 }
Пример #9
0
  /** 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;
  }