Search in sources :

Example 26 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project modrun by nanosai.

the class ModuleDependencyReader method readDependencies.

public static List<Dependency> readDependencies(Reader pomReader) {
    List<Dependency> dependencies = new ArrayList<>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {
        XMLStreamReader streamReader = factory.createXMLStreamReader(pomReader);
        while (streamReader.hasNext()) {
            streamReader.next();
            if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
                String elementName = streamReader.getLocalName();
                if (elementName.equals("dependency")) {
                    Dependency dependency = parseDependency(streamReader);
                    dependencies.add(dependency);
                }
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return dependencies;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 27 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project openhab1-addons by openhab.

the class DenonConnector method getDocument.

private <T> T getDocument(String uri, Class<T> response) {
    try {
        String result = doHttpRequest("GET", uri, null);
        logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);
        if (StringUtils.isNotBlank(result)) {
            JAXBContext jc = JAXBContext.newInstance(response);
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
            xsr = new PropertyRenamerDelegate(xsr);
            @SuppressWarnings("unchecked") T obj = (T) jc.createUnmarshaller().unmarshal(xsr);
            return obj;
        }
    } catch (UnmarshalException e) {
        logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
    } catch (JAXBException e) {
        logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
    } catch (XMLStreamException e) {
        logger.debug("Communication error: {}", e.getMessage());
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 28 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project spring-framework by spring-projects.

the class AbstractStaxHandler method startDocument.

@Override
public final void startDocument() throws SAXException {
    removeAllNamespaceMappings();
    newNamespaceMapping();
    try {
        startDocumentInternal();
    } catch (XMLStreamException ex) {
        throw new SAXException("Could not handle startDocument: " + ex.getMessage(), ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException)

Example 29 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project spring-framework by spring-projects.

the class AbstractXMLStreamReader method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    if (getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
    }
    int eventType = next();
    StringBuilder builder = new StringBuilder();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            builder.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", getLocation());
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("element text content may not contain START_ELEMENT", getLocation());
        } else {
            throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
        }
        eventType = next();
    }
    return builder.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException)

Example 30 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverter method read.

@Override
@SuppressWarnings("unchecked")
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ParameterizedType parameterizedType = (ParameterizedType) type;
    T result = createCollection((Class<?>) parameterizedType.getRawType());
    Class<?> elementClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    try {
        Unmarshaller unmarshaller = createUnmarshaller(elementClass);
        XMLStreamReader streamReader = this.inputFactory.createXMLStreamReader(inputMessage.getBody());
        int event = moveToFirstChildOfRootElement(streamReader);
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementClass.isAnnotationPresent(XmlRootElement.class)) {
                result.add(unmarshaller.unmarshal(streamReader));
            } else if (elementClass.isAnnotationPresent(XmlType.class)) {
                result.add(unmarshaller.unmarshal(streamReader, elementClass).getValue());
            } else {
                // should not happen, since we check in canRead(Type)
                throw new HttpMessageConversionException("Could not unmarshal to [" + elementClass + "]");
            }
            event = moveToNextElement(streamReader);
        }
        return result;
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    } catch (XMLStreamException ex) {
        throw new HttpMessageConversionException(ex.getMessage(), ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) Unmarshaller(javax.xml.bind.Unmarshaller)

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