Example usage for javax.xml.stream XMLStreamReader hasText

List of usage examples for javax.xml.stream XMLStreamReader hasText

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader hasText.

Prototype

public boolean hasText();

Source Link

Document

Return a boolean indicating whether the current event has text.

Usage

From source file:Main.java

public static void printText(XMLStreamReader xmlr) {
    if (xmlr.hasText()) {
        System.out.print(xmlr.getText());
    }/*from w  w w.  j  a va 2  s .  com*/
}

From source file:Main.java

private static void printText(XMLStreamReader xmlr) {
    if (xmlr.hasText()) {
        System.out.println("HAS TEXT: " + xmlr.getText());
    } else {/*from w  ww .j  a va 2s. co m*/
        System.out.println("HAS NO TEXT");
    }
}

From source file:StaxCursorTest.java

private static void printText(XMLStreamReader xmlr) {
    if (xmlr.hasText()) {
        System.out.print(xmlr.getText());
    }//  w w  w  .  jav a  2  s .c o  m
}

From source file:edu.utah.further.core.xml.stax.XmlStreamElementPrinter.java

@Override
public void visitProcessingInstruction(final XMLStreamReader xmlReader) {
    os.print("<?");
    if (xmlReader.hasText())
        os.print(xmlReader.getText());//from   w  w  w . j  a va 2s. c o  m
    os.print("?>");
}

From source file:edu.utah.further.core.xml.stax.XmlStreamElementPrinter.java

@Override
public void visitComment(final XMLStreamReader xmlReader) {
    if (isPrintComments()) {
        os.print("<!--");
        if (xmlReader.hasText())
            os.print(xmlReader.getText());
        os.print("-->");
    }//w ww. ja  v a  2  s  .  co m
}

From source file:edu.utah.further.core.xml.stax.XmlStreamElementPrinter.java

@Override
public void visitEntityReference(final XMLStreamReader xmlReader) {
    final String str = xmlReader.getLocalName() + "=";
    os.print(str);//from  w w  w. j a  va  2 s .  c  o m
    if (xmlReader.hasText()) {
        os.print("[" + xmlReader.getText() + "]");
    }
}

From source file:Main.java

public static String parse(XMLStreamReader reader) {
    StringBuffer result = new StringBuffer();
    if (reader != null) {
        try {/*from  w  w w.  j a  va  2s  . c o  m*/
            while (reader.hasNext()) {
                switch (reader.getEventType()) {

                case XMLStreamConstants.START_ELEMENT:
                    result.append("<");
                    printName(reader, result);
                    printNamespaces(reader, result);
                    printAttributes(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    result.append("</");
                    printName(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.SPACE:

                case XMLStreamConstants.CHARACTERS:
                    int start = reader.getTextStart();
                    int length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    break;

                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    result.append("<?");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("?>");
                    break;

                case XMLStreamConstants.CDATA:
                    result.append("<![CDATA[");
                    start = reader.getTextStart();
                    length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    result.append("]]>");
                    break;

                case XMLStreamConstants.COMMENT:
                    result.append("<!--");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("-->");
                    break;

                case XMLStreamConstants.ENTITY_REFERENCE:
                    result.append(reader.getLocalName()).append("=");
                    if (reader.hasText())
                        result.append("[").append(reader.getText()).append("]");
                    break;

                case XMLStreamConstants.START_DOCUMENT:
                    result.append("<?xml");
                    result.append(" version='").append(reader.getVersion()).append("'");
                    result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'");
                    if (reader.isStandalone())
                        result.append(" standalone='yes'");
                    else
                        result.append(" standalone='no'");
                    result.append("?>");
                    break;
                }
                reader.next();
            } // end while
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
        }
    }
    return result.toString();
}

From source file:XmlStreamUtils.java

public static String printEvent(XMLStreamReader xmlr) {
    StringBuffer b = new StringBuffer();
    b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber()
            + "] ");
    b.append(getName(xmlr.getEventType()));
    b.append(" [");
    switch (xmlr.getEventType()) {
    case XMLStreamReader.START_ELEMENT:
        b.append("<");
        printName(xmlr, b);/*from w w  w  .j  av a2 s .  co  m*/
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }

        for (int i = 0; i < xmlr.getAttributeCount(); i++) {
            b.append(" ");
            printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i),
                    b);
            b.append("=\"");
            b.append(xmlr.getAttributeValue(i));
            b.append("\"");
        }

        b.append(">");
        break;
    case XMLStreamReader.END_ELEMENT:
        b.append("</");
        printName(xmlr, b);
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }
        b.append(">");
        break;
    case XMLStreamReader.SPACE:
    case XMLStreamReader.CHARACTERS:
        // b.append(xmlr.getText());
        int start = xmlr.getTextStart();
        int length = xmlr.getTextLength();
        b.append(new String(xmlr.getTextCharacters(), start, length));
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        String target = xmlr.getPITarget();
        if (target == null)
            target = "";
        String data = xmlr.getPIData();
        if (data == null)
            data = "";
        b.append("<?");
        b.append(target + " " + data);
        b.append("?>");
        break;
    case XMLStreamReader.CDATA:
        b.append("<![CDATA[");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("]]&gt;");
        break;

    case XMLStreamReader.COMMENT:
        b.append("<!--");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("-->");
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        b.append(xmlr.getLocalName() + "=");
        if (xmlr.hasText())
            b.append("[" + xmlr.getText() + "]");
        break;
    case XMLStreamReader.START_DOCUMENT:
        b.append("<?xml");
        b.append(" version='" + xmlr.getVersion() + "'");
        b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'");
        if (xmlr.isStandalone())
            b.append(" standalone='yes'");
        else
            b.append(" standalone='no'");
        b.append("?>");
        break;

    }
    b.append("]");
    return b.toString();
}

From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java

/**
 * Convert XMLStreamReader To String//from  w ww.j  a  v a 2 s  .  c om
 * @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;
}

From source file:org.openanzo.services.serialization.XMLBackupReader.java

/**
 * Parse the data within the reader, passing the results to the IRepositoryHandler
 * /* w  w  w.j  a  v a 2 s  .c o m*/
 * @param reader
 *            reader containing the data
 * @param handler
 *            Handler which will handle the elements within the data
 * @throws AnzoException
 */
private void parseBackup(Reader reader, final IBackupHandler handler) throws AnzoException {
    try {
        XMLStreamReader parser = XMLFactoryFinder.getXMLInputFactory().createXMLStreamReader(reader);

        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    graphUri = parser.getAttributeValue(null, SerializationConstants.namedGraphUri);
                    String meta = parser.getAttributeValue(null, SerializationConstants.metadataGraphUri);
                    String uuidString = parser.getAttributeValue(null, SerializationConstants.namedGraphUUID);
                    namedGraphUri = Constants.valueFactory.createURI(graphUri);
                    metaURI = Constants.valueFactory.createURI(meta);
                    uuid = Constants.valueFactory.createURI(uuidString);
                    revisioned = Boolean
                            .parseBoolean(parser.getAttributeValue(null, SerializationConstants.revisioned));
                } else if (SerializationConstants.revisions.equals(parser.getLocalName())) {
                    revisions = new ArrayList<BackupRevision>();
                } else if (SerializationConstants.revisionInfo.equals(parser.getLocalName())) {
                    long revision = Long
                            .parseLong(parser.getAttributeValue(null, SerializationConstants.revision));
                    Long start = Long.valueOf(parser.getAttributeValue(null, SerializationConstants.start));
                    String endString = parser.getAttributeValue(null, SerializationConstants.end);
                    Long end = (endString != null) ? Long.valueOf(endString) : -1;
                    lastModified = Constants.valueFactory
                            .createURI(parser.getAttributeValue(null, SerializationConstants.lastModifiedBy));
                    revisions.add(new BackupRevision(revision, start, end, lastModified));
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                    if (processGraph) {
                        if (revisioned) {
                            String startString = parser.getAttributeValue(null, SerializationConstants.start);
                            String endString = parser.getAttributeValue(null, SerializationConstants.end);
                            if (startString != null)
                                start = Long.valueOf(startString);
                            if (endString != null)
                                end = Long.valueOf(endString);
                        }
                    }
                } else if (SerializationConstants.statements.equals(parser.getLocalName())) {
                    if (processGraph) {
                        metadata = !parser.getAttributeValue(null, SerializationConstants.namedGraphUri)
                                .equals(graphUri);
                    }
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    if (processGraph) {
                        nodeType = parser.getAttributeValue(null, SerializationConstants.subjectType);
                    }
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    if (processGraph) {
                        nodeType = parser.getAttributeValue(null, SerializationConstants.objectType);
                        language = parser.getAttributeValue(null, SerializationConstants.language);
                        datatype = parser.getAttributeValue(null, SerializationConstants.dataType);
                    }
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (SerializationConstants.namedGraph.equals(parser.getLocalName())) {
                    graphUri = null;
                    namedGraphUri = null;
                    metaURI = null;
                    uuid = null;
                    revisioned = true;
                    revisions = null;
                } else if (SerializationConstants.revisionInfo.equals(parser.getLocalName())) {
                } else if (SerializationConstants.revisions.equals(parser.getLocalName())) {
                    processGraph = handler.handleNamedGraph(revisioned, namedGraphUri, metaURI, uuid,
                            revisions);
                    revisions = null;
                } else if (SerializationConstants.statement.equals(parser.getLocalName())) {
                    if (processGraph) {
                        handler.handleStatement(metadata, revisioned,
                                Constants.valueFactory.createStatement(currentSubject, currentPredicate,
                                        currentObject, currentNamedGraphURI),
                                start, end);
                        start = null;
                        end = null;
                    }
                } else if (SerializationConstants.subject.equals(parser.getLocalName())) {
                    if (processGraph) {
                        if (NodeType.BNODE.name().equals(nodeType)) {
                            currentSubject = Constants.valueFactory.createBNode(currentValue);
                        } else {
                            currentSubject = Constants.valueFactory.createURI(currentValue);
                        }
                        currentValue = null;
                        nodeType = null;
                    }
                } else if (SerializationConstants.predicate.equals(parser.getLocalName())) {
                    if (processGraph) {
                        currentPredicate = Constants.valueFactory.createURI(currentValue);
                        currentValue = null;
                    }
                } else if (SerializationConstants.object.equals(parser.getLocalName())) {
                    if (processGraph) {
                        if (NodeType.BNODE.name().equals(nodeType)) {
                            currentObject = Constants.valueFactory.createBNode(currentValue);
                        } else if (NodeType.URI.name().equals(nodeType)) {
                            currentObject = Constants.valueFactory.createURI(currentValue);
                        } else if (NodeType.LITERAL.name().equals(nodeType)) {
                            if (currentValue == null) {
                                currentValue = "";
                            } else if (Base64
                                    .isArrayByteBase64(currentValue.getBytes(Constants.byteEncoding))) {
                                currentValue = new String(
                                        Base64.decodeBase64(currentValue.getBytes(Constants.byteEncoding)),
                                        Constants.byteEncoding);
                            }
                            if (datatype != null) {
                                currentObject = Constants.valueFactory.createLiteral(currentValue,
                                        Constants.valueFactory.createURI(datatype));
                            } else if (language != null) {
                                currentObject = Constants.valueFactory.createLiteral(currentValue, language);
                            } else {
                                currentObject = Constants.valueFactory.createLiteral(currentValue);
                            }
                        }
                        currentValue = null;
                        nodeType = null;
                        language = null;
                        datatype = null;
                    }
                } else if (SerializationConstants.namedGraphUri.equals(parser.getLocalName())) {
                    if (processGraph) {
                        currentNamedGraphURI = Constants.valueFactory.createURI(currentValue);
                        currentValue = null;
                    }
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                if (parser.hasText())
                    currentValue = parser.getText();
                break;
            case XMLStreamConstants.CDATA:
                if (parser.hasText())
                    currentValue = parser.getText();
                break;
            }
        }
        parser.close();
    } catch (XMLStreamException ex) {
        throw new AnzoException(ExceptionConstants.IO.READ_ERROR, ex, ex.getMessage());
    } catch (UnsupportedEncodingException uee) {
        throw new AnzoException(ExceptionConstants.IO.ENCODING_ERROR, uee);
    }
}