Example usage for javax.xml.stream XMLStreamConstants CHARACTERS

List of usage examples for javax.xml.stream XMLStreamConstants CHARACTERS

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants CHARACTERS.

Prototype

int CHARACTERS

To view the source code for javax.xml.stream XMLStreamConstants CHARACTERS.

Click Source Link

Document

Indicates an event is characters

Usage

From source file:org.pentaho.di.trans.steps.xmlinputstream.XMLInputStream.java

private Object[] processEvent() throws KettleException {

    Object[] outputRowData = RowDataUtil.allocateRowData(data.outputRowMeta.size());
    XMLEvent e = null;//w w  w. j  av  a2  s. co  m
    try {
        e = data.xmlEventReader.nextEvent();
    } catch (XMLStreamException ex) {
        throw new KettleException(ex);
    }

    int eventType = e.getEventType();
    if (data.pos_xml_data_type_numeric != -1) {
        outputRowData[data.pos_xml_data_type_numeric] = new Long(eventType);
    }
    if (data.pos_xml_data_type_description != -1) {
        if (eventType == 0 || eventType > eventDescription.length) {
            // unknown eventType
            outputRowData[data.pos_xml_data_type_description] = eventDescription[0] + "(" + eventType + ")";
        } else {
            outputRowData[data.pos_xml_data_type_description] = eventDescription[eventType];
        }
    }
    if (data.pos_xml_location_line != -1) {
        outputRowData[data.pos_xml_location_line] = new Long(e.getLocation().getLineNumber());
    }
    if (data.pos_xml_location_column != -1) {
        outputRowData[data.pos_xml_location_column] = new Long(e.getLocation().getColumnNumber());
    }

    switch (eventType) {

    case XMLStreamConstants.START_ELEMENT:
        data.elementLevel++;
        if (data.elementLevel > PARENT_ID_ALLOCATE_SIZE - 1) {
            throw new KettleException(BaseMessages.getString(PKG, "XMLInputStream.Log.TooManyNestedElements",
                    PARENT_ID_ALLOCATE_SIZE));
        }
        if (data.elementParentID[data.elementLevel] == null) {
            data.elementParentID[data.elementLevel] = data.elementID;
        }
        data.elementID++;
        data.elementLevelID[data.elementLevel] = data.elementID;

        String xml_data_name;
        if (meta.isEnableNamespaces()) {
            String prefix = e.asStartElement().getName().getPrefix();
            if (Utils.isEmpty(prefix)) {
                xml_data_name = e.asStartElement().getName().getLocalPart();
            } else { // add namespace prefix:
                xml_data_name = prefix + ":" + e.asStartElement().getName().getLocalPart();
            }
        } else {
            xml_data_name = e.asStartElement().getName().getLocalPart();
        }
        if (data.pos_xml_data_name >= 0) {
            outputRowData[data.pos_xml_data_name] = xml_data_name;
        }
        // store the name
        data.elementName[data.elementLevel] = xml_data_name;
        // store simple path
        data.elementPath[data.elementLevel] = data.elementPath[data.elementLevel - 1] + "/" + xml_data_name;

        // write Namespaces out
        if (meta.isEnableNamespaces()) {
            outputRowData = parseNamespaces(outputRowData, e);
        }

        // write Attributes out
        outputRowData = parseAttributes(outputRowData, e);

        break;

    case XMLStreamConstants.END_ELEMENT:
        parseEndElement(outputRowData, e.asEndElement());
        putRowOut(outputRowData);
        data.elementParentID[data.elementLevel + 1] = null;
        data.elementLevel--;
        outputRowData = null; // continue
        break;

    case XMLStreamConstants.SPACE:
        outputRowData = null; // ignore & continue
        break;

    case XMLStreamConstants.CHARACTERS:
    case XMLStreamConstants.CDATA:
        if (data.pos_xml_data_name >= 0) {
            outputRowData[data.pos_xml_data_name] = data.elementName[data.elementLevel];
        }
        String xml_data_value = e.asCharacters().getData();
        if (data.pos_xml_data_value >= 0) {
            if (meta.isEnableTrim()) {
                // optional trim is also eliminating white spaces, tab, cr, lf
                xml_data_value = Const.trim(xml_data_value);
            }
            outputRowData[data.pos_xml_data_value] = xml_data_value;
        }

        if (data.pos_xml_data_value < 0 || Utils.isEmpty((String) outputRowData[data.pos_xml_data_value])) {
            outputRowData = null; // ignore & continue
        }
        break;

    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        outputRowData = null; // ignore & continue
        // TODO test if possible
        break;

    case XMLStreamConstants.COMMENT:
        outputRowData = null; // ignore & continue
        // TODO test if possible
        break;

    case XMLStreamConstants.ENTITY_REFERENCE:
        // should be resolved by default
        outputRowData = null; // ignore & continue
        break;

    case XMLStreamConstants.START_DOCUMENT:
        // just get this information out
        break;

    case XMLStreamConstants.END_DOCUMENT:
        // just get this information out
        break;

    default:
        logBasic("Event:" + eventType);
        outputRowData = null; // ignore & continue
    }

    return outputRowData;
}

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

private void parseTable(final XMLStreamReader parser) throws XMLStreamException {
    final String name = getName(parser);
    final Map<String, AutoCompletionColumn> columns = addTable(name);
    AutoCompletionColumn currentColumn = null;

    for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            currentColumn = parseColumn(parser);
            columns.put(currentColumn.getName().toLowerCase(), currentColumn);
        } else if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) {
            parseColumnValue(parser, currentColumn);
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            currentColumn = null;// www  .  j  a va 2s.c  o m
            if (ELEM_TABLE.equals(parser.getLocalName())) {
                return;
            }
        }
    }
}

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;/*from www. j  av a2s .  c om*/
    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

/**
 * Helper method for digesting XML documents
 * @param reader XML reader/*  w  w  w  .j a  v a  2 s.c o m*/
 * @return EntityBody representation that corresponds to the xml
 * @throws XMLStreamException on malformed XML
 */
private static final EntityBody readDocument(final XMLStreamReader reader) throws XMLStreamException {
    if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
        EntityBody body = null;
        while (reader.hasNext()) {
            reader.next();
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT: {
                body = readDocumentElement(reader);
                return body;
            }
            case XMLStreamConstants.END_DOCUMENT: {
                return body;
            }
            case XMLStreamConstants.CHARACTERS: {
                // Ignore
                break;
            }
            default: {
                throw new XMLStreamException();
            }
            }
        }
    } else {
        throw new XMLStreamException(reader.getLocalName());
    }
    throw new XMLStreamException();
}

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

/**
 * Reads everything under the main document wrapper tag
 * @param reader Reader that we have for XML
 * @return EntityBody representation of the document
 * @throws XMLStreamException on malformed XML
 *///from  ww  w. ja  v  a  2s  .co  m
private static final EntityBody readDocumentElement(final XMLStreamReader reader) throws XMLStreamException {
    final Map<String, Object> elements = new HashMap<String, Object>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            final Pair<Object, Boolean> memberDataPair = readElement(reader);
            addToElements(reader.getLocalName(), memberDataPair, elements);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            return new EntityBody(elements);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore
            break;
        }
        default: {
            throw new XMLStreamException();
        }
        }
    }
    throw new XMLStreamException();
}

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 . j av a 2  s.  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 Application readApplication(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.APPLICATION, reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    Grammars grammars = null;/*  w ww  .  j  a  va 2 s .  co m*/
    Resources resources = null;
    final List<ResourceType> resourceTypes = new LinkedList<ResourceType>();
    final List<Method> methods = new LinkedList<Method>();
    final List<Representation> representations = new LinkedList<Representation>();
    final List<Representation> faults = new LinkedList<Representation>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
                break;
            } else if (match(WadlElementName.GRAMMARS, reader)) {
                grammars = assertNotNull(readGrammars(reader));
                break;
            } else if (match(WadlElementName.RESOURCES, reader)) {
                resources = assertNotNull(readResources(reader));
                break;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.APPLICATION, reader);
            return new Application(doc, grammars, resources, resourceTypes, methods, representations, faults);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

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   w  w  w.ja  v a  2s .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 Option readOption(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.OPTION, reader);
    final String value = getStringAttribute(WadlAttributeName.VALUE, null, reader);
    final List<Documentation> doc = new LinkedList<Documentation>();
    while (reader.hasNext()) {
        reader.next();/* ww  w  .j  a v a2s  . c om*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.DOCUMENTATION, reader)) {
                doc.add(readDocumentation(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.OPTION, reader);
            return new Option(value, doc);
        }
        case XMLStreamConstants.CHARACTERS: {
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

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

private static final Grammars readGrammars(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.GRAMMARS, reader);
    final List<Documentation> documentation = new LinkedList<Documentation>();
    final List<Include> includes = new LinkedList<Include>();
    while (reader.hasNext()) {
        reader.next();/*from   w w w . java  2 s  . c om*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.INCLUDE, reader)) {
                includes.add(readInclude(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.GRAMMARS, reader);
            return new Grammars(documentation, includes);
        }
        case XMLStreamConstants.CHARACTERS: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}