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.apache.sysml.runtime.controlprogram.parfor.opt.PerfTestTool.java

private static void readProfile(String fname) throws XMLStreamException, IOException {
    //init profile map
    _profile = new HashMap<Integer, HashMap<Integer, CostFunction>>();

    //read existing profile
    FileInputStream fis = new FileInputStream(fname);

    try {//  w ww .  java  2 s  . c  om
        //xml parsing
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(fis);

        int e = xsr.nextTag(); // profile start

        while (true) //read all instructions
        {
            e = xsr.nextTag(); // instruction start
            if (e == XMLStreamConstants.END_ELEMENT)
                break; //reached profile end tag

            //parse instruction
            int ID = Integer.parseInt(xsr.getAttributeValue(null, XML_ID));
            //String name = xsr.getAttributeValue(null, XML_NAME).trim().replaceAll(" ", Lops.OPERAND_DELIMITOR);
            HashMap<Integer, CostFunction> tmp = new HashMap<Integer, CostFunction>();
            _profile.put(ID, tmp);

            while (true) {
                e = xsr.nextTag(); // cost function start
                if (e == XMLStreamConstants.END_ELEMENT)
                    break; //reached instruction end tag

                //parse cost function
                TestMeasure m = TestMeasure.valueOf(xsr.getAttributeValue(null, XML_MEASURE));
                TestVariable lv = TestVariable.valueOf(xsr.getAttributeValue(null, XML_VARIABLE));
                InternalTestVariable[] pv = parseTestVariables(
                        xsr.getAttributeValue(null, XML_INTERNAL_VARIABLES));
                DataFormat df = DataFormat.valueOf(xsr.getAttributeValue(null, XML_DATAFORMAT));
                int tDefID = getTestDefID(m, lv, df, pv);

                xsr.next(); //read characters
                double[] params = parseParams(xsr.getText());
                boolean multidim = _regTestDef.get(tDefID).getInternalVariables().length > 1;
                CostFunction cf = new CostFunction(params, multidim);
                tmp.put(tDefID, cf);

                xsr.nextTag(); // cost function end
                //System.out.println("added cost function");
            }
        }
        xsr.close();
    } finally {
        IOUtilFunctions.closeSilently(fis);
    }

    //mark profile as successfully read
    _flagReadData = true;
}

From source file:org.atomserver.core.validators.SimpleXMLContentValidator.java

private void touchEvent(XMLStreamReader reader) throws XMLStreamException {
    int eventCode = reader.next();
    String val = null;
    switch (eventCode) {
    case XMLStreamReader.START_ELEMENT:
        val = reader.getLocalName();
        if (log.isTraceEnabled()) {
            log.trace("event = START_ELEMENT");
            log.trace("Localname = " + val);
        }/*w ww .ja  va2 s. c o m*/
        break;
    case XMLStreamReader.END_ELEMENT:
        val = reader.getLocalName();
        if (log.isTraceEnabled()) {
            log.trace("event = END_ELEMENT");
            log.trace("Localname = " + val);
        }
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        val = reader.getPIData();
        if (log.isTraceEnabled()) {
            log.trace("event = PROCESSING_INSTRUCTION");
            log.trace("PIData = " + val);
        }
        break;
    case XMLStreamReader.CHARACTERS:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = CHARACTERS");
            log.trace("Characters = " + val);
        }
        break;
    case XMLStreamReader.COMMENT:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = COMMENT");
            log.trace("Comment = " + val);
        }
        break;
    case XMLStreamReader.SPACE:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = SPACE");
            log.trace("Space = " + val);
        }
        break;
    case XMLStreamReader.START_DOCUMENT:
        if (log.isTraceEnabled()) {
            log.trace("event = START_DOCUMENT");
            log.trace("Document Started.");
        }
        break;
    case XMLStreamReader.END_DOCUMENT:
        if (log.isTraceEnabled()) {
            log.trace("event = END_DOCUMENT");
            log.trace("Document Ended");
        }
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = ENTITY_REFERENCE");
            log.trace("Text = " + val);
        }
        break;
    case XMLStreamReader.DTD:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = DTD");
            log.trace("DTD = " + val);
        }
        break;
    case XMLStreamReader.CDATA:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = CDATA");
            log.trace("CDATA = " + val);
        }
        break;
    }
}

From source file:org.auraframework.impl.factory.SVGParser.java

@Override
public SVGDef getDefinition(DefDescriptor<SVGDef> descriptor, TextSource<SVGDef> source)
        throws SVGParserException, QuickFixException {
    if (descriptor.getDefType() == DefType.SVG) {
        XMLStreamReader reader = null;
        String contents = source.getContents();
        //If the file is too big throw before we parse the whole thing.
        SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition();
        try (StringReader stringReader = new StringReader(contents)) {
            reader = xmlInputFactory.createXMLStreamReader(stringReader);
            if (reader != null) {
                LOOP: while (reader.hasNext()) {
                    int type = reader.next();
                    switch (type) {
                    case XMLStreamConstants.END_DOCUMENT:
                        break LOOP;
                    //This is plain text inside the file
                    case XMLStreamConstants.CHARACTERS:
                        if (DISSALOWED_LIST.matcher(reader.getText()).matches()) {
                            throw new InvalidDefinitionException(
                                    String.format("Text contains disallowed symbols: %s", reader.getText()),
                                    XMLParser.getLocation(reader, source));
                        }/*  w  ww. j  a v  a  2  s .c o  m*/
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        String name = reader.getName().toString().toLowerCase();
                        if (!SVG_TAG_WHITELIST.contains(name)) {
                            throw new InvalidDefinitionException(
                                    String.format("Invalid SVG tag specified: %s", name),
                                    XMLParser.getLocation(reader, source));
                        }
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            QName qAttr = reader.getAttributeName(i);
                            String attr = qAttr.getLocalPart();
                            if (SVG_ATTR_BLACKLIST.contains(attr)) {
                                throw new InvalidDefinitionException(
                                        String.format("Invalid SVG attribute specified: %s", attr),
                                        XMLParser.getLocation(reader, source));
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                    case XMLStreamConstants.COMMENT:
                    case XMLStreamConstants.DTD:
                    case XMLStreamConstants.SPACE:
                        continue;
                    default:
                        throw new InvalidDefinitionException(String.format("Found unexpected element in xml."),
                                XMLParser.getLocation(reader, source));
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage()));
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (XMLStreamException e) {
                    //Well I tried to play nicely
                }
            }
        }
        return ret;
    }
    return null;
}

From source file:org.auraframework.impl.svg.parser.SVGParser.java

@Override
public SVGDef parse(DefDescriptor<SVGDef> descriptor, Source<SVGDef> source)
        throws SVGParserException, QuickFixException {
    if (descriptor.getDefType() == DefType.SVG) {
        XMLStreamReader reader = null;
        String contents = source.getContents();
        //If the file is too big throw before we parse the whole thing.
        SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition();
        try (StringReader stringReader = new StringReader(contents)) {
            reader = xmlInputFactory.createXMLStreamReader(stringReader);
            if (reader != null) {
                LOOP: while (reader.hasNext()) {
                    int type = reader.next();
                    switch (type) {
                    case XMLStreamConstants.END_DOCUMENT:
                        break LOOP;
                    //This is plain text inside the file
                    case XMLStreamConstants.CHARACTERS:
                        if (DISSALOWED_LIST.matcher(reader.getText()).matches()) {
                            throw new InvalidDefinitionException(
                                    String.format("Text contains disallowed symbols: %s", reader.getText()),
                                    XMLParser.getLocation(reader, source));
                        }//from  ww w.j  a va 2 s.  c  o m
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        String name = reader.getName().toString().toLowerCase();
                        if (!SVG_TAG_WHITELIST.contains(name)) {
                            throw new InvalidDefinitionException(
                                    String.format("Invalid SVG tag specified: %s", name),
                                    XMLParser.getLocation(reader, source));
                        }
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            QName qAttr = reader.getAttributeName(i);
                            String attr = qAttr.getLocalPart();
                            if (SVG_ATTR_BLACKLIST.contains(attr)) {
                                throw new InvalidDefinitionException(
                                        String.format("Invalid SVG attribute specified: %s", attr),
                                        XMLParser.getLocation(reader, source));
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                    case XMLStreamConstants.COMMENT:
                    case XMLStreamConstants.DTD:
                    case XMLStreamConstants.SPACE:
                        continue;
                    default:
                        throw new InvalidDefinitionException(String.format("Found unexpected element in xml."),
                                XMLParser.getLocation(reader, source));
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage()));
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (XMLStreamException e) {
                    //Well I tried to play nicely
                }
            }
        }
        return ret;
    }
    return null;
}

From source file:org.deegree.client.core.renderer.OutputXMLRenderer.java

private void encodeXML(ResponseWriter writer, String value) throws IOException {
    int depth = 0;
    try {//  ww w  .  j a v a2 s  .  co m
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(value));
        boolean lastWasEndElement = false;
        boolean lastWasComment = false;
        while (reader.hasNext()) {
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                if (!lastWasComment) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                }
                writer.write(getSpaces(depth));
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                String prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String attributePrefix = reader.getAttributePrefix(i);
                    writer.write(
                            (attributePrefix != null && attributePrefix.length() > 0 ? attributePrefix + ":"
                                    : "") + reader.getAttributeLocalName(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(reader.getAttributeValue(i)));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                for (int i = 0; i < reader.getNamespaceCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String nsPrefix = reader.getNamespacePrefix(i);
                    writer.write("xmlns");
                    if (nsPrefix != null && !nsPrefix.isEmpty()) {
                        writer.write(":" + nsPrefix);
                    }
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(reader.getNamespaceURI(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                depth++;
                lastWasEndElement = false;
                lastWasComment = false;
                break;
            case XMLStreamConstants.CHARACTERS:
                String text = reader.getText();
                if (text.trim().length() > 0) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(text));
                    writer.endElement("span");
                    lastWasEndElement = false;
                    lastWasComment = false;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                depth--;
                if (lastWasEndElement) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                    writer.write(getSpaces(depth));
                }
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;/");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                lastWasEndElement = true;
                lastWasComment = false;
                break;
            case XMLStreamConstants.COMMENT:
                writer.startElement("div", null);
                writer.writeAttribute("class", "comment", null);
                writer.write("&lt;/!--" + reader.getText() + "--&gt;");
                writer.endElement("div");
                lastWasEndElement = false;
                lastWasComment = true;
                break;
            default:
                break;
            }
            reader.next();

        }
        reader.close();
    } catch (Throwable e) {
        if (depth == 0) {
            writer.writeText("Response could not parsed as XML.", null);
        } else {
            writer.writeText("... (if you want the complete document, please click the download button)", null);
        }
    }
}

From source file:org.deegree.services.csw.exporthandling.GetCapabilitiesHelper.java

private void writeTemplateElement(XMLStreamWriter writer, XMLStreamReader inStream,
        Map<String, String> varToValue) throws XMLStreamException {

    if (inStream.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("Input stream does not point to a START_ELEMENT event.");
    }/*from  w ww  .  j a  v  a2s  .com*/
    int openElements = 0;
    boolean firstRun = true;
    while (firstRun || openElements > 0) {
        firstRun = false;
        int eventType = inStream.getEventType();

        switch (eventType) {
        case CDATA: {
            writer.writeCData(inStream.getText());
            break;
        }
        case CHARACTERS: {
            String s = new String(inStream.getTextCharacters(), inStream.getTextStart(),
                    inStream.getTextLength());
            // TODO optimize
            for (String param : varToValue.keySet()) {
                String value = varToValue.get(param);
                s = s.replace(param, value);
            }
            writer.writeCharacters(s);

            break;
        }
        case END_ELEMENT: {
            writer.writeEndElement();
            openElements--;
            break;
        }
        case START_ELEMENT: {
            if (inStream.getNamespaceURI() == "" || inStream.getPrefix() == DEFAULT_NS_PREFIX
                    || inStream.getPrefix() == null) {
                writer.writeStartElement(inStream.getLocalName());
            } else {
                if (writer.getNamespaceContext().getPrefix(inStream.getPrefix()) == "") {
                    // TODO handle special cases for prefix binding, see
                    // http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/xml/namespace/NamespaceContext.html#getNamespaceURI(java.lang.String)
                    writer.setPrefix(inStream.getPrefix(), inStream.getNamespaceURI());
                }
                writer.writeStartElement(inStream.getPrefix(), inStream.getLocalName(),
                        inStream.getNamespaceURI());
            }
            // copy all namespace bindings
            for (int i = 0; i < inStream.getNamespaceCount(); i++) {
                String nsPrefix = inStream.getNamespacePrefix(i);
                String nsURI = inStream.getNamespaceURI(i);
                writer.writeNamespace(nsPrefix, nsURI);
            }

            // copy all attributes
            for (int i = 0; i < inStream.getAttributeCount(); i++) {
                String localName = inStream.getAttributeLocalName(i);
                String nsPrefix = inStream.getAttributePrefix(i);
                String value = inStream.getAttributeValue(i);
                String nsURI = inStream.getAttributeNamespace(i);
                if (nsURI == null) {
                    writer.writeAttribute(localName, value);
                } else {
                    writer.writeAttribute(nsPrefix, nsURI, localName, value);
                }
            }

            openElements++;
            break;
        }
        default: {
            break;
        }
        }
        if (openElements > 0) {
            inStream.next();
        }
    }
}

From source file:org.erdc.cobie.shared.COBieUtility.java

static public Map<String, String> elementMapFromXMLObject(XmlObject xml) {
    Map<String, String> elementMap = new HashMap<String, String>();
    XMLStreamReader rdr = xml.newXMLStreamReader();
    String keyName = "";
    String keyVal = "";
    boolean lastWasStartElement = false;
    int attCount = 0;
    try {//from   w  ww.  j av  a2s  . co  m

        while (rdr.hasNext()) {
            try {
                attCount = rdr.getAttributeCount();
            } catch (Exception ex) {
                attCount = 0;
            }
            if (attCount > 0) {
                for (int i = 0; i < attCount; i++) {
                    keyName = rdr.getAttributeLocalName(i);
                    keyVal = rdr.getAttributeValue(i);
                    if (!elementMap.containsKey(keyName)) {
                        elementMap.put(keyName, escape(keyVal));
                    }
                    lastWasStartElement = false;
                }
            }
            if (rdr.isStartElement()) {
                keyName = rdr.getLocalName();
                lastWasStartElement = true;

            } else if (rdr.isCharacters() && lastWasStartElement) {
                if (isElementADateTime(xml, keyName)) {
                    keyVal = COBieUtility.stringFromXmlDateTime(xml, keyName);

                } else {
                    keyVal = rdr.getText();
                }
                if (!elementMap.containsKey(keyName)) {
                    elementMap.put(keyName, escape(keyVal));
                }
                lastWasStartElement = false;
            } else {
                lastWasStartElement = false;
            }

            rdr.next();
        }
    } catch (XMLStreamException e) {
        LOGGER.error("", e);
    }
    return elementMap;
}

From source file:org.flowable.bpmn.converter.BaseBpmnXMLConverter.java

@SuppressWarnings("unchecked")
protected ExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
    ExtensionElement extensionElement = new ExtensionElement();
    extensionElement.setName(xtr.getLocalName());
    if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
        extensionElement.setNamespace(xtr.getNamespaceURI());
    }//from  w ww.  ja  va 2 s  .  c o m
    if (StringUtils.isNotEmpty(xtr.getPrefix())) {
        extensionElement.setNamespacePrefix(xtr.getPrefix());
    }

    BpmnXMLUtil.addCustomAttributes(xtr, extensionElement, defaultElementAttributes);

    boolean readyWithExtensionElement = false;
    while (readyWithExtensionElement == false && xtr.hasNext()) {
        xtr.next();
        if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) {
            if (StringUtils.isNotEmpty(xtr.getText().trim())) {
                extensionElement.setElementText(xtr.getText().trim());
            }
        } else if (xtr.isStartElement()) {
            ExtensionElement childExtensionElement = parseExtensionElement(xtr);
            extensionElement.addChildElement(childExtensionElement);
        } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
            readyWithExtensionElement = true;
        }
    }
    return extensionElement;
}

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

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    String condition = xtr.getText();
    if (StringUtils.isNotEmpty(condition)) {
        CmmnElement currentCmmnElement = conversionHelper.getCurrentCmmnElement();
        if (currentCmmnElement instanceof SentryIfPart) {
            ((SentryIfPart) currentCmmnElement).setCondition(condition);
        } else if (currentCmmnElement instanceof RepetitionRule) {
            ((RepetitionRule) currentCmmnElement).setCondition(condition);
        }//from   w  ww . ja v  a  2 s.  c om
    }
    return null;
}

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

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    String expression = xtr.getText();
    if (StringUtils.isNotEmpty(expression)
            && conversionHelper.getCurrentCmmnElement() instanceof DecisionTask) {
        DecisionTask decisionTask = (DecisionTask) conversionHelper.getCurrentCmmnElement();
        decisionTask.setDecisionRefExpression(expression);
    }//  w  w w .j  a v  a2s.co m
    return null;
}