Example usage for javax.xml.stream XMLStreamException XMLStreamException

List of usage examples for javax.xml.stream XMLStreamException XMLStreamException

Introduction

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

Prototype

public XMLStreamException(String msg, Location location) 

Source Link

Document

Construct an exception with the assocated message, exception and location.

Usage

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java

private String getElementText(XMLStreamReader xmlr) throws XMLStreamException {
    if (xmlr.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("parser must be on START_ELEMENT to read next text", xmlr.getLocation());
    }//from   w  w  w.  j  av a2  s .c om
    int eventType = xmlr.next();
    StringBuffer content = new StringBuffer();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                || eventType == XMLStreamConstants.SPACE
        /* || eventType == XMLStreamConstants.ENTITY_REFERENCE*/) {
            content.append(xmlr.getText());
        } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                || eventType == XMLStreamConstants.COMMENT
                || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            // skipping
        } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException("unexpected end of document when reading element text content");
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("element text content may not contain START_ELEMENT",
                    xmlr.getLocation());
        } else {
            throw new XMLStreamException("Unexpected event type " + eventType, xmlr.getLocation());
        }
        eventType = xmlr.next();
    }
    return content.toString();
}

From source file:nl.ordina.bag.etl.util.XMLStreamReaderUtils.java

public static Tag readTag(XMLStreamReader reader, Tag... tags) throws XMLStreamException {
    reader.nextTag();//w ww . j a  v  a 2 s .c  o  m
    for (Tag tag : tags)
        if (reader.getEventType() == tag.getElementType())
            if (reader.getLocalName().equals(tag.getLocalName()))
                return tag;
    throw new XMLStreamException("Encountered element [" + reader.getLocalName()
            + "] while one of the following elements [" + StringUtils.join(tags, ",") + "] was expected",
            reader.getLocation());
}

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * Returns the next tag.//from   w  w  w. j  a va2  s . c om
 *
 * @return Returns int.
 * @throws org.apache.axiom.om.impl.exception.OMStreamingException
 *
 * @throws XMLStreamException
 */
public int nextTag() throws XMLStreamException {
    int eventType = next();
    while ((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
            || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) // skip whitespace
            || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
            || eventType == XMLStreamConstants.COMMENT) {
        eventType = next();
    }
    if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
        throw new XMLStreamException("expected start or end tag", getLocation());
    }
    return eventType;
}

From source file:org.apache.axiom.om.impl.llom.OMStAXWrapper.java

/**
 * @return Returns String./*w  w  w.ja  va  2 s  .  c  om*/
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getElementText()
 */
public String getElementText() throws XMLStreamException {
    if (parser != null) {
        try {
            return parser.getElementText();
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        ///////////////////////////////////////////////////////
        //// Code block directly from the API documentation ///
        if (getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
        }
        int eventType = next();
        StringBuffer content = new StringBuffer();
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE
                    || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                content.append(getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content");
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT");
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
            }
            eventType = next();
        }
        return content.toString();
        ///////////////////////////////////////////////////////////////
    }

}

From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java

private void serializeDataHandler() throws XMLStreamException {
    try {//from   www  .j a va  2 s .  co  m
        if (dataHandlerReader.isDeferred()) {
            dataHandlerWriter.writeDataHandler(dataHandlerReader.getDataHandlerProvider(),
                    dataHandlerReader.getContentID(), dataHandlerReader.isOptimized());
        } else {
            dataHandlerWriter.writeDataHandler(dataHandlerReader.getDataHandler(),
                    dataHandlerReader.getContentID(), dataHandlerReader.isOptimized());
        }
    } catch (IOException ex) {
        throw new XMLStreamException("Error while reading data handler", ex);
    }
}

From source file:org.apache.axiom.om.impl.SwitchingWrapper.java

/**
 * @return Returns String.//from  w ww .j  a  v  a2 s .  c  o  m
 * @throws XMLStreamException
 * @see javax.xml.stream.XMLStreamReader#getElementText()
 */
public String getElementText() throws XMLStreamException {
    if (parser != null) {
        try {
            String elementText = parser.getElementText();
            currentEvent = END_ELEMENT;
            return elementText;
        } catch (XMLStreamException e) {
            throw new OMStreamingException(e);
        }
    } else {
        ///////////////////////////////////////////////////////
        //// Code block directly from the API documentation ///
        if (getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
        }
        int eventType = next();
        StringBuffer content = new StringBuffer();
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE
                    || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                content.append(getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content");
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT");
            } else {
                throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
            }
            eventType = next();
        }
        return content.toString();
        ///////////////////////////////////////////////////////////////
    }

}

From source file:org.apache.axiom.om.impl.SwitchingWrapper.java

/**
 * Method next.//from  w  ww  .  ja va2  s.c o  m
 *
 * @return Returns int.
 * @throws XMLStreamException
 */
public int next() throws XMLStreamException {
    switch (state) {
    case DOCUMENT_COMPLETE:
        throw new NoSuchElementException("End of the document reached");
    case COMPLETED:
        state = DOCUMENT_COMPLETE;
        currentEvent = END_DOCUMENT;
        break;
    case SWITCH_AT_NEXT:
        state = SWITCHED;

        // load the parser
        try {
            setParser((XMLStreamReader) builder.getParser());
        } catch (Exception e) {
            throw new XMLStreamException("problem accessing the parser. " + e.getMessage(), e);
        }

        // We should throw an END_DOCUMENT
        if ((currentEvent == START_DOCUMENT) && (currentEvent == parser.getEventType())) {
            currentEvent = parser.next();
        } else {
            currentEvent = parser.getEventType();
        }
        updateCompleteStatus();
        break;
    case NAVIGABLE:
        currentEvent = generateEvents(currentNode);
        updateCompleteStatus();
        updateLastNode();
        break;
    case SWITCHED:
        if (parser.hasNext()) {
            currentEvent = parser.next();
        }
        updateCompleteStatus();
        break;
    default:
        throw new OMStreamingException("unsuppported state!");
    }
    return currentEvent;
}

From source file:org.apache.axiom.util.stax.xop.XOPDecodingStreamReader.java

private static String toBase64(DataHandler dh) throws XMLStreamException {
    try {//from  w  w  w  .j  av a 2  s  .c  o  m
        return Base64Utils.encode(dh);
    } catch (IOException ex) {
        throw new XMLStreamException("Exception when encoding data handler as base64", ex);
    }
}

From source file:org.apache.axiom.util.stax.xop.XOPDecodingStreamReader.java

private String toBase64() throws XMLStreamException {
    if (base64 == null) {
        try {/*from   w w w . j av a  2  s  .c o  m*/
            base64 = toBase64(dh.getDataHandler());
        } catch (IOException ex) {
            throw new XMLStreamException("Failed to load MIME part '" + dh.getContentID() + "'", ex);
        }
    }
    return base64;
}

From source file:org.apache.axis2.databinding.utils.ConverterUtil.java

public static void serializeAnyType(Object value, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    if (value instanceof String) {
        serializeAnyType("string", value.toString(), xmlStreamWriter);
    } else if (value instanceof Integer) {
        serializeAnyType("int", value.toString(), xmlStreamWriter);
    } else if (value instanceof Boolean) {
        serializeAnyType("boolean", value.toString(), xmlStreamWriter);
    } else if (value instanceof URI) {
        serializeAnyType("anyURI", value.toString(), xmlStreamWriter);
    } else if (value instanceof Byte) {
        serializeAnyType("byte", value.toString(), xmlStreamWriter);
    } else if (value instanceof Date) {
        serializeAnyType("date", convertToString((Date) value), xmlStreamWriter);
    } else if (value instanceof Calendar) {
        serializeAnyType("dateTime", convertToString((Calendar) value), xmlStreamWriter);
    } else if (value instanceof Time) {
        serializeAnyType("time", convertToString((Time) value), xmlStreamWriter);
    } else if (value instanceof Float) {
        serializeAnyType("float", value.toString(), xmlStreamWriter);
    } else if (value instanceof Long) {
        serializeAnyType("long", value.toString(), xmlStreamWriter);
    } else if (value instanceof Double) {
        serializeAnyType("double", value.toString(), xmlStreamWriter);
    } else if (value instanceof Short) {
        serializeAnyType("short", value.toString(), xmlStreamWriter);
    } else if (value instanceof BigDecimal) {
        serializeAnyType("decimal", ((BigDecimal) value).toPlainString(), xmlStreamWriter);
    } else if (value instanceof DataHandler) {
        addTypeAttribute(xmlStreamWriter, "base64Binary");
        try {//from   w w  w .  j  a v a 2s .c o  m
            XMLStreamWriterUtils.writeDataHandler(xmlStreamWriter, (DataHandler) value, null, true);
        } catch (IOException ex) {
            throw new XMLStreamException("Unable to read data handler", ex);
        }
    } else if (value instanceof QName) {
        QName qNameValue = (QName) value;
        String prefix = xmlStreamWriter.getPrefix(qNameValue.getNamespaceURI());
        if (prefix == null) {
            prefix = BeanUtil.getUniquePrefix();
            xmlStreamWriter.writeNamespace(prefix, qNameValue.getNamespaceURI());
            xmlStreamWriter.setPrefix(prefix, qNameValue.getNamespaceURI());
        }
        String attributeValue = qNameValue.getLocalPart();
        if (!prefix.equals("")) {
            attributeValue = prefix + ":" + attributeValue;
        }
        serializeAnyType("QName", attributeValue, xmlStreamWriter);
    } else if (value instanceof UnsignedByte) {
        serializeAnyType("unsignedByte", convertToString((UnsignedByte) value), xmlStreamWriter);
    } else if (value instanceof UnsignedLong) {
        serializeAnyType("unsignedLong", convertToString((UnsignedLong) value), xmlStreamWriter);
    } else if (value instanceof UnsignedShort) {
        serializeAnyType("unsignedShort", convertToString((UnsignedShort) value), xmlStreamWriter);
    } else if (value instanceof UnsignedInt) {
        serializeAnyType("unsignedInt", convertToString((UnsignedInt) value), xmlStreamWriter);
    } else if (value instanceof PositiveInteger) {
        serializeAnyType("positiveInteger", convertToString((PositiveInteger) value), xmlStreamWriter);
    } else if (value instanceof NegativeInteger) {
        serializeAnyType("negativeInteger", convertToString((NegativeInteger) value), xmlStreamWriter);
    } else if (value instanceof NonNegativeInteger) {
        serializeAnyType("nonNegativeInteger", convertToString((NonNegativeInteger) value), xmlStreamWriter);
    } else if (value instanceof NonPositiveInteger) {
        serializeAnyType("nonPositiveInteger", convertToString((NonPositiveInteger) value), xmlStreamWriter);
    } else {
        throw new XMLStreamException("Unknow type can not serialize");
    }
}