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.flowable.cmmn.converter.ExtensionElementsXMLConverter.java

protected void readCompletionNeutralRule(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    if (conversionHelper.getCurrentCmmnElement() instanceof PlanItemControl) {
        CompletionNeutralRule completionNeutralRule = new CompletionNeutralRule();
        completionNeutralRule.setName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));

        PlanItemControl planItemControl = (PlanItemControl) conversionHelper.getCurrentCmmnElement();
        planItemControl.setCompletionNeutralRule(completionNeutralRule);

        readCommonXmlInfo(completionNeutralRule, xtr);

        boolean readyWithChildElements = false;
        try {//from w w  w  .ja  va 2s. com

            while (!readyWithChildElements && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement()) {
                    if (CmmnXmlConstants.ELEMENT_CONDITION.equals(xtr.getLocalName())) {
                        xtr.next();
                        if (xtr.isCharacters()) {
                            completionNeutralRule.setCondition(xtr.getText());
                        }
                        break;
                    }

                } else if (xtr.isEndElement()) {
                    if (CmmnXmlConstants.ELEMENT_COMPLETION_NEUTRAL_RULE.equalsIgnoreCase(xtr.getLocalName())) {
                        readyWithChildElements = true;
                    }
                }

            }
        } catch (Exception ex) {
            LOGGER.error("Error processing CMMN document", ex);
            throw new XMLException("Error processing CMMN document", ex);
        }
    }
}

From source file:org.flowable.cmmn.converter.ProcessRefExpressionXmlConverter.java

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    String expression = xtr.getText();
    if (StringUtils.isNotEmpty(expression) && conversionHelper.getCurrentCmmnElement() instanceof ProcessTask) {
        ProcessTask processTask = (ProcessTask) conversionHelper.getCurrentCmmnElement();
        processTask.setProcessRefExpression(expression);
    }//w  w  w  .  j a v a 2  s  .c o  m
    return null;
}

From source file:org.flowable.cmmn.converter.TimerExpressionXmlConverter.java

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    String expression = xtr.getText();
    if (StringUtils.isNotEmpty(expression)
            && conversionHelper.getCurrentCmmnElement() instanceof TimerEventListener) {
        TimerEventListener timerEventListener = (TimerEventListener) conversionHelper.getCurrentCmmnElement();
        timerEventListener.setTimerExpression(expression);
    }/*w  ww. j  a  v  a2  s.  c  o  m*/
    return null;
}

From source file:org.graphipedia.wikipedia.parser.SimpleStaxParser.java

/**
 * Parses the elements in the XML file.//from  www .ja v a  2s  .co  m
 * @param reader The XML stream.
 * @throws XMLStreamException when something goes wrong while parsing the XML file.
 */
private void parseElements(XMLStreamReader reader) throws XMLStreamException {
    LinkedList<String> elementStack = new LinkedList<String>();
    StringBuilder textBuffer = new StringBuilder();
    List<String> attributeValues = new ArrayList<String>();

    while (reader.hasNext()) {
        switch (reader.next()) {
        case XMLEvent.START_ELEMENT:
            String startElement = reader.getName().getLocalPart();
            elementStack.push(startElement);
            attributeValues = new ArrayList<String>();
            if (isInterestingWithAttributes(startElement)) {
                int noAttributes = reader.getAttributeCount();
                for (int i = 0; i < noAttributes; i += 1)
                    attributeValues.add(reader.getAttributeValue(i));
            }
            textBuffer.setLength(0);
            break;
        case XMLEvent.END_ELEMENT:
            String element = elementStack.pop();
            if (isInterestingWithAttributes(element)) {
                if (!handleElement(element, textBuffer.toString().trim(), attributeValues))
                    return;
            } else if (isInteresting(element)) {
                if (!handleElement(element, textBuffer.toString().trim()))
                    return;
            }
            break;
        case XMLEvent.CHARACTERS:
            if (isInteresting(elementStack.peek())) {
                textBuffer.append(reader.getText());
            }
            break;
        }
    }
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * {@inheritDoc}/*from  ww w  . j  ava  2 s.co  m*/
 */
@Override
public Config processConfig(XMLStreamReader aStreamReader) {
    JWebSocketConfig.Builder lConfigBuilder = new JWebSocketConfig.Builder();

    try {
        while (aStreamReader.hasNext()) {
            aStreamReader.next();
            if (aStreamReader.isStartElement()) {
                String lElementName = aStreamReader.getLocalName();
                if (lElementName.equals(ELEMENT_PROTOCOL)) {
                    aStreamReader.next();
                    lConfigBuilder.setProtocol(aStreamReader.getText());
                } else if (lElementName.equals(ELEMENT_NODE_ID)) {
                    aStreamReader.next();
                    lConfigBuilder.setNodeId(aStreamReader.getText());
                } else if (lElementName.equals(ELEMENT_LIBRARY_FOLDER)) {
                    aStreamReader.next();
                    lConfigBuilder.setLibraryFolder(aStreamReader.getText());
                } else if (lElementName.equals(ELEMENT_LIBRARIES)) {
                    List<LibraryConfig> lLibraries = handleLibraries(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setLibraries(lLibraries);
                } else if (lElementName.equals(ELEMENT_ENGINES)) {
                    List<EngineConfig> lEngines = handleEngines(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setEngines(lEngines);
                } else if (lElementName.equals(ELEMENT_SERVERS)) {
                    List<ServerConfig> lServers = handleServers(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setServers(lServers);
                } else if (lElementName.equals(ELEMENT_PLUGINS)) {
                    List<PluginConfig> lPlugins = handlePlugins(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setPlugins(lPlugins);
                } else if (lElementName.equals(ELEMENT_FILTERS)) {
                    List<FilterConfig> lFilters = handleFilters(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setFilters(lFilters);
                } else if (lElementName.equals(ELEMENT_LOGGING)) {
                    List<LoggingConfig> loggingConfigs = handleLoggingConfigs(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setLoggingConfig(loggingConfigs);
                } else if (lElementName.equals(ELEMENT_RIGHTS)) {
                    List<RightConfig> lGlobalRights = handleRights(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setGlobalRights(lGlobalRights);
                } else if (lElementName.equals(ELEMENT_ROLES)) {
                    List<RoleConfig> lRoles = handleRoles(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setGlobalRoles(lRoles);
                } else if (lElementName.equals(ELEMENT_USERS)) {
                    List<UserConfig> lUsers = handleUsers(aStreamReader);
                    lConfigBuilder = lConfigBuilder.setUsers(lUsers);
                } else {
                    // ignore
                }
            }
            if (aStreamReader.isEndElement()) {
                String lElementName = aStreamReader.getLocalName();
                if (lElementName.equals(JWEBSOCKET)) {
                    break;
                }
            }
        }
    } catch (XMLStreamException lEx) {
        throw new WebSocketRuntimeException("Error parsing jWebSocket.xml configuration file", lEx);
    }

    // if no filters where given in the .xml file
    // initialize empty filter list here
    if (lConfigBuilder.getFilters() == null) {
        lConfigBuilder.setFilters(new FastList<FilterConfig>());
    }

    // now return the config object, this is the only one config object that
    // should exists
    // in the system
    return lConfigBuilder.buildConfig();
}

From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java

/**
 * Read the map of plug-in specific settings
 * @param aStreamReader// ww  w.  j  a v a 2 s.  c  om
 *        the stream reader object
 * @return the list of domains for the engine
 * @throws XMLStreamException
 *         in case of stream exception
 */
public static Map<String, Object> getSettings(XMLStreamReader aStreamReader) throws XMLStreamException {

    Map<String, Object> lSettings = new FastMap<String, Object>();
    while (aStreamReader.hasNext()) {
        aStreamReader.next();
        if (aStreamReader.isStartElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(SETTING)) {
                String lKey = aStreamReader.getAttributeValue(null, "key");
                String lType = aStreamReader.getAttributeValue(null, "type");

                aStreamReader.next();
                String lValue = aStreamReader.getText();

                if (lKey != null && lValue != null) {
                    if ("json".equalsIgnoreCase(lType)) {
                        JSONObject lJSON = null;
                        try {
                            lJSON = new JSONObject(lValue);
                        } catch (JSONException lEx) {
                            // TODO: handle invalid JSON code in settings properly!
                        }
                        lSettings.put(lKey, lJSON);
                    } else {
                        lSettings.put(lKey, lValue);
                    }
                }
            }
        }

        if (aStreamReader.isEndElement()) {
            String lElementName = aStreamReader.getLocalName();
            if (lElementName.equals(SETTINGS)) {
                break;
            }
        }
    }

    return lSettings;
}

From source file:org.mule.module.xml.util.XMLUtils.java

public static void copy(XMLStreamReader reader, XMLStreamWriter writer, boolean fragment)
        throws XMLStreamException {
    // number of elements read in
    int read = 0;
    int event = reader.getEventType();

    while (reader.hasNext()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            read++;//ww w.j a  va 2  s. c o  m
            writeStartElement(reader, writer);
            break;
        case XMLStreamConstants.END_ELEMENT:
            writer.writeEndElement();
            read--;
            if (read <= 0 && !fragment) {
                return;
            }
            break;
        case XMLStreamConstants.CHARACTERS:
            writer.writeCharacters(reader.getText());
            break;
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT:
        case XMLStreamConstants.ATTRIBUTE:
        case XMLStreamConstants.NAMESPACE:
            break;
        default:
            break;
        }
        event = reader.next();
    }
}

From source file:org.mule.modules.jive.api.xml.XmlMapper.java

/**Maps an xml from a {@link Reader} to a {@link Map}.
 * @param reader The {@link Reader} with the xml data
 * @return The map with the entity data//  w  ww .ja v  a2s. co  m
 * */
@SuppressWarnings("unchecked")
public final Map<String, Object> xml2map(final Reader reader) {
    final Map<String, Object> ret = new HashMap<String, Object>();
    final Stack<Map<String, Object>> maps = new Stack<Map<String, Object>>();
    Map<String, Object> current = ret;

    try {
        final XMLStreamReader r = xmlInputFactory.createXMLStreamReader(reader);
        StringBuilder lastText = new StringBuilder();
        String currentElement = null;
        int returnCount = 0;
        while (r.hasNext()) {
            final int eventType = r.next();
            if (eventType == CHARACTERS || eventType == CDATA || eventType == SPACE
                    || eventType == ENTITY_REFERENCE) {
                lastText.append(r.getText());
            } else if (eventType == END_DOCUMENT) {
                break;
            } else if (eventType == START_ELEMENT) {
                if (currentElement != null) {
                    maps.push(current);
                    final Map<String, Object> map = new HashMap<String, Object>();
                    if (StringUtils.startsWith(currentElement, "return")) {
                        currentElement = currentElement + "--" + String.valueOf(returnCount);
                        returnCount++;
                    }
                    current.put(currentElement, map);
                    current = map;
                }
                currentElement = r.getLocalName();
            } else if (eventType == END_ELEMENT) {
                if (currentElement == null) {
                    current = maps.pop();
                } else {
                    current.put(currentElement, lastText.toString().trim());
                    currentElement = null;
                    lastText = new StringBuilder();
                }
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType);
            }
        }

        final Object obj = ret.get(ret.keySet().iterator().next());
        if (obj instanceof String) {
            Map<String, Object> responseTag = new HashMap<String, Object>();
            responseTag.put("response", ret.keySet().iterator().next().toString());
            return responseTag;
        } else {
            final Map<String, Object> returnXMLElement = (Map<String, Object>) ret
                    .get(ret.keySet().iterator().next());

            if (returnXMLElement.keySet().contains("return--1")) {
                return returnXMLElement;
            }
            return (Map<String, Object>) returnXMLElement.get("return--0");
        }

    } catch (XMLStreamException e) {
        throw new UnhandledException(e);
    }
}

From source file:org.netbeans.jbatch.modeler.spec.core.Definitions.java

static void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) {
    try {/*from   w ww  . j  ava 2 s.  com*/
        //            TransformerFactory tf = TransformerFactory.newInstance();
        //            Transformer t = tf.newTransformer();
        //            StAXSource source = new StAXSource(xmlStreamReader);
        //            StAXResult result = new StAXResult(xmlStreamWriter);
        //            t.transform(source, result);
        System.out.println("Defnition Id : " + xmlStreamReader.getAttributeValue(null, "id"));
        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.START_ELEMENT:
                String prefix = xmlStreamReader.getPrefix();
                String namespaceURI = xmlStreamReader.getNamespaceURI();
                if (namespaceURI != null) {
                    if (prefix != null) {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(),
                                xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI());
                    } else {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(),
                                xmlStreamReader.getLocalName());
                    }
                } else {
                    xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                }

                for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) {
                    xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i),
                            xmlStreamReader.getNamespaceURI(i));
                }
                int count = xmlStreamReader.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    //                            xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i),
                    //                                    xmlStreamReader.getAttributeNamespace(i),
                    //                                    xmlStreamReader.getAttributeLocalName(i),
                    //                                    xmlStreamReader.getAttributeValue(i));

                    String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i),
                            attrPrefix = xmlStreamReader.getAttributePrefix(i);
                    if (attrNamespaceURI != null) {
                        if (attrPrefix != null) {
                            xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        } else {
                            xmlStreamWriter.writeAttribute(attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        }
                    } else {
                        xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i),
                                xmlStreamReader.getAttributeValue(i));
                    }

                }
                break;
            case XMLEvent.END_ELEMENT:
                xmlStreamWriter.writeEndElement();
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            case XMLEvent.SPACE:
            case XMLEvent.CHARACTERS:
                xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(),
                        xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength());
                break;
            case XMLEvent.PROCESSING_INSTRUCTION:
                xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(),
                        xmlStreamReader.getPIData());
                break;
            case XMLEvent.CDATA:
                xmlStreamWriter.writeCData(xmlStreamReader.getText());
                break;

            case XMLEvent.COMMENT:
                xmlStreamWriter.writeComment(xmlStreamReader.getText());
                break;
            case XMLEvent.ENTITY_REFERENCE:
                xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName());
                break;
            case XMLEvent.START_DOCUMENT:
                String encoding = xmlStreamReader.getCharacterEncodingScheme();
                String version = xmlStreamReader.getVersion();

                if (encoding != null && version != null) {
                    xmlStreamWriter.writeStartDocument(encoding, version);
                } else if (version != null) {
                    xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion());
                }
                break;
            case XMLEvent.END_DOCUMENT:
                xmlStreamWriter.writeEndDocument();
                break;
            case XMLEvent.DTD:
                xmlStreamWriter.writeDTD(xmlStreamReader.getText());
                break;

            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.netbeans.jbatch.modeler.specification.model.job.util.JobUtil.java

void transformXMLStream(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter) {
    try {//from  w  w  w  . jav a2 s . c o  m
        //            TransformerFactory tf = TransformerFactory.newInstance();
        //            Transformer t = tf.newTransformer();
        //            StAXSource source = new StAXSource(xmlStreamReader);
        //            StAXResult result = new StAXResult(xmlStreamWriter);
        //            t.transform(source, result);

        boolean finish = false;
        while (xmlStreamReader.hasNext() && !finish) {
            switch (xmlStreamReader.getEventType()) {
            case XMLEvent.START_ELEMENT:
                String prefix = xmlStreamReader.getPrefix();
                String namespaceURI = xmlStreamReader.getNamespaceURI();
                if (namespaceURI != null) {
                    if (prefix != null) {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getPrefix(),
                                xmlStreamReader.getLocalName(), xmlStreamReader.getNamespaceURI());
                    } else {
                        xmlStreamWriter.writeStartElement(xmlStreamReader.getNamespaceURI(),
                                xmlStreamReader.getLocalName());
                    }
                } else {
                    xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                }

                for (int i = 0; i < xmlStreamReader.getNamespaceCount(); i++) {
                    xmlStreamWriter.writeNamespace(xmlStreamReader.getNamespacePrefix(i),
                            xmlStreamReader.getNamespaceURI(i));
                }
                int count = xmlStreamReader.getAttributeCount();
                for (int i = 0; i < count; i++) {
                    //                            xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributePrefix(i),
                    //                                    xmlStreamReader.getAttributeNamespace(i),
                    //                                    xmlStreamReader.getAttributeLocalName(i),
                    //                                    xmlStreamReader.getAttributeValue(i));

                    String attrNamespaceURI = xmlStreamReader.getAttributeNamespace(i),
                            attrPrefix = xmlStreamReader.getAttributePrefix(i);
                    if (attrNamespaceURI != null) {
                        if (attrPrefix != null) {
                            xmlStreamWriter.writeAttribute(attrPrefix, attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        } else {
                            xmlStreamWriter.writeAttribute(attrNamespaceURI,
                                    xmlStreamReader.getAttributeLocalName(i),
                                    xmlStreamReader.getAttributeValue(i));
                        }
                    } else {
                        xmlStreamWriter.writeAttribute(xmlStreamReader.getAttributeLocalName(i),
                                xmlStreamReader.getAttributeValue(i));
                    }

                }
                break;
            case XMLEvent.END_ELEMENT:
                xmlStreamWriter.writeEndElement();
                if (xmlStreamReader.getLocalName().equals("definitions")) {
                    finish = true;
                }
                break;
            case XMLEvent.SPACE:
            case XMLEvent.CHARACTERS:
                xmlStreamWriter.writeCharacters(xmlStreamReader.getTextCharacters(),
                        xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength());
                break;
            case XMLEvent.PROCESSING_INSTRUCTION:
                xmlStreamWriter.writeProcessingInstruction(xmlStreamReader.getPITarget(),
                        xmlStreamReader.getPIData());
                break;
            case XMLEvent.CDATA:
                xmlStreamWriter.writeCData(xmlStreamReader.getText());
                break;

            case XMLEvent.COMMENT:
                xmlStreamWriter.writeComment(xmlStreamReader.getText());
                break;
            case XMLEvent.ENTITY_REFERENCE:
                xmlStreamWriter.writeEntityRef(xmlStreamReader.getLocalName());
                break;
            case XMLEvent.START_DOCUMENT:
                String encoding = xmlStreamReader.getCharacterEncodingScheme();
                String version = xmlStreamReader.getVersion();

                if (encoding != null && version != null) {
                    xmlStreamWriter.writeStartDocument(encoding, version);
                } else if (version != null) {
                    xmlStreamWriter.writeStartDocument(xmlStreamReader.getVersion());
                }
                break;
            case XMLEvent.END_DOCUMENT:
                xmlStreamWriter.writeEndDocument();
                break;
            case XMLEvent.DTD:
                xmlStreamWriter.writeDTD(xmlStreamReader.getText());
                break;

            }
            if (!finish) {
                xmlStreamReader.next();
            }
        }
    } catch (XMLStreamException ex) {
        Exceptions.printStackTrace(ex);
    }
}