Java Code Examples for javax.xml.stream.XMLStreamReader#hasText()

The following examples show how to use javax.xml.stream.XMLStreamReader#hasText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlHelperTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void xxeWithoutProtection() throws Exception {
  InputStream content = new ByteArrayInputStream(XML_XXE.getBytes("UTF-8"));
  XMLStreamReader streamReader = createStreamReaderWithExternalEntitySupport(content);

  boolean foundExternalEntity = false;

  while (streamReader.hasNext()) {
    streamReader.next();

    if (streamReader.hasText() && "some text".equals(streamReader.getText())) {
      foundExternalEntity = true;
      break;
    }

  }
  assertTrue(foundExternalEntity);
}
 
Example 2
Source File: CaldavConnection.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public void handleProp(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, "prop")) {
        reader.next();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            String tagText = null;
            if ("displayname".equals(tagLocalName) || reader.hasText()) {
                tagText = XMLStreamUtil.getElementText(reader);
            }
            properties.put(tagLocalName, tagText);
        }
    }
}
 
Example 3
Source File: EntityTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void handleStartElement(XMLStreamReader xmlr) {
    output += "<";
    output += xmlr.getLocalName();
    if (xmlr.hasText())
        output += xmlr.getText();
    printAttributes(xmlr);
    output += ">";
}
 
Example 4
Source File: BasicJavaClientREST.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
/**
 * Convert XMLStreamReader To String
 * 
 * @param XMLStreamReader
 * @return String
 * @throws XMLStreamException
 *           , TransformerException, IOException,
 *           ParserConfigurationException, SAXException
 */
public String convertXMLStreamReaderToString(XMLStreamReader reader) throws XMLStreamException, TransformerException, IOException, ParserConfigurationException, SAXException {
  String str = null;
  while (reader.hasNext())
  {
    reader.next();
    int a = reader.getEventType();
    if (reader.hasText())
      if (reader.getText() != "null")
        str = str + reader.getText().trim();
  }
  return str;
}
 
Example 5
Source File: BaseStreamTest.java    From woodstox with Apache License 2.0 5 votes vote down vote up
/**
 * Method that will iterate through contents of an XML document
 * using specified stream reader; will also access some of data
 * to make sure reader reads most of lazy-loadable data.
 * Method is usually called to try to get an exception for invalid
 * content.
 *
 * @return Dummy value calculated on contents; used to make sure
 *   no dead code is eliminated
 */
protected int streamThrough(XMLStreamReader sr)
    throws XMLStreamException
{
    int result = 0;

    assertNotNull(sr);
    try {
        while (sr.hasNext()) {
            int type = sr.next();
            result += type;
            if (sr.hasText()) {
                /* will also do basic verification for text content, to 
                 * see that all text accessor methods return same content
                 */
                result += getAndVerifyText(sr).hashCode();
            }
            if (sr.hasName()) {
                QName n = sr.getName();
                assertNotNull(n);
                result += n.hashCode();
            }
        }
    } catch (RuntimeException rex) {
        // Let's try to find a nested XMLStreamException, if possible
        Throwable t = rex;
        while (t != null) {
            t = t.getCause();
            if (t instanceof XMLStreamException) {
                throw (XMLStreamException) t;
            }
        }
        // Nope, just a runtime exception
        throw rex;
    }
        
    return result;
}
 
Example 6
Source File: ParsingUtils.java    From galleon with Apache License 2.0 4 votes vote down vote up
public static XMLStreamException unexpectedContent(final XMLStreamReader reader) {
    final String kind;
    switch (reader.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            kind = "attribute";
            break;
        case XMLStreamConstants.CDATA:
            kind = "cdata";
            break;
        case XMLStreamConstants.CHARACTERS:
            kind = "characters";
            break;
        case XMLStreamConstants.COMMENT:
            kind = "comment";
            break;
        case XMLStreamConstants.DTD:
            kind = "dtd";
            break;
        case XMLStreamConstants.END_DOCUMENT:
            kind = "document end";
            break;
        case XMLStreamConstants.END_ELEMENT:
            kind = "element end";
            break;
        case XMLStreamConstants.ENTITY_DECLARATION:
            kind = "entity declaration";
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            kind = "entity ref";
            break;
        case XMLStreamConstants.NAMESPACE:
            kind = "namespace";
            break;
        case XMLStreamConstants.NOTATION_DECLARATION:
            kind = "notation declaration";
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            kind = "processing instruction";
            break;
        case XMLStreamConstants.SPACE:
            kind = "whitespace";
            break;
        case XMLStreamConstants.START_DOCUMENT:
            kind = "document start";
            break;
        case XMLStreamConstants.START_ELEMENT:
            kind = "element start";
            break;
        default:
            kind = "unknown";
            break;
    }

    return new XMLStreamException("unexpected content: " + kind + (reader.hasName() ? reader.getName() : null) +
            (reader.hasText() ? reader.getText() : null), reader.getLocation());
}
 
Example 7
Source File: EntityTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void handleComment(XMLStreamReader xmlr) {
    if (xmlr.hasText())
        output += xmlr.getText();
}
 
Example 8
Source File: EntityTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void handleCharacters(XMLStreamReader xmlr) {
    if (xmlr.hasText())
        output += xmlr.getText();
}