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.apache.synapse.commons.json.JsonDataSource.java

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();//from   www . java  2s  .  c  o  m
    while (reader.hasNext()) {
        int x = reader.next();
        switch (x) {
        case XMLStreamConstants.START_ELEMENT:
            xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI());
            int namespaceCount = reader.getNamespaceCount();
            for (int i = namespaceCount - 1; i >= 0; i--) {
                xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
            }
            int attributeCount = reader.getAttributeCount();
            for (int i = 0; i < attributeCount; i++) {
                xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                        reader.getAttributeLocalName(i), reader.getAttributeValue(i));
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        case XMLStreamConstants.CHARACTERS:
            xmlWriter.writeCharacters(reader.getText());
            break;
        case XMLStreamConstants.CDATA:
            xmlWriter.writeCData(reader.getText());
            break;
        case XMLStreamConstants.END_ELEMENT:
            xmlWriter.writeEndElement();
            break;
        case XMLStreamConstants.END_DOCUMENT:
            xmlWriter.writeEndDocument();
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            xmlWriter.writeComment(reader.getText());
            break;
        case XMLStreamConstants.DTD:
            xmlWriter.writeDTD(reader.getText());
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData());
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            xmlWriter.writeEntityRef(reader.getLocalName());
            break;
        default:
            throw new OMException();
        }
    }
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
}

From source file:org.apache.xml.security.stax.impl.transformer.TransformBase64Decode.java

@Override
public void transform(XMLSecEvent xmlSecEvent) throws XMLStreamException {
    int eventType = xmlSecEvent.getEventType();
    switch (eventType) {
    case XMLStreamConstants.CHARACTERS:
        if (getOutputStream() != null) {
            //we have an output stream
            //encoding shouldn't matter here, because the data is Base64 encoded and is therefore in the ASCII range.
            try {
                getOutputStream().write(xmlSecEvent.asCharacters().getData().getBytes());
            } catch (IOException e) {
                throw new XMLStreamException(e);
            }//from w w w  . ja  v a  2s . co m
        } else {
            //we have a child transformer
            if (childOutputMethod == null) {

                final XMLSecurityConstants.TransformMethod preferredChildTransformMethod = getTransformer()
                        .getPreferredTransformMethod(XMLSecurityConstants.TransformMethod.XMLSecEvent);

                switch (preferredChildTransformMethod) {
                case XMLSecEvent: {
                    childOutputMethod = new ChildOutputMethod() {

                        private UnsynchronizedByteArrayOutputStream byteArrayOutputStream;
                        private Base64OutputStream base64OutputStream;

                        @Override
                        public void transform(Object object) throws XMLStreamException {
                            if (base64OutputStream == null) {
                                byteArrayOutputStream = new UnsynchronizedByteArrayOutputStream();
                                base64OutputStream = new Base64OutputStream(byteArrayOutputStream, false);
                            }
                            try {
                                base64OutputStream.write(((byte[]) object));
                            } catch (IOException e) {
                                throw new XMLStreamException(e);
                            }
                        }

                        @Override
                        public void doFinal() throws XMLStreamException {
                            try {
                                base64OutputStream.close();
                            } catch (IOException e) {
                                throw new XMLStreamException(e);
                            }
                            XMLEventReaderInputProcessor xmlEventReaderInputProcessor = new XMLEventReaderInputProcessor(
                                    null,
                                    getXmlInputFactory()
                                            .createXMLStreamReader(new UnsynchronizedByteArrayInputStream(
                                                    byteArrayOutputStream.toByteArray())));

                            try {
                                XMLSecEvent xmlSecEvent;
                                do {
                                    xmlSecEvent = xmlEventReaderInputProcessor.processNextEvent(null);
                                    getTransformer().transform(xmlSecEvent);
                                } while (xmlSecEvent.getEventType() != XMLStreamConstants.END_DOCUMENT);
                            } catch (XMLSecurityException e) {
                                throw new XMLStreamException(e);
                            }
                            getTransformer().doFinal();
                        }
                    };
                    break;
                }
                case InputStream: {
                    childOutputMethod = new ChildOutputMethod() {

                        private UnsynchronizedByteArrayOutputStream byteArrayOutputStream;
                        private Base64OutputStream base64OutputStream;

                        @Override
                        public void transform(Object object) throws XMLStreamException {
                            if (base64OutputStream == null) {
                                byteArrayOutputStream = new UnsynchronizedByteArrayOutputStream();
                                base64OutputStream = new Base64OutputStream(byteArrayOutputStream, false);
                            }
                            try {
                                base64OutputStream.write(((byte[]) object));
                            } catch (IOException e) {
                                throw new XMLStreamException(e);
                            }
                        }

                        @Override
                        public void doFinal() throws XMLStreamException {
                            try {
                                base64OutputStream.close();
                            } catch (IOException e) {
                                throw new XMLStreamException(e);
                            }
                            getTransformer().transform(new UnsynchronizedByteArrayInputStream(
                                    byteArrayOutputStream.toByteArray()));
                            getTransformer().doFinal();
                        }
                    };
                    break;
                }
                }
            }
            childOutputMethod.transform(xmlSecEvent.asCharacters().getData().getBytes());
        }
        break;
    }
}

From source file:org.apereo.portal.xml.stream.IndentingXMLEventWriter.java

@Override
public void add(XMLEvent event) throws XMLStreamException {
    switch (event.getEventType()) {
    case XMLStreamConstants.CHARACTERS:
    case XMLStreamConstants.CDATA:
    case XMLStreamConstants.SPACE: {
        wrappedWriter.add(event);//from  w  w  w.  j  a v a 2s. co m
        afterData();
        return;
    }
    case XMLStreamConstants.START_ELEMENT: {
        beforeStartElement();
        wrappedWriter.add(event);
        afterStartElement();
        return;
    }

    case XMLStreamConstants.END_ELEMENT: {
        beforeEndElement();
        wrappedWriter.add(event);
        afterEndElement();
        return;
    }
    case XMLStreamConstants.START_DOCUMENT:
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
    case XMLStreamConstants.COMMENT:
    case XMLStreamConstants.DTD: {
        beforeMarkup();
        wrappedWriter.add(event);
        afterMarkup();
        return;
    }
    case XMLStreamConstants.END_DOCUMENT: {
        wrappedWriter.add(event);
        afterEndDocument();
        break;
    }
    default: {
        wrappedWriter.add(event);
        return;
    }
    }
}

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));
                        }// 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.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  www . ja  va2s  . 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 {//  w  w w.  ja  v a  2 s  .  c  o  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.fcrepo.migration.foxml.FoxmlInputStreamFedoraObjectProcessor.java

/**
 * foxml input stream fedora object processor.
 * @param is the input stream/*  w ww .  j a v a  2  s.  co m*/
 * @param fetcher the fetcher
 * @param resolver the resolver
 * @param localFedoraServer the host and port (formatted like "localhost:8080") of the fedora 3 server
 *                          from which the content exposed by the "is" parameter comes.
 * @throws XMLStreamException xml stream exception
 */
public FoxmlInputStreamFedoraObjectProcessor(final InputStream is, final URLFetcher fetcher,
        final InternalIDResolver resolver, final String localFedoraServer) throws XMLStreamException {
    this.fetcher = fetcher;
    this.idResolver = resolver;
    this.localFedoraServer = localFedoraServer;
    final XMLInputFactory factory = XMLInputFactory.newFactory();
    reader = factory.createXMLStreamReader(is);
    reader.nextTag();
    final Map<String, String> attributes = getAttributes(reader, "PID", "VERSION", "FEDORA_URI",
            "schemaLocation");
    if (attributes.get("VERSION") == null || !attributes.get("VERSION").equals("1.1")) {
        isFedora2 = true;
    }
    objectInfo = new DefaultObjectInfo(attributes.get("PID"), attributes.get("FEDORA_URI"));
    while (reader.next() == XMLStreamConstants.CHARACTERS) {
    }

    tempFiles = new ArrayList<File>();
}

From source file:org.intermine.webservice.client.results.XMLTableResult.java

private List<String> getNextRow() {
    List<String> row = new ArrayList<String>();
    boolean hasGotWholeRow = false;
    boolean hasGotWholeResultSet = false;
    String currentElemName = null;
    String errorMessage = null;/*from   www  . j av  a2 s.c om*/
    String errorCause = null;
    String currentValue = null;
    try {
        while (xmlReader.hasNext()) {
            int eventType = xmlReader.getEventType();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                currentElemName = xmlReader.getLocalName();
                break;

            case XMLStreamConstants.END_ELEMENT:
                String elemName = xmlReader.getLocalName();
                if ("Result".equals(elemName)) {
                    hasGotWholeRow = true;
                } else if ("ResultSet".equals(elemName)) {
                    hasGotWholeResultSet = true;
                } else if ("i".equals(elemName)) {
                    row.add(currentValue);
                    currentValue = null;
                }
                break;

            case XMLStreamConstants.CHARACTERS:
                if ("i".equals(currentElemName)) {
                    String valueChunk = StringEscapeUtils.unescapeXml(xmlReader.getText());
                    if (currentValue == null) {
                        currentValue = valueChunk;
                    } else {
                        currentValue += valueChunk;
                    }
                } else if ("message".equals(currentElemName)) {
                    errorMessage = xmlReader.getText();
                } else if ("cause".equals(currentElemName)) {
                    errorCause = xmlReader.getText();
                } else {
                    String data = xmlReader.getText();
                    if (!(data == null || "".equals(data.trim()))) {
                        throw new ServiceException("Character data found in illegal place: " + currentElemName
                                + ", '" + data + "'");
                    }
                }
                break;
            default:
                break;
            }
            xmlReader.next();
            if (hasGotWholeRow) {
                break;
            }
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error parsing XML result response", e);
    }
    // Check to make sure we can return data
    if (errorMessage != null || errorCause != null) {
        throw new ResultException(errorMessage, errorCause);
    }
    if (!hasGotWholeRow && !hasGotWholeResultSet) {
        throw new TransferInterruptedException();
    }
    if (row.isEmpty()) {
        return null;
    } else {
        return row;
    }
}

From source file:org.mcisb.subliminal.SubliminalUtils.java

/**
 * //from   w  w  w .j  av  a  2 s.  com
 * @param elementName
 * @param is
 * @param onlyValues
 * @return Collection
 * @throws XMLStreamException
 * @throws UnsupportedEncodingException
 */
private static Collection<String> getElements(final String elementName, final InputStream is,
        final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException {
    final Collection<String> elements = new ArrayList<>();
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final XMLEventReader reader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final XMLEventWriter writer = XMLOutputFactory.newInstance()
            .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name()));
    boolean read = false;
    String characters = null;

    while (reader.peek() != null) {
        final XMLEvent event = (XMLEvent) reader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            read = read || elementName.equals(event.asStartElement().getName().getLocalPart());

            if (read && !onlyValues) {
                writer.add(event);
            }

            break;
        }
        case XMLStreamConstants.ATTRIBUTE: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            characters = event.asCharacters().getData();
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (read && !onlyValues) {
                writer.add(event);
            }
            if (elementName.equals(event.asEndElement().getName().getLocalPart())) {
                writer.flush();

                if (characters != null) {
                    elements.add(characters);
                }

                os.reset();
                read = false;
            }
            break;
        }
        default: {
            // Ignore
            break;
        }
        }
    }

    return elements;
}

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++;//from   w  ww .  j a  v a2s .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();
    }
}