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:Main.java

/**
 * Raises a new unexpected element exception, reporting the single expected element.
 * @param reader Reader.//from w ww .j a v  a2 s .c  om
 * @param expected Expected element.
 * @return Exception.
 */
public static XMLStreamException unexpectedElementException(XMLStreamReader reader, String expected) {
    return new XMLStreamException(
            String.format("Unexpected element. Expected %s, got %s.", expected, reader.getName().toString()));
}

From source file:Main.java

/**
 * Raises a new unexpected element exception.
 * @param reader XML reader./*from  w  ww  .java  2s .c  o m*/
 * @return Exception.
 */
public static XMLStreamException unexpectedElementException(XMLStreamReader reader) {
    return new XMLStreamException(String.format("Unexpected element: %s.", reader.getName().toString()));
}

From source file:Main.java

private static void writeAsEncodedUnicode(StartElement element, Writer writer, boolean isEmpty)
        throws XMLStreamException {
    try {/*from  w w  w .j a v a2  s .  c o  m*/
        // Write start tag.
        writer.write('<');
        QName name = element.getName();

        String prefix = name.getPrefix();
        if (prefix != null && prefix.length() > 0) {
            writer.write(prefix);
            writer.write(':');
        }
        writer.write(name.getLocalPart());

        // Write namespace declarations.
        Iterator nsIter = element.getNamespaces();
        while (nsIter.hasNext()) {
            Namespace ns = (Namespace) nsIter.next();
            writer.write(' ');
            ns.writeAsEncodedUnicode(writer);
        }

        // Write attributes
        Iterator attrIter = element.getAttributes();
        while (attrIter.hasNext()) {
            Attribute attr = (Attribute) attrIter.next();
            writer.write(' ');
            attr.writeAsEncodedUnicode(writer);
        }

        if (isEmpty)
            writer.write('/');
        writer.write('>');
    } catch (IOException ioe) {
        throw new XMLStreamException(ioe);
    }
}

From source file:Main.java

private static void writeAsEncodedUnicode(EndElement element, Writer writer) throws XMLStreamException {
    try {/*from www  . j av a2  s. c  om*/
        // Write end tags.
        writer.write("</");
        QName name = element.getName();
        String prefix = name.getPrefix();
        if (prefix != null && prefix.length() > 0) {
            writer.write(prefix);
            writer.write(':');
        }
        writer.write(name.getLocalPart());
        writer.write('>');
    } catch (IOException ioe) {
        throw new XMLStreamException(ioe);
    }
}

From source file:Main.java

public static String getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters)
        throws XMLStreamException {
    String rootElementName = getLocalName(element);

    StringWriter buffer = new StringWriter(1024);

    StartElement pendingElement = null;
    String pendingElementName = null;

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (pendingElement != null) {
            boolean skip = false;

            if (event.isEndElement() && pendingElementName.equals(getLocalName(event.asEndElement()))) {
                writeAsEncodedUnicode(pendingElement, buffer, true); // empty tag
                skip = true; // skip this end tag
            } else {
                writeAsEncodedUnicode(pendingElement, buffer, false);
            }/*from w w  w .j  ava2 s  . c  o m*/

            pendingElement = null;
            pendingElementName = null;

            if (skip)
                continue;
        }

        if (event.isEndElement()) {
            EndElement endElement = event.asEndElement();
            String name = getLocalName(endElement);

            if (rootElementName.equals(name))
                return buffer.toString();

            writeAsEncodedUnicode(endElement, buffer);
        } else if (event.isStartElement()) {
            pendingElement = event.asStartElement();
            pendingElementName = getLocalName(pendingElement);
        } else if (event.isCharacters() && decodeCharacters) {
            buffer.append(event.asCharacters().getData());
        } else {
            event.writeAsEncodedUnicode(buffer);
        }
    }

    throw new XMLStreamException(format("Missing closing tag for '" + rootElementName + "' element", element));
}

From source file:Main.java

/**
 * Utility method that throws an exception if the provided reader is not
 * positioned before a StartElement event with the specified tag name.
 * //from ww  w. j  a v  a2 s  .  c om
 * @param reader The reader to test.
 * @param qname The required name of the start-tag. If <code>null</code>,
 *            any start tag is accepted.
 * @throws XMLStreamException If an error occurs reading from the stream.
 */
public static final void requireStartElement(XMLEventReader reader, QName qname) throws XMLStreamException {

    if (reader.hasNext()) {

        XMLEvent nextEvent = reader.peek();
        if (nextEvent.isStartElement()) {

            if (qname != null) {

                StartElement start = nextEvent.asStartElement();
                QName name = start.getName();
                if (!name.equals(qname)) {

                    throw new XMLStreamException(
                            "Encountered unexpected element; expected " + qname + ", but found " + name);

                }

            }

        } else {

            throw new XMLStreamException("Encountered unexpected event; expected " + qname
                    + " start-tag, but found event " + nextEvent);

        }

    } else {

        throw new XMLStreamException("Encountered unexpected end of stream; expected element " + qname);

    }

}

From source file:com.backelite.sonarqube.commons.surefire.SurefireStaxHandler.java

private static long getTimeAttributeInMS(String value) throws XMLStreamException {
    // hardcoded to Locale.ENGLISH see http://jira.codehaus.org/browse/SONAR-602
    try {/*w  w w.  j  a v  a  2s  .  c o  m*/
        Double time = ParsingUtils.parseNumber(value, Locale.ENGLISH);
        return !Double.isNaN(time) ? (long) ParsingUtils.scaleValue(time * 1000, 3) : 0L;
    } catch (ParseException e) {
        throw new XMLStreamException(e);
    }
}

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

private static void parseProbesResult(XMLEventReader reader, Map<String, Result> result)
        throws XMLStreamException {
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement startElement = event.asStartElement();
            if (PROBE.matches(startElement.getName().getLocalPart())) {
                parseProbe(reader, startElement, result);
            }//from   www  . ja v a2s. c  o  m
        } else if (event.isEndElement()) {
            EndElement endElement = event.asEndElement();
            boolean isProbeEnd = PROBE.matches(endElement.getName().getLocalPart());
            boolean isProbeResultEnd = PROBES_RESULT.matches(endElement.getName().getLocalPart());
            if (!isProbeEnd && !isProbeResultEnd) {
                throw new XMLStreamException("Unexpected end element " + endElement.getName());
            }
            if (isProbeResultEnd) {
                return;
            }
        }
    }
}

From source file:com.qualitesys.sonarqcr4pblplugin.AbstractQcrStaxParser.java

public void parse(File violationsXMLFile) throws XMLStreamException {
    if (violationsXMLFile != null && violationsXMLFile.exists()) {
        InputStream input = null;
        try {//w w  w.j  ava2 s.  c  o  m
            input = new FileInputStream(violationsXMLFile);
            parse(input);

        } catch (FileNotFoundException e) {
            throw new XMLStreamException(e);

        } finally {
            IOUtils.closeQuietly(input);
        }
    }
}

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

private static Result parseOperationsPerSecResult(XMLEventReader reader) throws XMLStreamException {
    Long invocations = null;//from   w  w w. j a  v  a2  s. c om
    Double operationsPerSecond = null;

    while (reader.hasNext()) {
        XMLEvent xmlEvent = reader.nextEvent();
        if (xmlEvent.isStartElement()) {
            StartElement startElement = xmlEvent.asStartElement();
            if (INVOCATIONS.matches(startElement.getName().getLocalPart())) {
                if (invocations != null) {
                    throw new XMLStreamException(
                            "Unexpected element " + INVOCATIONS.getName() + " (has been already defined)");
                }
                invocations = Long.parseLong(parseCharsAndEndCurrentElement(reader));
            } else if (OPERATIONS_PER_SECOND.matches(startElement.getName().getLocalPart())) {
                if (operationsPerSecond != null) {
                    throw new XMLStreamException("Unexpected element " + OPERATIONS_PER_SECOND.getName()
                            + " (has been already defined)");
                }
                operationsPerSecond = Double.parseDouble(parseCharsAndEndCurrentElement(reader));
            }
            if (invocations != null && operationsPerSecond != null) {
                return new OperationsPerSecResult(invocations, operationsPerSecond);
            }
        }
    }
    throw new XMLStreamException("Unexpected end of stream");
}