Example usage for javax.xml.stream XMLStreamConstants CDATA

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

Introduction

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

Prototype

int CDATA

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

Click Source Link

Document

Indicates an event is a CDATA section

Usage

From source file:Main.java

/** Get the human readable event name for the numeric event id */
public static String getEventName(int eventId) {
    switch (eventId) {
    case XMLStreamConstants.START_ELEMENT:
        return "StartElementEvent";
    case XMLStreamConstants.END_ELEMENT:
        return "EndElementEvent";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "ProcessingInstructionEvent";
    case XMLStreamConstants.CHARACTERS:
        return "CharacterEvent";
    case XMLStreamConstants.COMMENT:
        return "CommentEvent";
    case XMLStreamConstants.START_DOCUMENT:
        return "StartDocumentEvent";
    case XMLStreamConstants.END_DOCUMENT:
        return "EndDocumentEvent";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "EntityReferenceEvent";
    case XMLStreamConstants.ATTRIBUTE:
        return "AttributeBase";
    case XMLStreamConstants.DTD:
        return "DTDEvent";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    }//w w w. j a  va2s. com
    return "UNKNOWN_EVENT_TYPE";
}

From source file:Main.java

/**
 * Get the string representation of a given StAX event type. The returned
 * value is the name of the constant in {@link XMLStreamConstants}
 * corresponding to the event type.//from   w  ww.j a  v a2s.com
 * 
 * @param event
 *            the event type as returned by
 *            {@link javax.xml.stream.events.XMLEvent#getEventType()},
 *            {@link javax.xml.stream.XMLStreamReader#getEventType()} or
 *            {@link javax.xml.stream.XMLStreamReader#next()}
 * @return a string representation of the event type
 */
public static String getEventTypeString(int event) {
    String state = null;
    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        state = "START_ELEMENT";
        break;
    case XMLStreamConstants.START_DOCUMENT:
        state = "START_DOCUMENT";
        break;
    case XMLStreamConstants.CHARACTERS:
        state = "CHARACTERS";
        break;
    case XMLStreamConstants.CDATA:
        state = "CDATA";
        break;
    case XMLStreamConstants.END_ELEMENT:
        state = "END_ELEMENT";
        break;
    case XMLStreamConstants.END_DOCUMENT:
        state = "END_DOCUMENT";
        break;
    case XMLStreamConstants.SPACE:
        state = "SPACE";
        break;
    case XMLStreamConstants.COMMENT:
        state = "COMMENT";
        break;
    case XMLStreamConstants.DTD:
        state = "DTD";
        break;
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        state = "PROCESSING_INSTRUCTION";
        break;
    case XMLStreamConstants.ENTITY_REFERENCE:
        state = "ENTITY_REFERENCE";
        break;
    default:
        state = "UNKNOWN_STATE: " + event;
    }
    return state;
}

From source file:Main.java

public static String parse(XMLStreamReader reader) {
    StringBuffer result = new StringBuffer();
    if (reader != null) {
        try {/*from   w  w  w.j ava  2s  .c o m*/
            while (reader.hasNext()) {
                switch (reader.getEventType()) {

                case XMLStreamConstants.START_ELEMENT:
                    result.append("<");
                    printName(reader, result);
                    printNamespaces(reader, result);
                    printAttributes(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    result.append("</");
                    printName(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.SPACE:

                case XMLStreamConstants.CHARACTERS:
                    int start = reader.getTextStart();
                    int length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    break;

                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    result.append("<?");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("?>");
                    break;

                case XMLStreamConstants.CDATA:
                    result.append("<![CDATA[");
                    start = reader.getTextStart();
                    length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    result.append("]]>");
                    break;

                case XMLStreamConstants.COMMENT:
                    result.append("<!--");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("-->");
                    break;

                case XMLStreamConstants.ENTITY_REFERENCE:
                    result.append(reader.getLocalName()).append("=");
                    if (reader.hasText())
                        result.append("[").append(reader.getText()).append("]");
                    break;

                case XMLStreamConstants.START_DOCUMENT:
                    result.append("<?xml");
                    result.append(" version='").append(reader.getVersion()).append("'");
                    result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'");
                    if (reader.isStandalone())
                        result.append(" standalone='yes'");
                    else
                        result.append(" standalone='no'");
                    result.append("?>");
                    break;
                }
                reader.next();
            } // end while
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
        }
    }
    return result.toString();
}

From source file:net.di2e.ecdr.libs.result.relevance.TextParser.java

/**
 * Given xml as a string, this method will parse out element text and CDATA text. It separates
 * each by one space character./*from w  w w  .j  ava 2  s.c  o m*/
 *
 * @param xmlData
 *            XML as a {@code String}
 * @return parsed CDATA and element text
 */
protected static String parseTextFrom(String xmlData) {

    StringBuilder builder = new StringBuilder();

    XMLStreamReader xmlStreamReader;

    try {
        // xml parser does not handle leading whitespace
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlData));

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

            if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) {

                String text = xmlStreamReader.getText();

                if (StringUtils.isNotBlank(text)) {
                    builder.append(" " + text.trim());
                }

            }
            if (event == XMLStreamConstants.START_ELEMENT) {
                for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {

                    String text = xmlStreamReader.getAttributeValue(i);

                    if (StringUtils.isNotBlank(text)) {
                        builder.append(" " + text.trim());
                    }
                }
            }
        }
    } catch (XMLStreamException e1) {
        LOGGER.warn("Failure occurred in parsing the xml data (" + xmlData
                + "). No data has been stored or indexed.", e1);
    }

    return builder.toString();
}

From source file:com.widowcrawler.exo.parse.Parser.java

private static String getEventTypeString(int eventType) {
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        return "START_ELEMENT";
    case XMLStreamConstants.END_ELEMENT:
        return "END_ELEMENT";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "PROCESSING_INSTRUCTION";
    case XMLStreamConstants.CHARACTERS:
        return "CHARACTERS";
    case XMLStreamConstants.COMMENT:
        return "COMMENT";
    case XMLStreamConstants.START_DOCUMENT:
        return "START_DOCUMENT";
    case XMLStreamConstants.END_DOCUMENT:
        return "END_DOCUMENT";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "ENTITY_REFERENCE";
    case XMLStreamConstants.ATTRIBUTE:
        return "ATTRIBUTE";
    case XMLStreamConstants.DTD:
        return "DTD";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    case XMLStreamConstants.SPACE:
        return "SPACE";
    case XMLStreamConstants.ENTITY_DECLARATION:
        return "ENTITY_DECLARATION";
    case XMLStreamConstants.NAMESPACE:
        return "NAMESPACE";
    case XMLStreamConstants.NOTATION_DECLARATION:
        return "NOTATION_DECLARATION";
    }/*w w w.ja va2 s .  c o  m*/
    return "UNKNOWN_EVENT_TYPE: " + eventType;
}

From source file:com.wavemaker.runtime.data.parser.BaseHbmParser.java

protected String nextInternal(String parentElementName, int numTries, String... elementNames) {

    int i = 1;//from  ww w. j  av a  2s.  co  m

    try {

        for (int event = this.xmlReader.next(); event != XMLStreamConstants.END_DOCUMENT; event = this.xmlReader
                .next()) {

            switch (event) {
            case XMLStreamConstants.START_ELEMENT:

                this.currentText.delete(0, this.currentText.length());
                this.currentElementName = this.xmlReader.getName().toString();

                if (numTries > -1) {
                    if (i == numTries) {
                        return this.currentElementName;
                    } else if (i > numTries) {
                        return null;
                    }
                }

                i++;

                if (elementNames != null) {
                    for (String s : elementNames) {
                        if (s.equals(this.currentElementName)) {
                            return this.currentElementName;
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                String endElementName = this.xmlReader.getName().toString();
                if (endElementName.equals(parentElementName)) {
                    return null;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                this.currentText.append(this.xmlReader.getText());
                break;
            case XMLStreamConstants.CDATA:
                break;
            }
        }

    } catch (XMLStreamException ex) {
        throw new WMRuntimeException(ex);
    }

    return null;

}

From source file:lux.search.highlight.XmlHighlighter.java

@Override
public void handleEvent(XMLStreamReader reader, int eventType) throws XMLStreamException {

    switch (eventType) {
    case XMLStreamConstants.START_DOCUMENT:
        xmlStreamReader = reader; // cache the reader so we can pull events
        super.handleEvent(reader, eventType);
        break;/* w w  w .  java2  s.com*/

    case XMLStreamConstants.START_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens
                .pushElement(new QName(reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix()));
        break;

    case XMLStreamConstants.END_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens.popElement();
        break;

    case XMLStreamConstants.COMMENT:
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CDATA:
        throw new XMLStreamException("unexpected CDATA event");

    case XMLStreamConstants.SPACE:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CHARACTERS:
        textReader.text();
        try {
            highlightTextNode();
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
        break;

    case XMLStreamConstants.ENTITY_REFERENCE:
        throw new XMLStreamException("unexpected entity reference event");

    default:
        super.handleEvent(reader, eventType);
    }

}

From source file:org.javelin.sws.ext.bind.internal.model.ComplexTypePattern.java

@Override
public T consumeValue(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException {
    // first create an object to be filled (using PropertyAccessors - direct or bean) according to the content model
    T object = BeanUtils.instantiate(this.getJavaType());

    // the order is dictated by incoming events, not by the mode
    // TODO: create a property to enable strict unmarshalling - dictated by content model
    // only this (ContentModel) pattern iterates over XML Events
    XMLEvent event = null;//from   w  w w  .  j  a  va 2s  .  c om
    PropertyMetadataValue<T, ?> pmv = null;

    // this loop will only handle first level of start elements and only single end element
    // deeper levels will be handled by nested patterns
    while (true) {
        boolean end = false;
        event = eventReader.peek();
        pmv = null;

        switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            pmv = this.consumeNestedAttribute(eventReader, context);
            break;
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            if (this.simpleContent != null) {
                pmv = this.consumeSimpleContent(eventReader, context);
                break;
            }
        case XMLStreamConstants.COMMENT:
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.START_DOCUMENT:
            // strange
            break;
        case XMLStreamConstants.START_ELEMENT:
            pmv = this.consumeNestedElement(eventReader, context);
            break;
        case XMLStreamConstants.END_ELEMENT:
            // TODO: in mixed content there will be more than one end element it this content model's level
        case XMLStreamConstants.END_DOCUMENT:
            end = true;
            break;
        }

        if (end)
            break;

        if (pmv != null)
            pmv.getMetadata().setValue(object, pmv.getValue());
    }

    return (T) object;
}

From source file:com.marklogic.contentpump.AggregateXMLReader.java

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (xmlSR == null) {
        hasNext = false;/*from  w ww  . j a  v  a  2  s.c  o m*/
        return false;
    }

    try {
        while (xmlSR.hasNext()) {
            int eventType;
            //getCharacterOffset() returns int; 
            //int will overflows if file is larger than 2GB
            if (!overflow && xmlSR.getLocation().getCharacterOffset() < -1) {
                overflow = true;
                LOG.info("In progress...");
            }
            //do not update pos if offset overflows
            if (!overflow) {
                pos = xmlSR.getLocation().getCharacterOffset();
            }
            eventType = xmlSR.next();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                if (startOfRecord) {
                    // this is the start of the root, only copy
                    // namespaces
                    copyNameSpaceDecl();
                    startOfRecord = false;
                    continue;
                }
                processStartElement();
                break;
            case XMLStreamConstants.CHARACTERS:
                write(StringEscapeUtils.escapeXml(xmlSR.getText()));
                break;
            case XMLStreamConstants.CDATA:
                write("<![CDATA[");
                write(xmlSR.getText());
                write("]]>");
                break;
            case XMLStreamConstants.SPACE:
                write(xmlSR.getText());
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                write("&");
                write(xmlSR.getLocalName());
                write(";");
                break;
            case XMLStreamConstants.DTD:
                write("<!DOCTYPE");
                write(xmlSR.getText());
                write(">");
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                write("<?");
                write(xmlSR.getPIData());
                write("?>");
                break;
            case XMLStreamConstants.COMMENT:
                write("<!--");
                write(xmlSR.getText());
                write("-->");
                break;
            case XMLStreamConstants.END_ELEMENT:
                keepGoing = processEndElement();
                if (!keepGoing) {
                    keepGoing = true;
                    return true;
                }
                break;
            case XMLStreamConstants.START_DOCUMENT:
                throw new XMLStreamException("unexpected start of document within record!\n" + "recordName = "
                        + recordName + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation());
            case XMLStreamConstants.END_DOCUMENT:
                if (currentId != null) {
                    throw new XMLStreamException(
                            "end of document before end of current record!\n" + "recordName = " + recordName
                                    + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation());
                } else {
                    if (compressed) {
                        //this doc is done, refer to the zip for next doc
                        hasNext = false;
                        return false;
                    } else {
                        //get next file from FileIterator
                        if (iterator != null && iterator.hasNext()) {
                            close();
                            initStreamReader(iterator.next());
                            continue;
                        } else {
                            hasNext = false;
                            return false;
                        }
                    }
                }
            default:
                throw new XMLStreamException("UNIMPLEMENTED: " + eventType);
            }
        }
    } catch (XMLStreamException e) {
        LOG.error("Parsing error", e);
        throw new IOException("Parsing error", e);
    }

    return false;
}

From source file:com.liferay.portal.util.LocalizationImpl.java

private void _copyNonExempt(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter,
        String exemptLanguageId, String defaultLanguageId, boolean cdata) throws XMLStreamException {

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

        if (event == XMLStreamConstants.START_ELEMENT) {
            String languageId = xmlStreamReader.getAttributeValue(null, _LANGUAGE_ID);

            if (Validator.isNull(languageId)) {
                languageId = defaultLanguageId;
            }/*from  w w  w .  j av  a 2s .  c  o  m*/

            if (!languageId.equals(exemptLanguageId)) {
                xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                xmlStreamWriter.writeAttribute(_LANGUAGE_ID, languageId);

                while (xmlStreamReader.hasNext()) {
                    event = xmlStreamReader.next();

                    if ((event == XMLStreamConstants.CHARACTERS) || (event == XMLStreamConstants.CDATA)) {

                        String text = xmlStreamReader.getText();

                        if (cdata) {
                            xmlStreamWriter.writeCData(text);
                        } else {
                            xmlStreamWriter.writeCharacters(xmlStreamReader.getText());
                        }

                        break;
                    } else if (event == XMLStreamConstants.END_ELEMENT) {
                        break;
                    }
                }

                xmlStreamWriter.writeEndElement();
            }
        } else if (event == XMLStreamConstants.END_DOCUMENT) {
            break;
        }
    }
}