Example usage for javax.xml.stream XMLStreamReader getText

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

Introduction

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

Prototype

public String getText();

Source Link

Document

Returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD.

Usage

From source file:org.restcomm.connect.interpreter.rcml.Parser.java

private void text(final Stack<Tag.Builder> builders, final XMLStreamReader stream) {
    if (!stream.isWhiteSpace()) {
        // Read the text.
        final Tag.Builder builder = builders.peek();
        final String text = stream.getText().trim();
        builder.setText(text);// w ww. ja v  a  2s . c  o m
    }
}

From source file:org.reusables.dbunit.autocomplete.AutoCompletionRules.java

private void parseColumnValue(final XMLStreamReader parser, final AutoCompletionColumn currentColumn) {
    if (currentColumn != null) {
        currentColumn.setValue(parser.getText());
    }/*from   w  ww. j  a va 2  s .  c o  m*/
}

From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java

private static void updateFile(File configFile, Map<String, PropertySimple> allProps)
        throws IOException, InterruptedException, XMLStreamException {
    InputStream in = null;//  w  w  w .  j a  v  a  2 s . co  m
    XMLStreamReader rdr = null;

    OutputStream out = null;
    XMLStreamWriter outWrt = null;

    try {
        Set<String> processedPropertyNames = new HashSet<String>();

        in = new BufferedInputStream(new FileInputStream(configFile));
        rdr = XML_INPUT_FACTORY.createXMLStreamReader(in);

        File tmpFile = File.createTempFile("hadoop-plugin", null);
        out = new FileOutputStream(tmpFile);
        outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);

        ByteArrayOutputStream stash = new ByteArrayOutputStream();
        XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash);
        boolean outputActive = true;

        outWrt.writeStartDocument();

        while (rdr.hasNext()) {
            int event = rdr.next();

            XMLStreamWriter wrt = outputActive ? outWrt : stashWrt;

            switch (event) {
            case XMLStreamConstants.ATTRIBUTE:
                break;
            case XMLStreamConstants.CDATA:
                wrt.writeCData(rdr.getText());
                break;
            case XMLStreamConstants.CHARACTERS:
                wrt.writeCharacters(rdr.getText());
                break;
            case XMLStreamConstants.COMMENT:
                wrt.writeComment(rdr.getText());
                break;
            case XMLStreamConstants.DTD:
                wrt.writeDTD(rdr.getText());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                wrt.writeEndDocument();
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    String encoding = rdr.getEncoding();
                    if (encoding == null) {
                        encoding = "UTF-8";
                    }

                    String propertyTagSoFar = Charset.forName(encoding)
                            .decode(ByteBuffer.wrap(stash.toByteArray())).toString();
                    DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps);

                    //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write.
                    outWrt.flush();
                    out.write(propAndTag.updatedTag.getBytes("UTF-8"));

                    processedPropertyNames.add(propAndTag.propertyName);

                    //reset stuff
                    stash.reset();
                    wrt = outWrt;
                    outputActive = true;
                } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    //now add the new props
                    for (String prop : processedPropertyNames) {
                        allProps.remove(prop);
                    }

                    for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) {
                        outWrt.writeStartElement(PROPERTY_TAG_NAME);

                        outWrt.writeStartElement(NAME_TAG_NAME);
                        outWrt.writeCharacters(e.getKey());
                        outWrt.writeEndElement();

                        outWrt.writeStartElement(VALUE_TAG_NAME);
                        outWrt.writeCharacters(e.getValue().getStringValue());
                        outWrt.writeEndElement();

                        outWrt.writeEndElement();
                    }
                }
                wrt.writeEndElement();
                break;
            case XMLStreamConstants.ENTITY_DECLARATION:
                //XXX could not find what to do with this
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                wrt.writeEntityRef(rdr.getText());
                break;
            case XMLStreamConstants.NAMESPACE:
                for (int i = 0; i < rdr.getNamespaceCount(); ++i) {
                    wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i));
                }
                break;
            case XMLStreamConstants.NOTATION_DECLARATION:
                //XXX could not find what to do with this
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData());
                break;
            case XMLStreamConstants.SPACE:
                wrt.writeCharacters(rdr.getText());
                break;
            case XMLStreamConstants.START_DOCUMENT:
                //this seems to be never called for some strange reason
                //wrt.writeStartDocument();
                break;
            case XMLStreamConstants.START_ELEMENT:
                wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(),
                        rdr.getName().getNamespaceURI());

                for (int i = 0; i < rdr.getAttributeCount(); ++i) {
                    wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i),
                            rdr.getAttributeLocalName(i), rdr.getAttributeValue(i));
                }

                if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    wrt.writeCharacters("");
                    outputActive = false;
                }
                break;
            }
        }

        outWrt.flush();
        out.flush();
        out.close();

        in.close();

        //now copy the temp file in the place of the original one
        FileUtil.copyFile(tmpFile, configFile);
    } finally {
        rdr.close();

        outWrt.flush();
        outWrt.close();

        try {
            in.close();
        } finally {
            out.flush();
            out.close();
        }
    }
}

From source file:org.slc.sli.api.resources.config.StAXMsgBodyReader.java

/**
 * Reads individual elements inside of a XML Document
 * @param reader xml reader//from   w  w  w. jav a  2s.  c  o m
 * @return a pair representing the Object value of the element (Left) as well as
 *         a boolean value representing either true (part of a list) or false
 *         (single value)
 * @throws XMLStreamException on malformed XML
 */
private static final Pair<Object, Boolean> readElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName elementName = reader.getName();
    final StringBuilder sb = new StringBuilder();
    final Map<String, Object> data = new HashMap<String, Object>();
    final Boolean member = isMember(reader);
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            final QName key = reader.getName();
            final Pair<Object, Boolean> elem = readElement(reader);
            addToElements(key.getLocalPart(), elem, data);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (elementName.equals(reader.getName())) {
                if (data.size() > 0) {
                    return new ImmutablePair<Object, Boolean>(data, member);
                } else {
                    return new ImmutablePair<Object, Boolean>(sb.toString(), member);
                }
            } else {
                throw new XMLStreamException(reader.getName().getLocalPart());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            sb.append(reader.getText());
            break;
        }
        default: {
            throw new XMLStreamException();
        }
        }
    }
    throw new XMLStreamException();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Documentation readDocumentation(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.DOCUMENTATION, reader);
    final String title = getTitle(reader, null);
    final String lang = getLang(reader, null);
    final List<DmNode> content = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();/*from ww w .j  av a2  s.co m*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            content.add(readMixedElement(reader));
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.DOCUMENTATION, reader);
            return new Documentation(title, lang, content);
        }
        case XMLStreamConstants.CHARACTERS: {
            content.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            content.add(new DmComment(reader.getText()));
            break;
        }
        case XMLStreamConstants.PROCESSING_INSTRUCTION: {
            content.add(new DmProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final DmElement readMixedElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName name = reader.getName();
    final List<DmNode> children = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();/*from w w  w. ja  va2s. c  o m*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            children.add(readMixedElement(reader));
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (name.equals(reader.getName())) {
                return new DmElement(name, children);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Assume that the element will coalesce text nodes.
            children.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            // Ignore for now.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final String readStringContent(final QName elementName, final XMLStreamReader reader)
        throws XMLStreamException {
    assertStartElement(reader);// w w  w.  j a  v  a  2s  .co m
    assertName(elementName, reader);
    final StringBuilder sb = new StringBuilder();
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            throw new AssertionError(reader.getLocalName());
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(elementName, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            sb.append(reader.getText());
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return sb.toString().trim();
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiMappingStatus readStatus(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);//from   ww w .j  a va 2s  .co m
    assertName(XmiMappingConstants.STATUS, reader);
    final StringBuilder sb = new StringBuilder();
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            throw new AssertionError(reader.getLocalName());
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiMappingConstants.STATUS, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            sb.append(reader.getText());
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    // Intentionally make the coupling loose between the serialization and
    // the enumeration.
    final String value = sb.toString().trim();
    if (XmiMappingValues.STATUS_MATCH.equals(value)) {
        return XmiMappingStatus.MATCH;
    } else if (XmiMappingValues.STATUS_IGNORABLE.equals(value)) {
        return XmiMappingStatus.IGNORABLE;
    } else if (XmiMappingValues.STATUS_ALIGN.equals(value)) {
        return XmiMappingStatus.ALIGN;
    } else if (XmiMappingValues.STATUS_UNKNOWN.equals(value)) {
        return XmiMappingStatus.UNKNOWN;
    } else if (XmiMappingValues.STATUS_TRANSIENT.equals(value)) {
        return XmiMappingStatus.TRANSIENT;
    } else if (XmiMappingValues.STATUS_BUG.equals(value)) {
        return XmiMappingStatus.BUG;
    } else if (XmiMappingValues.STATUS_FINANCIAL.equals(value)) {
        return XmiMappingStatus.FINANCIAL;
    } else {
        throw new AssertionError(XmiMappingConstants.STATUS + " : " + value);
    }
}

From source file:org.slc.sli.modeling.xmi.reader.XmiReader.java

protected static final String readTaggedValueData(final XMLStreamReader reader) throws XMLStreamException {
    assertName(XmiElementName.TAGGED_VALUE_DOT_DATA_VALUE, reader);
    final StringBuilder sb = new StringBuilder();
    while (reader.hasNext()) {
        reader.next();//  www  .ja  v a  2s . c om
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            throw new AssertionError(reader.getLocalName());
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiElementName.TAGGED_VALUE_DOT_DATA_VALUE, reader);
            return collapseWhitespace(sb.toString());
        }
        case XMLStreamConstants.CHARACTERS: {
            sb.append(reader.getText());
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new Object from the current element being read in the specified
 * <code>StreamReader</code>.
 * /*from w w  w .  j ava 2 s  .  co  m*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the created Object.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child.
 * @see #parseArrayElement(XMLStreamReader)
 * @see #parseStructElement(XMLStreamReader)
 */
protected final XmlRpcElement parseValueElement(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int event = reader.next();
        String localName = null;

        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.ARRAY.equals(localName)) {
                return parseArrayElement(reader);

            } else if (XmlRpcElementNames.BASE_64.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBase64(source);

            } else if (XmlRpcElementNames.BOOLEAN.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBoolean(source);

            } else if (XmlRpcElementNames.DATE_TIME.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDateTime(source);

            } else if (XmlRpcElementNames.DOUBLE.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDouble(source);

            } else if (XmlRpcElementNames.I4.equals(localName) || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcInteger(source);

            } else if (XmlRpcElementNames.STRING.equals(localName)
                    || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcString(source);

            } else if (XmlRpcElementNames.STRUCT.equals(localName)) {
                return parseStructElement(reader);

            } else {
                XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
            }

        case XMLStreamConstants.CHARACTERS:
            String source = reader.getText();
            return new XmlRpcString(source);
        }
    }

    // we should not reach this point.
    return null;
}