Ejemplo n.º 1
0
  public void testNextTagOk() throws XMLStreamException {
    String XML = "<root>\n" + "<branch>   <leaf>  </leaf></branch>" + "</root>";

    for (int i = 0; i < 4; ++i) {
      boolean ns = (i & 1) != 0;
      boolean coal = (i & 2) != 0;
      XMLEventReader er = getReader(XML, ns, coal);

      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());

      assertTokenType(START_ELEMENT, er.nextTag().getEventType());
      assertTokenType(START_ELEMENT, er.nextTag().getEventType());
      /* Ok, let's mix in bit of peeking to ensure reader won't
       * be confused too badly...
       */
      // This should be space between <branch> and <leaf>...
      assertTokenType(CHARACTERS, er.peek().getEventType());

      // And then the leaf
      assertTokenType(START_ELEMENT, er.nextTag().getEventType());

      assertTokenType(END_ELEMENT, er.nextTag().getEventType());
      assertTokenType(END_ELEMENT, er.nextTag().getEventType());
      assertTokenType(END_ELEMENT, er.nextTag().getEventType());

      assertTokenType(END_DOCUMENT, er.nextEvent().getEventType());
      assertFalse(er.hasNext());
    }
  }
Ejemplo n.º 2
0
  /**
   * This test was inspired by an actual bug in one of implementations: initial state was not
   * properly set if nextTag() was called (instead of nextEvent()), and subsequent peek() failed.
   */
  public void testPeek() throws XMLStreamException {
    String XML = "<root>text</root>";

    for (int i = 0; i < 4; ++i) {
      boolean ns = (i & 1) != 0;
      boolean coal = (i & 2) != 0;
      XMLEventReader er = getReader(XML, ns, coal);
      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());

      XMLEvent tag = er.nextTag();
      assertTokenType(START_ELEMENT, tag.getEventType());

      // Now, peek() should produce text..
      XMLEvent text = er.peek();
      assertTokenType(CHARACTERS, text.getEventType());
      Characters chars = text.asCharacters();
      assertNotNull(chars);
      assertEquals("text", chars.getData());

      // and need nextEvent() to get rid of it, too:
      text = er.nextEvent();
      // Let's verify it again:
      assertTokenType(CHARACTERS, text.getEventType());
      chars = text.asCharacters();
      assertNotNull(chars);
      assertEquals("text", chars.getData());
      assertTokenType(END_ELEMENT, er.nextTag().getEventType());
      assertTokenType(END_DOCUMENT, er.nextEvent().getEventType());

      // And at the end, peek() should return null
      assertNull(er.peek());
    }
  }
Ejemplo n.º 3
0
  public void testNextTagInvalid() throws XMLStreamException {
    String XML = "<root>   non-empty<leaf /></root>";
    String XML2 = "<root><leaf></leaf>text   </root>";

    for (int i = 0; i < 4; ++i) {
      boolean ns = (i & 1) != 0;
      boolean coal = (i & 2) != 0;
      XMLEventReader er = getReader(XML, ns, coal);
      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      String msg = null;
      try {
        XMLEvent evt = er.nextTag();
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on non-empty CHARACTERS";
      } catch (XMLStreamException sex) {
        // fine!
      } catch (Throwable t) {
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on non-empty CHARACTERS; got ("
                + t.getClass()
                + "): "
                + t;
      }
      if (msg != null) {
        fail(msg);
      }
      er.close();

      /* any other easily failing cases? Maybe if we are on top of
       * END_ELEMENT, and will hit another one?
       */
      er = getReader(XML2, ns, coal);
      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      assertTokenType(END_ELEMENT, er.nextEvent().getEventType());

      try {
        XMLEvent evt = er.nextTag();
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on END_ELEMENT and hitting non-ws text; got event "
                + tokenTypeDesc(evt);
      } catch (XMLStreamException sex) {
        msg = null; // fine!
      } catch (Throwable t) {
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on END_ELEMENT and hitting non-ws text; got: "
                + t;
      }
      if (msg != null) {
        fail(msg);
      }
      er.close();
    }
  }
Ejemplo n.º 4
0
 static XmlResourceValue parseStyle(XMLEventReader eventReader, StartElement start)
     throws XMLStreamException {
   Map<String, String> values = new HashMap<>();
   for (XMLEvent element = eventReader.nextTag();
       !isEndTag(element, TAG_STYLE);
       element = eventReader.nextTag()) {
     if (isItem(element)) {
       values.put(getElementName(element.asStartElement()), eventReader.getElementText());
     }
   }
   return StyleXmlResourceValue.of(parseReferenceFromElementAttribute(start, ATTR_PARENT), values);
 }
Ejemplo n.º 5
0
 static XmlResourceValue parsePlurals(XMLEventReader eventReader) throws XMLStreamException {
   Map<String, String> values = new HashMap<>();
   for (XMLEvent element = eventReader.nextTag();
       !isEndTag(element, TAG_PLURALS);
       element = eventReader.nextTag()) {
     if (isItem(element)) {
       values.put(
           getElementAttributeByName(element.asStartElement(), ATTR_QUANTITY),
           eventReader.getElementText());
     }
   }
   return PluralXmlResourceValue.of(values);
 }
Ejemplo n.º 6
0
 static void parseDeclareStyleable(
     FullyQualifiedName.Factory fqnFactory,
     Path path,
     KeyValueConsumer<DataKey, DataResource> overwritingConsumer,
     KeyValueConsumer<DataKey, DataResource> nonOverwritingConsumer,
     XMLEventReader eventReader,
     StartElement start)
     throws XMLStreamException {
   List<String> members = new ArrayList<>();
   for (XMLEvent element = eventReader.nextTag();
       !isEndTag(element, TAG_DECLARE_STYLEABLE);
       element = eventReader.nextTag()) {
     if (isStartTag(element, TAG_ATTR)) {
       StartElement attr = element.asStartElement();
       members.add(getElementName(attr));
       overwritingConsumer.consume(
           fqnFactory.create(ResourceType.ATTR, getElementName(attr)),
           DataResourceXml.of(path, parseAttr(eventReader, start)));
     }
   }
   nonOverwritingConsumer.consume(
       fqnFactory.create(ResourceType.STYLEABLE, getElementName(start)),
       DataResourceXml.of(path, StyleableXmlResourceValue.of(members)));
 }
Ejemplo n.º 7
0
  public static void main(String[] args) throws XMLStreamException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
      InputStream stream = new FileInputStream("data/xml/log.xml");

      if ((new File("data/xml/log.xml")).exists()) {
        System.out.println("EXISTS");
      }
      XMLEventReader eventReader = inputFactory.createXMLEventReader(stream);
      while (eventReader.hasNext()) {
        XMLEvent tag = eventReader.nextTag();
        if (tag.isStartElement()
            && tag.asStartElement().getName().getLocalPart().equalsIgnoreCase("request")) {
          System.out.println("request");
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 8
0
  public void rewriteEndpoint(Reader reader, Writer writer, int port, Exchange exc)
      throws XMLStreamException {
    XMLEventReader parser = inputFactory.createXMLEventReader(reader);
    XMLEventWriter eventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(writer);

    String id = null;
    String url = null;

    skip:
    while (parser.hasNext()) {
      XMLEvent e = parser.nextEvent();

      if (e.isStartElement()) {
        if (isReplyTo(e.asStartElement())) {
          while (e.isStartElement() || !isReplyTo(e.asEndElement())) {
            if (e.isStartElement() && isAddress(e.asStartElement())) {
              url = parser.getElementText();
              addRewrittenAddressElement(eventWriter, url, port, e.asStartElement());

              continue skip;
            }

            eventWriter.add(e);
            e = parser.nextTag();
          }
        }

        if (isMessageId(e.asStartElement())) {
          id = parser.getElementText();
          exc.setProperty("messageId", id);
          addMessageIdElement(eventWriter, id, e.asStartElement());

          continue skip;
        }
      }

      eventWriter.add(e);
    }

    registry.register(id, url);
  }