Search in sources :

Example 6 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class XMLStreamReaderInputStream method ensureBuffering.

private int ensureBuffering(int size) throws IOException {
    if (size < bpos) {
        return bpos;
    }
    // refill the buffer as more buffering is requested than the current buffer status
    try {
        // very first event
        if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
            writer.writeStartDocument(charset, "1.0");
        }
        if (chunk.size() < buffer.length) {
            while (reader.hasNext()) {
                int code = reader.next();
                switch(code) {
                    case XMLStreamConstants.END_DOCUMENT:
                        writer.writeEndDocument();
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        QName qname = reader.getName();
                        writer.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            String namespaceUri = reader.getAttributeNamespace(i);
                            writer.writeAttribute(reader.getAttributePrefix(i), namespaceUri == null ? "" : namespaceUri, reader.getAttributeLocalName(i), reader.getAttributeValue(i));
                        }
                        for (int i = 0; i < reader.getNamespaceCount(); i++) {
                            writer.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        writer.writeEndElement();
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        writer.writeCharacters(reader.getText());
                        break;
                    case XMLStreamConstants.COMMENT:
                        writer.writeComment(reader.getText());
                        break;
                    case XMLStreamConstants.CDATA:
                        writer.writeCData(reader.getText());
                        break;
                    default:
                        break;
                }
                // check if the chunk is full
                final int csize = buffer.length - bpos;
                if (chunk.size() > csize) {
                    System.arraycopy(chunk.getByteArray(), 0, buffer, bpos, csize);
                    bpos = buffer.length;
                    chunk.trim(csize, 0);
                    return buffer.length;
                }
            }
        }
        final int csize = chunk.size() < buffer.length - bpos ? chunk.size() : buffer.length - bpos;
        if (csize > 0) {
            System.arraycopy(chunk.getByteArray(), 0, buffer, bpos, csize);
            bpos += csize;
            chunk.trim(csize, 0);
            return bpos;
        } else {
            return bpos > 0 ? bpos : -1;
        }
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) IOException(java.io.IOException)

Example 7 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class XMLStreamReaderReader method ensureBuffering.

private int ensureBuffering(int size) throws IOException {
    if (size < bpos) {
        return bpos;
    }
    // refill the buffer as more buffering is requested than the current buffer status
    try {
        // very first event
        if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
            writer.writeStartDocument("utf-8", "1.0");
        }
        if (chunk.size() < buffer.length) {
            while (reader.hasNext()) {
                int code = reader.next();
                switch(code) {
                    case XMLStreamConstants.END_DOCUMENT:
                        writer.writeEndDocument();
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        QName qname = reader.getName();
                        writer.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i));
                        }
                        for (int i = 0; i < reader.getNamespaceCount(); i++) {
                            writer.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        writer.writeEndElement();
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        writer.writeCharacters(reader.getText());
                        break;
                    case XMLStreamConstants.COMMENT:
                        writer.writeComment(reader.getText());
                        break;
                    case XMLStreamConstants.CDATA:
                        writer.writeCData(reader.getText());
                        break;
                    default:
                        break;
                }
                // check if the chunk is full
                final int csize = buffer.length - bpos;
                if (chunk.size() > csize) {
                    System.arraycopy(chunk.getCharArray(), 0, buffer, bpos, csize);
                    bpos = buffer.length;
                    chunk.trim(csize, 0);
                    return buffer.length;
                }
            }
        }
        final int csize = chunk.size() < buffer.length - bpos ? chunk.size() : buffer.length - bpos;
        if (csize > 0) {
            System.arraycopy(chunk.getCharArray(), 0, buffer, bpos, csize);
            bpos += csize;
            chunk.trim(csize, 0);
            return bpos;
        } else {
            return bpos > 0 ? bpos : -1;
        }
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) IOException(java.io.IOException)

Example 8 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project hadoop by apache.

the class NodeInfo method checkConf.

public static List<String> checkConf(InputStream in) {
    List<NodeInfo> nodes = null;
    List<String> errors = new ArrayList<String>();
    try {
        nodes = parseConf(in);
        if (nodes == null) {
            errors.add("bad conf file: top-level element not <configuration>");
        }
    } catch (XMLStreamException e) {
        errors.add("bad conf file: " + e.getMessage());
    }
    if (!errors.isEmpty()) {
        return errors;
    }
    Map<String, List<Integer>> duplicatedProperties = new HashMap<String, List<Integer>>();
    for (NodeInfo node : nodes) {
        StartElement element = node.getStartElement();
        int line = element.getLocation().getLineNumber();
        if (!element.getName().equals(new QName("property"))) {
            errors.add(String.format("Line %d: element not <property>", line));
            continue;
        }
        List<XMLEvent> events = node.getXMLEventsForQName(new QName("name"));
        if (events == null) {
            errors.add(String.format("Line %d: <property> has no <name>", line));
        } else {
            String v = null;
            for (XMLEvent event : events) {
                if (event.isAttribute()) {
                    v = ((Attribute) event).getValue();
                } else {
                    Characters c = node.getElement(event.asStartElement());
                    if (c != null) {
                        v = c.getData();
                    }
                }
                if (v == null || v.isEmpty()) {
                    errors.add(String.format("Line %d: <property> has an empty <name>", line));
                }
            }
            if (v != null && !v.isEmpty()) {
                List<Integer> lines = duplicatedProperties.get(v);
                if (lines == null) {
                    lines = new ArrayList<Integer>();
                    duplicatedProperties.put(v, lines);
                }
                lines.add(node.getStartElement().getLocation().getLineNumber());
            }
        }
        events = node.getXMLEventsForQName(new QName("value"));
        if (events == null) {
            errors.add(String.format("Line %d: <property> has no <value>", line));
        }
        for (QName qName : node.getDuplicatedQNames()) {
            if (!qName.equals(new QName("source"))) {
                errors.add(String.format("Line %d: <property> has duplicated <%s>s", line, qName));
            }
        }
    }
    for (Entry<String, List<Integer>> e : duplicatedProperties.entrySet()) {
        List<Integer> lines = e.getValue();
        if (1 < lines.size()) {
            errors.add(String.format("Line %s: duplicated <property>s for %s", StringUtils.join(", ", lines), e.getKey()));
        }
    }
    return errors;
}
Also used : HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Characters(javax.xml.stream.events.Characters) ArrayList(java.util.ArrayList) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project hadoop by apache.

the class OfflineImageReconstructor method loadNodeChildrenHelper.

private void loadNodeChildrenHelper(Node parent, String expected, String[] terminators) throws IOException {
    XMLEvent ev = null;
    while (true) {
        try {
            ev = events.peek();
            switch(ev.getEventType()) {
                case XMLEvent.END_ELEMENT:
                    if (terminators.length != 0) {
                        return;
                    }
                    events.nextEvent();
                    return;
                case XMLEvent.START_ELEMENT:
                    String key = ev.asStartElement().getName().getLocalPart();
                    for (String terminator : terminators) {
                        if (terminator.equals(key)) {
                            return;
                        }
                    }
                    events.nextEvent();
                    Node node = new Node();
                    parent.addChild(key, node);
                    loadNodeChildrenHelper(node, expected, new String[0]);
                    break;
                case XMLEvent.CHARACTERS:
                    String val = XMLUtils.unmangleXmlString(ev.asCharacters().getData(), true);
                    parent.setVal(val);
                    events.nextEvent();
                    break;
                case XMLEvent.ATTRIBUTE:
                    throw new IOException("Unexpected XML event " + ev);
                default:
                    // Ignore other event types like comment, etc.
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Skipping XMLEvent " + ev);
                    }
                    events.nextEvent();
                    break;
            }
        } catch (XMLStreamException e) {
            throw new IOException("Expecting " + expected + ", but got XMLStreamException", e);
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 10 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project hbase by apache.

the class RemoteAdmin method getInputStream.

/**
   * Convert the REST server's response to an XML reader.
   *
   * @param response The REST server's response.
   * @return A reader over the parsed XML document.
   * @throws IOException If the document fails to parse
   */
private XMLStreamReader getInputStream(Response response) throws IOException {
    try {
        // Prevent the parser from reading XMl with external entities defined
        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        return xif.createXMLStreamReader(new ByteArrayInputStream(response.getBody()));
    } catch (XMLStreamException e) {
        throw new IOException("Failed to parse XML", e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)274 IOException (java.io.IOException)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)74 XMLInputFactory (javax.xml.stream.XMLInputFactory)60 InputStream (java.io.InputStream)43 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)34 XMLEvent (javax.xml.stream.events.XMLEvent)24 StringReader (java.io.StringReader)22 JAXBException (javax.xml.bind.JAXBException)22 XMLEventReader (javax.xml.stream.XMLEventReader)19 ModelNode (org.jboss.dmr.ModelNode)18 ArrayList (java.util.ArrayList)16 XMLExtendedStreamReader (org.jboss.staxmapper.XMLExtendedStreamReader)15 ModelElement (org.wildfly.extension.picketlink.common.model.ModelElement)15 SAXException (org.xml.sax.SAXException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 StringWriter (java.io.StringWriter)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Characters (javax.xml.stream.events.Characters)11