Search in sources :

Example 66 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project webservices-axiom by apache.

the class StAXUtils method getXMLInputFactory.

/**
     * Get a cached {@link XMLInputFactory} instance using the specified configuration.
     * 
     * @param configuration
     *            the configuration applied to the requested factory
     * @return an {@link XMLInputFactory} instance.
     */
public static XMLInputFactory getXMLInputFactory(StAXParserConfiguration configuration) {
    if (configuration == null) {
        configuration = StAXParserConfiguration.DEFAULT;
    }
    XMLInputFactory f = inputFactoryMap.get(configuration);
    if (f == null) {
        f = newXMLInputFactory(StAXUtils.class.getClassLoader(), configuration);
        inputFactoryMap.put(configuration, f);
        if (log.isDebugEnabled()) {
            if (f != null) {
                log.debug("Created singleton XMLInputFactory " + f.getClass() + " with configuration " + configuration);
            }
        }
    }
    return f;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory) ImmutableXMLInputFactory(org.apache.axiom.util.stax.wrapper.ImmutableXMLInputFactory)

Example 67 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project ddf by codice.

the class TransactionRequestConverter method getElementFromXml.

private <T> T getElementFromXml(String xml, Class<T> clazz) {
    JAXBElement<T> root;
    try {
        JAXBContext jaxbContext = getJaxBContext();
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xml));
        root = jaxbContext.createUnmarshaller().unmarshal(xmlStreamReader, clazz);
    } catch (JAXBException | XMLStreamException e) {
        throw new ConversionException(e);
    }
    return root.getValue();
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 68 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project bnd by bndtools.

the class ObrContentProvider method checkStream.

public CheckResult checkStream(String name, InputStream stream) throws IOException {
    XMLStreamReader reader = null;
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        reader = inputFactory.createXMLStreamReader(stream);
        ParserState state = ParserState.beforeRoot;
        while (reader.hasNext()) {
            int type = reader.next();
            String localName;
            switch(type) {
                case PROCESSING_INSTRUCTION:
                    if (PI_TARGET_STYLESHEET.equals(reader.getPITarget()) && PI_DATA_STYLESHEET.equals(reader.getPIData()))
                        return new CheckResult(accept, "Recognised stylesheet", null);
                    break;
                case START_ELEMENT:
                    localName = reader.getLocalName();
                    switch(state) {
                        case beforeRoot:
                            String nsUri = reader.getNamespaceURI();
                            if (nsUri != null)
                                return CheckResult.fromBool(NS_URI.equals(nsUri), "Correct namespace on root element", "Incorrect namespace on root element: " + nsUri, null);
                            if (!TAG_REPOSITORY.equals(localName))
                                return new CheckResult(reject, "Incorrect root element name", null);
                            state = ParserState.inRoot;
                            break;
                        case inRoot:
                            if (TAG_RESOURCE.equals(localName)) {
                                state = ParserState.inResource;
                            } else if (!TAG_REFERRAL.equals(localName)) {
                                return new CheckResult(reject, String.format("Incorrect element '%s', expected '%s' or '%s'.", localName, TAG_RESOURCE, TAG_REFERRAL), null);
                            }
                            break;
                        case inResource:
                            if (TAG_CAPABILITY.equals(localName)) {
                                state = ParserState.inCapability;
                            }
                            break;
                        case inCapability:
                            return CheckResult.fromBool(TAG_PROPERTY.equals(localName), "Found 'p' tag inside 'capability'", String.format("Incorrect element '%s' inside '%s'; expected '%s'.", localName, TAG_CAPABILITY, TAG_PROPERTY), null);
                    }
                    break;
                case END_ELEMENT:
                    localName = reader.getLocalName();
                    if (state == ParserState.inResource && TAG_RESOURCE.equals(localName))
                        state = ParserState.inRoot;
                    if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName))
                        state = ParserState.inResource;
                    break;
            }
        }
        return new CheckResult(undecided, "Reached end of stream", null);
    } catch (XMLStreamException e) {
        return new CheckResult(reject, "Invalid XML", e);
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) CheckResult(aQute.bnd.deployer.repository.api.CheckResult) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 69 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project bnd by bndtools.

the class R5RepoContentProvider method checkStream.

public CheckResult checkStream(String name, InputStream stream) throws IOException {
    XMLStreamReader reader = null;
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        reader = inputFactory.createXMLStreamReader(stream);
        ParserState state = ParserState.beforeRoot;
        while (reader.hasNext()) {
            int type = reader.next();
            String localName;
            switch(type) {
                case START_ELEMENT:
                    localName = reader.getLocalName();
                    switch(state) {
                        case beforeRoot:
                            String nsUri = reader.getNamespaceURI();
                            if (nsUri != null)
                                return CheckResult.fromBool(NS_URI.equals(nsUri), "Corrent namespace", "Incorrect namespace: " + nsUri, null);
                            if (!TAG_REPOSITORY.equals(localName))
                                return new CheckResult(reject, "Incorrect root element name", null);
                            state = ParserState.inRoot;
                            break;
                        case inRoot:
                            if (TAG_RESOURCE.equals(localName)) {
                                state = ParserState.inResource;
                            }
                            break;
                        case inResource:
                            if (TAG_REQUIREMENT.equals(localName))
                                return new CheckResult(accept, "Recognised element 'requirement' in 'resource'", null);
                            if (TAG_CAPABILITY.equals(localName))
                                state = ParserState.inCapability;
                            break;
                        case inCapability:
                            if (TAG_ATTRIBUTE.equals(localName) || TAG_DIRECTIVE.equals(localName)) {
                                return new CheckResult(accept, "Recognised element '%s' in 'capability'", null);
                            }
                            break;
                    }
                    break;
                case END_ELEMENT:
                    localName = reader.getLocalName();
                    if (state == ParserState.inResource && TAG_RESOURCE.equals(localName))
                        state = ParserState.inRoot;
                    if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName))
                        state = ParserState.inResource;
                    break;
                default:
                    break;
            }
        }
        return new CheckResult(undecided, "Reached end of stream", null);
    } catch (XMLStreamException e) {
        return new CheckResult(reject, "Invalid XML", e);
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) CheckResult(aQute.bnd.deployer.repository.api.CheckResult) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 70 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project bnd by bndtools.

the class R5RepoContentProvider method parseIndex.

public void parseIndex(InputStream stream, URI baseUri, IRepositoryIndexProcessor listener, LogService log) throws Exception {
    XMLStreamReader reader = null;
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        reader = inputFactory.createXMLStreamReader(stream);
        ResourceBuilder resourceBuilder = null;
        CapReqBuilder capReqBuilder = null;
        while (reader.hasNext()) {
            int type = reader.next();
            String localName;
            switch(type) {
                case START_ELEMENT:
                    localName = reader.getLocalName();
                    if (TAG_REFERRAL.equals(localName)) {
                        String url = reader.getAttributeValue(null, ATTR_REFERRAL_URL);
                        String depth = reader.getAttributeValue(null, ATTR_REFERRAL_DEPTH);
                        Referral referral = new Referral(url, parseInt(depth));
                        listener.processReferral(baseUri, referral, 0, 0);
                    } else if (TAG_RESOURCE.equals(localName)) {
                        resourceBuilder = new ResourceBuilder();
                    } else if (TAG_CAPABILITY.equals(localName) || TAG_REQUIREMENT.equals(localName)) {
                        String namespace = reader.getAttributeValue(null, ATTR_NAMESPACE);
                        capReqBuilder = new CapReqBuilder(namespace);
                    } else if (TAG_ATTRIBUTE.equals(localName)) {
                        String name = reader.getAttributeValue(null, ATTR_NAME);
                        String valueStr = reader.getAttributeValue(null, ATTR_VALUE);
                        String typeAttr = reader.getAttributeValue(null, ATTR_TYPE);
                        if (capReqBuilder != null) {
                            // base URI.
                            if (ContentNamespace.CONTENT_NAMESPACE.equals(capReqBuilder.getNamespace()) && ContentNamespace.CAPABILITY_URL_ATTRIBUTE.equals(name)) {
                                URI resolvedUri = resolveUri(valueStr, baseUri);
                                capReqBuilder.addAttribute(name, resolvedUri);
                            } else {
                                Object convertedAttr = convertAttribute(valueStr, typeAttr);
                                capReqBuilder.addAttribute(name, convertedAttr);
                            }
                        }
                    } else if (TAG_DIRECTIVE.equals(localName)) {
                        String name = reader.getAttributeValue(null, ATTR_NAME);
                        String valueStr = reader.getAttributeValue(null, ATTR_VALUE);
                        if (capReqBuilder != null)
                            capReqBuilder.addDirective(name, valueStr);
                    }
                    break;
                case END_ELEMENT:
                    localName = reader.getLocalName();
                    if (TAG_CAPABILITY.equals(localName)) {
                        if (resourceBuilder != null && capReqBuilder != null)
                            resourceBuilder.addCapability(capReqBuilder);
                        capReqBuilder = null;
                    } else if (TAG_REQUIREMENT.equals(localName)) {
                        if (resourceBuilder != null && capReqBuilder != null)
                            resourceBuilder.addRequirement(capReqBuilder);
                        capReqBuilder = null;
                    } else if (TAG_RESOURCE.equals(localName)) {
                        if (resourceBuilder != null) {
                            Resource resource = resourceBuilder.build();
                            listener.processResource(resource);
                            resourceBuilder = null;
                        }
                    }
                    break;
            }
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) XMLStreamReader(javax.xml.stream.XMLStreamReader) Referral(aQute.bnd.deployer.repository.api.Referral) Resource(org.osgi.resource.Resource) URI(java.net.URI) XMLInputFactory(javax.xml.stream.XMLInputFactory) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)182 XMLStreamReader (javax.xml.stream.XMLStreamReader)114 XMLStreamException (javax.xml.stream.XMLStreamException)74 InputStream (java.io.InputStream)54 StringReader (java.io.StringReader)45 IOException (java.io.IOException)40 XMLEventReader (javax.xml.stream.XMLEventReader)36 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Test (org.junit.Test)29 InputStreamReader (java.io.InputStreamReader)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 XMLEvent (javax.xml.stream.events.XMLEvent)15 StAXSource (javax.xml.transform.stax.StAXSource)15 StreamSource (javax.xml.transform.stream.StreamSource)15 ArrayList (java.util.ArrayList)14 JAXBException (javax.xml.bind.JAXBException)14 HashMap (java.util.HashMap)13 DOMSource (javax.xml.transform.dom.DOMSource)12 StartElement (javax.xml.stream.events.StartElement)10 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10