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(Throwable th) 

Source Link

Document

Construct an exception with the assocated exception

Usage

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * Reads from the XML stream and tries to determine if the ACI response contains an error or not. The stream is left
 * at the end of the body content of the <tt>/autnresponse/response</tt> element, if one could be found.
 * @param xmlStreamReader The response to process
 * @return <tt>true</tt> if the response contains an error, <tt>false</tt> otherwise
 * @throws javax.xml.stream.XMLStreamException If there was a problem reading the IDOL Server response.
 *//*w  w  w . jav  a 2 s.c o  m*/
protected boolean isErrorResponse(final XMLStreamReader xmlStreamReader) throws XMLStreamException {
    LOGGER.trace("isErrorResponse() called...");

    // Get the /autnresponse/response element...
    while (xmlStreamReader.hasNext()) {
        // Get the event type...
        final int eventType = xmlStreamReader.next();

        // Check to see if it's a start event...
        if ((XMLEvent.START_ELEMENT == eventType)
                && ("response".equalsIgnoreCase(xmlStreamReader.getLocalName()))) {
            return "ERROR".equalsIgnoreCase(xmlStreamReader.getElementText());
        }
    }

    // Couldn't find a /autnresponse/response element...
    throw new XMLStreamException("Unable to find /autnresponse/response element.");
}

From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java

private static void parseBuckets(XMLEventReader reader, LinearHistogram histogram) throws XMLStreamException {
    while (reader.hasNext()) {
        XMLEvent xmlEvent = reader.nextEvent();
        if (xmlEvent.isStartElement()) {
            StartElement startElement = xmlEvent.asStartElement();
            if (LATENCY_DIST_BUCKET.matches(startElement.getName().getLocalPart())) {
                parseBucket(reader, startElement, histogram);
            }/*  w ww . j a va  2s  . com*/
        } else if (xmlEvent.isEndElement()) {
            EndElement endElement = xmlEvent.asEndElement();
            if (!LATENCY_DIST_BUCKETS.matches(endElement.getName().getLocalPart())) {
                throw new XMLStreamException("Unexpected end element " + endElement.getName());
            }
            return;
        }
    }
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * Move the cursor forward through the XML stream to the next start element.
 * @param xmlStreamReader The XML stream to use
 * @throws XMLStreamException If there was an error using the stream
 *//*from ww  w .j a va2s .  co m*/
protected void forwardToNextStartElement(final XMLStreamReader xmlStreamReader) throws XMLStreamException {
    while (xmlStreamReader.hasNext()) {
        final int eventType = xmlStreamReader.next();
        if (XMLEvent.START_ELEMENT == eventType) {
            return;
        }
    }
    throw new XMLStreamException("No more START_ELEMENT events found");
}

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

private void processStartElement() throws XMLStreamException {
    String name = xmlSR.getLocalName();
    String namespace = xmlSR.getNamespaceURI();
    if (LOG.isTraceEnabled()) {
        LOG.trace("Start-tag: " + xmlSR.getName() + " at depth " + currDepth);
    }/*from   www .  j  a  v  a 2 s. com*/
    if (namespace == null) {
        String prefix = xmlSR.getPrefix();
        if ("".equals(prefix)) {
            prefix = DEFAULT_NS;
        }
        if (nameSpaces.get(prefix) != null) {
            namespace = nameSpaces.get(prefix).peek();
        }
    }

    String prefix = xmlSR.getPrefix();
    int attrCount = xmlSR.getAttributeCount();
    boolean isNewRootStart = false;
    currDepth++;
    Location loc = xmlSR.getLocation();
    if (recordName == null) {
        recordName = name;
        if (recordNamespace == null) {
            recordNamespace = namespace;
        }
        recordDepth = currDepth;
        isNewRootStart = true;
        newDoc = true;
        newUriId = true;
        if (useAutomaticId) {
            setKey(idGen.incrementAndGet(), loc.getLineNumber(), loc.getColumnNumber(), true);
        }
    } else {
        // record element name may not nest
        if (name.equals(recordName) && ((recordNamespace == null && namespace == null)
                || (recordNamespace != null && recordNamespace.equals(namespace)))) {
            recordDepth = currDepth;
            isNewRootStart = true;
            newDoc = true;
            newUriId = true;
            if (useAutomaticId) {
                setKey(idGen.incrementAndGet(), loc.getLineNumber(), loc.getColumnNumber(), true);
            }
        }
    }
    copyNameSpaceDecl();
    if (!newDoc) {
        return;
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<");
    if (prefix != null && !prefix.equals("")) {
        sb.append(prefix + ":" + name);
    } else {
        sb.append(name);
    }
    // add namespaces declared into the new root element
    if (isNewRootStart) {
        Set<String> keys = nameSpaces.keySet();
        for (String k : keys) {
            String v = nameSpaces.get(k).peek();
            if (DEFAULT_NS == k) {
                sb.append(" xmlns=\"" + v + "\"");
            } else {
                sb.append(" xmlns:" + k + "=\"" + v + "\"");
            }
        }
    } else {
        // add new namespace declaration into current element
        int stop = xmlSR.getNamespaceCount();
        if (stop > 0) {
            String nsDeclPrefix, nsDeclUri;
            if (LOG.isTraceEnabled()) {
                LOG.trace("checking namespace declarations");
            }
            for (int i = 0; i < stop; i++) {
                nsDeclPrefix = xmlSR.getNamespacePrefix(i);
                nsDeclUri = xmlSR.getNamespaceURI(i);
                if (LOG.isTraceEnabled()) {
                    LOG.trace(nsDeclPrefix + ":" + nsDeclUri);
                }
                if (DEFAULT_NS == nsDeclPrefix) {
                    sb.append(" xmlns=\"" + nsDeclUri + "\"");
                } else {
                    sb.append(" xmlns:" + nsDeclPrefix + "=\"" + nsDeclUri + "\"");
                }
            }
        }
    }
    for (int i = 0; i < attrCount; i++) {
        String aPrefix = xmlSR.getAttributePrefix(i);
        String aName = xmlSR.getAttributeLocalName(i);
        String aValue = StringEscapeUtils.escapeXml(xmlSR.getAttributeValue(i));
        sb.append(" " + (null == aPrefix ? "" : (aPrefix + ":")) + aName + "=\"" + aValue + "\"");
        if (!useAutomaticId && newDoc && ("@" + aName).equals(idName) && currentId == null) {
            currentId = aValue;
            setKey(aValue, loc.getLineNumber(), loc.getColumnNumber(), true);
        }
    }
    sb.append(">");

    // allow for repeated idName elements: first one wins
    // NOTE: idName is namespace-insensitive
    if (!useAutomaticId && newDoc && name.equals(idName)) {
        int nextToken = xmlSR.next();
        if (nextToken != XMLStreamConstants.CHARACTERS) {
            throw new XMLStreamException(
                    "badly formed xml or " + idName + " is not a simple node: at" + xmlSR.getLocation());
        }
        do {
            String idStr = StringEscapeUtils.escapeXml(xmlSR.getText());
            if (currentId == null) {
                currentId = "";
            }
            currentId += idStr;
            sb.append(idStr);
        } while ((nextToken = xmlSR.next()) == XMLStreamConstants.CHARACTERS);
        if (newUriId) {
            setKey(currentId, loc.getLineNumber(), loc.getColumnNumber(), true);
            newUriId = false;
        } else if (LOG.isDebugEnabled()) {
            LOG.debug("Duplicate URI_ID match found: key = " + key);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("URI_ID: " + currentId);
        }
        // advance to the END_ELEMENT
        if (nextToken != XMLStreamConstants.END_ELEMENT) {
            throw new XMLStreamException("badly formed xml: no END_TAG after id text" + xmlSR.getLocation());
        }
        sb.append("</");
        if (prefix != null && !prefix.equals("")) {
            sb.append(prefix + ":" + name);
        } else {
            sb.append(name);
        }
        sb.append(">");
        currDepth--;
    }
    write(sb.toString());
}

From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java

private static void parseBucket(XMLEventReader reader, StartElement element, LinearHistogram histogram)
        throws XMLStreamException {
    String upperBound = element.getAttributeByName(new QName(LATENCY_DIST_UPPER_BOUND.getName())).getValue();
    String values = element.getAttributeByName(new QName(LATENCY_DIST_VALUES.getName())).getValue();
    histogram.addMultipleValues(Integer.parseInt(upperBound) - 1, Integer.parseInt(values));

    while (reader.hasNext()) {
        XMLEvent xmlEvent = reader.nextEvent();
        if (xmlEvent.isEndElement()) {
            EndElement endElement = xmlEvent.asEndElement();
            if (LATENCY_DIST_BUCKET.getName().equals(endElement.getName().getLocalPart())) {
                return;
            }//www  .j  a v  a2 s  .c  om
        }
    }
    throw new XMLStreamException("Unexpected end of the document");
}

From source file:com.prowidesoftware.swift.model.mx.XmlEventWriter.java

public void flush() throws XMLStreamException {
    log.fine("flush ");
    try {/*ww  w .  java 2 s.  c o  m*/
        out.flush();
    } catch (IOException e) {
        throw new XMLStreamException(e);
    }
}

From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java

private static String parseCharsAndEndCurrentElement(XMLEventReader reader) throws XMLStreamException {
    XMLEvent xmlEvent = reader.nextEvent();
    if (!xmlEvent.isCharacters()) {
        throw new XMLStreamException("Unexpected event " + xmlEvent);
    }//from www . j a v a 2 s  .c o  m
    String data = xmlEvent.asCharacters().getData();
    while (reader.hasNext()) {
        xmlEvent = reader.nextEvent();
        if (xmlEvent.isEndElement()) {
            return data;
        }
    }
    throw new XMLStreamException("Unexpected end of the document");
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * Move the cursor forward through the XML stream to the next start or end element, which ever comes first.
 * @param xmlStreamReader The XML stream to use
 * @return The type of event forwarded to, i.e. <tt>XMLEvent.START_ELEMENT</tt> or <tt>XMLEvent.END_ELEMENT</tt>.
 * @throws XMLStreamException If there was an error using the stream
 *//*from w  ww . ja  v a 2s  . c  o  m*/
protected int forwardToNextStartOrEndElement(final XMLStreamReader xmlStreamReader) throws XMLStreamException {
    while (xmlStreamReader.hasNext()) {
        final int eventType = xmlStreamReader.next();
        if ((XMLEvent.START_ELEMENT == eventType) || (XMLEvent.END_ELEMENT == eventType)) {
            return eventType;
        }
    }
    throw new XMLStreamException("No more START_ELEMENT or END_ELEMENT events found");
}

From source file:com.autonomy.aci.client.services.impl.AbstractStAXProcessor.java

/**
 * Forwards through the stream looking for the an element with <tt>elementName</tt>
 * @param elementName     The name of the element to find.
 * @param xmlStreamReader The stream to forward through
 * @throws XMLStreamException If there was an error using the stream, or if no element with <tt>elementName</tt>
 *                            could be found
 *//*w ww . ja v  a 2s  . c o  m*/
protected void forwardToNamedStartElement(final String elementName, final XMLStreamReader xmlStreamReader)
        throws XMLStreamException {
    while (xmlStreamReader.hasNext()) {
        final int eventType = xmlStreamReader.next();
        if ((eventType == XMLEvent.START_ELEMENT) && (elementName.equals(xmlStreamReader.getLocalName()))) {
            return;
        }
    }
    throw new XMLStreamException("Unable to find a start element for, " + elementName);
}

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

private void processCodeBook(XMLStreamReader xmlr, SDIOMetadata smd) throws XMLStreamException {

    for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (xmlr.getLocalName().equals("fileDscr")) {
                processFileDscr(xmlr, smd);
            } else if (xmlr.getLocalName().equals("dataDscr")) {
                processDataDscr(xmlr, smd);
            } else {
                throw new XMLStreamException("Unsupported DDI Element:" + xmlr.getLocalName());
            }/* w w w.j a  v  a 2 s.c  o m*/
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            if (xmlr.getLocalName().equals("codeBook")) {
                dbgLog.info("processed codeBook section;");
                return;
            } else {
                throw new XMLStreamException(
                        "Mismatched DDI Formatting: </codeBook> expected, found " + xmlr.getLocalName());
            }
        }
    }

}