Search in sources :

Example 46 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project midpoint by Evolveum.

the class Validator method readFromStreamAndValidate.

private EventResult readFromStreamAndValidate(XMLStreamReader stream, OperationResult objectResult, Map<String, String> rootNamespaceDeclarations, OperationResult validatorResult) {
    objectResult.addContext(START_LINE_NUMBER, stream.getLocation().getLineNumber());
    Document objectDoc;
    try {
        // Parse the object from stream to DOM
        objectDoc = domConverter.buildDocument(stream);
    } catch (XMLStreamException ex) {
        validatorResult.recordFatalError("XML parsing error: " + ex.getMessage(), ex);
        if (handler != null) {
            handler.handleGlobalError(validatorResult);
        }
        objectResult.recordFatalError(ex);
        return EventResult.skipObject(ex.getMessage());
    }
    objectResult.addContext(END_LINE_NUMBER, stream.getLocation().getLineNumber());
    // This element may not have complete namespace definitions for a
    // stand-alone
    // processing, therefore copy namespace definitions from the root
    // element
    Element objectElement = DOMUtil.getFirstChildElement(objectDoc);
    DOMUtil.setNamespaceDeclarations(objectElement, rootNamespaceDeclarations);
    return validateObjectInternal(objectElement, objectResult, validatorResult);
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 47 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project midpoint by Evolveum.

the class MidpointFunctionsImpl method parseXmlToMap.

@Override
public Map<String, String> parseXmlToMap(String xml) {
    Map<String, String> resultingMap = new HashMap<String, String>();
    if (xml != null && !xml.isEmpty()) {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        String value = "";
        String startName = "";
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        boolean isRootElement = true;
        try {
            XMLEventReader eventReader = factory.createXMLEventReader(stream);
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                Integer code = event.getEventType();
                if (code == XMLStreamConstants.START_ELEMENT) {
                    StartElement startElement = event.asStartElement();
                    startName = startElement.getName().getLocalPart();
                    if (!isRootElement) {
                        resultingMap.put(startName, null);
                    } else {
                        isRootElement = false;
                    }
                } else if (code == XMLStreamConstants.CHARACTERS) {
                    Characters characters = event.asCharacters();
                    if (!characters.isWhiteSpace()) {
                        StringBuilder valueBuilder;
                        if (value != null) {
                            valueBuilder = new StringBuilder(value).append(" ").append(characters.getData().toString());
                        } else {
                            valueBuilder = new StringBuilder(characters.getData().toString());
                        }
                        value = valueBuilder.toString();
                    }
                } else if (code == XMLStreamConstants.END_ELEMENT) {
                    EndElement endElement = event.asEndElement();
                    String endName = endElement.getName().getLocalPart();
                    if (endName.equals(startName)) {
                        if (value != null) {
                            resultingMap.put(endName, value);
                            value = null;
                        }
                    } else {
                        LOGGER.trace("No value between xml tags, tag name : {0}", endName);
                    }
                } else if (code == XMLStreamConstants.END_DOCUMENT) {
                    isRootElement = true;
                }
            }
        } catch (XMLStreamException e) {
            StringBuilder error = new StringBuilder("Xml stream exception wile parsing xml string").append(e.getLocalizedMessage());
            throw new SystemException(error.toString());
        }
    } else {
        LOGGER.trace("Input xml string null or empty.");
    }
    return resultingMap;
}
Also used : EndElement(javax.xml.stream.events.EndElement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Characters(javax.xml.stream.events.Characters) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 48 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project jdk8u_jdk by JetBrains.

the class SkipDTDTest method test.

@Test
public void test() {
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        for (int i = 0; i < 3; i++) {
            runReader(factory, i);
        }
    } catch (XMLStreamException xe) {
        xe.printStackTrace();
        Assert.fail(xe.getMessage());
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.testng.annotations.Test)

Example 49 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project OpenRefine by OpenRefine.

the class XmlImporter method descendElement.

private static final JSONObject descendElement(XMLStreamReader parser, PreviewParsingState state) {
    JSONObject result = new JSONObject();
    {
        String name = parser.getLocalName();
        JSONUtilities.safePut(result, "n", name);
        String prefix = parser.getPrefix();
        if (prefix != null) {
            JSONUtilities.safePut(result, "p", prefix);
        }
        String nsUri = parser.getNamespaceURI();
        if (nsUri != null) {
            JSONUtilities.safePut(result, "uri", nsUri);
        }
    }
    int namespaceCount = parser.getNamespaceCount();
    if (namespaceCount > 0) {
        JSONArray namespaces = new JSONArray();
        JSONUtilities.safePut(result, "ns", namespaces);
        for (int i = 0; i < namespaceCount; i++) {
            JSONObject namespace = new JSONObject();
            JSONUtilities.append(namespaces, namespace);
            JSONUtilities.safePut(namespace, "p", parser.getNamespacePrefix(i));
            JSONUtilities.safePut(namespace, "uri", parser.getNamespaceURI(i));
        }
    }
    int attributeCount = parser.getAttributeCount();
    if (attributeCount > 0) {
        JSONArray attributes = new JSONArray();
        JSONUtilities.safePut(result, "a", attributes);
        for (int i = 0; i < attributeCount; i++) {
            JSONObject attribute = new JSONObject();
            JSONUtilities.append(attributes, attribute);
            JSONUtilities.safePut(attribute, "n", parser.getAttributeLocalName(i));
            JSONUtilities.safePut(attribute, "v", parser.getAttributeValue(i));
            String prefix = parser.getAttributePrefix(i);
            if (prefix != null) {
                JSONUtilities.safePut(attribute, "p", prefix);
            }
        }
    }
    JSONArray children = new JSONArray();
    try {
        while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
            int tokenType = parser.next();
            state.tokenCount++;
            if (tokenType == XMLStreamConstants.END_ELEMENT) {
                break;
            } else if (tokenType == XMLStreamConstants.START_ELEMENT) {
                JSONObject childElement = descendElement(parser, state);
                if (childElement != null) {
                    JSONUtilities.append(children, childElement);
                }
            } else if (tokenType == XMLStreamConstants.CHARACTERS || tokenType == XMLStreamConstants.CDATA || tokenType == XMLStreamConstants.SPACE) {
                JSONObject childElement = new JSONObject();
                JSONUtilities.safePut(childElement, "t", parser.getText());
                JSONUtilities.append(children, childElement);
            } else {
            // ignore everything else
            }
        }
    } catch (XMLStreamException e) {
        logger.error("Error generating parser UI initialization data for XML file", e);
    }
    if (children.length() > 0) {
        JSONUtilities.safePut(result, "c", children);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) XMLStreamException(javax.xml.stream.XMLStreamException) JSONArray(org.json.JSONArray)

Example 50 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project OpenRefine by OpenRefine.

the class XmlImporter method wrapPrefixRemovingInputStream.

private static final InputStream wrapPrefixRemovingInputStream(InputStream inputStream) throws XMLStreamException, IOException {
    PushbackInputStream pis = new PushbackInputStream(inputStream);
    int b;
    int count = 0;
    while (count < 100 && (b = pis.read()) >= 0) {
        if (++count > 100) {
            throw new XMLStreamException("File starts with too much non-XML content to skip over");
        } else if (b == '<') {
            pis.unread(b);
            break;
        }
    }
    return pis;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) PushbackInputStream(java.io.PushbackInputStream)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)442 XMLStreamReader (javax.xml.stream.XMLStreamReader)137 IOException (java.io.IOException)126 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)75 XMLInputFactory (javax.xml.stream.XMLInputFactory)69 InputStream (java.io.InputStream)66 Document (org.w3c.dom.Document)36 JAXBException (javax.xml.bind.JAXBException)35 Fault (org.apache.cxf.interceptor.Fault)34 Element (org.w3c.dom.Element)32 StringReader (java.io.StringReader)30 XMLEvent (javax.xml.stream.events.XMLEvent)28 DOMSource (javax.xml.transform.dom.DOMSource)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)23 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)23 Node (org.w3c.dom.Node)22 StringWriter (java.io.StringWriter)21 XMLEventReader (javax.xml.stream.XMLEventReader)20