Example usage for javax.xml.stream XMLInputFactory newInstance

List of usage examples for javax.xml.stream XMLInputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory newInstance.

Prototype

public static XMLInputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:org.commonjava.maven.galley.maven.parse.XMLInfrastructure.java

public XMLInfrastructure() {
    transformerFactory = TransformerFactory.newInstance();

    if (!transformerFactory.getClass().getName().contains("redirected")) {
        safeInputFactory = XMLInputFactory.newInstance();
        safeInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        safeInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
    } else {/*from  w  w w. java  2  s  .c  o m*/
        logger.warn("Somebody is playing games with the TransformerFactory...we cannot use it safely: {}",
                transformerFactory);
        safeInputFactory = null;
    }

    dbFactory = DocumentBuilderFactory.newInstance();

    // TODO: Probably don't need these available, since it's unlikely Maven can do much with them.
    dbFactory.setValidating(false);
    dbFactory.setXIncludeAware(false);
    dbFactory.setNamespaceAware(false);

    // TODO: Are these wise?? We're mainly interested in harvesting POM information, not preserving fidelity...
    dbFactory.setIgnoringComments(true);
    dbFactory.setExpandEntityReferences(false);
    dbFactory.setCoalescing(true);
}

From source file:org.commonjava.rwx.impl.stax.StaxParser.java

public StaxParser(final InputStream in) throws XmlRpcException {
    factory = XMLInputFactory.newInstance();
    try {/*  w  w  w .  j  a v  a 2  s.c  o m*/
        reader = factory.createXMLStreamReader(in);
    } catch (final XMLStreamException e) {
        throw new XmlRpcException("Failed to initialize stream reader: " + e.getMessage(), e);
    }
}

From source file:org.commonjava.rwx.impl.stax.StaxParser.java

public StaxParser(final Reader in) throws XmlRpcException {
    factory = XMLInputFactory.newInstance();
    try {/*from  www.j ava 2s  . co  m*/
        reader = factory.createXMLStreamReader(in);
    } catch (final XMLStreamException e) {
        throw new XmlRpcException("Failed to initialize stream reader: " + e.getMessage(), e);
    }
}

From source file:org.commonjava.rwx.impl.stax.StaxParser.java

public StaxParser(final String in) throws XmlRpcException {
    factory = XMLInputFactory.newInstance();
    try {/*w  w  w  .  j  a va  2  s .c  o m*/
        reader = factory.createXMLStreamReader(new StringReader(in));
    } catch (final XMLStreamException e) {
        throw new XmlRpcException("Failed to initialize stream reader: " + e.getMessage(), e);
    }
}

From source file:org.deegree.client.core.renderer.OutputXMLRenderer.java

private void encodeXML(ResponseWriter writer, String value) throws IOException {
    int depth = 0;
    try {/*  w w w.  j  a v a  2 s  . c om*/
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(value));
        boolean lastWasEndElement = false;
        boolean lastWasComment = false;
        while (reader.hasNext()) {
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                if (!lastWasComment) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                }
                writer.write(getSpaces(depth));
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("<");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                String prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String attributePrefix = reader.getAttributePrefix(i);
                    writer.write(
                            (attributePrefix != null && attributePrefix.length() > 0 ? attributePrefix + ":"
                                    : "") + reader.getAttributeLocalName(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(reader.getAttributeValue(i)));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                for (int i = 0; i < reader.getNamespaceCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String nsPrefix = reader.getNamespacePrefix(i);
                    writer.write("xmlns");
                    if (nsPrefix != null && !nsPrefix.isEmpty()) {
                        writer.write(":" + nsPrefix);
                    }
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(reader.getNamespaceURI(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                depth++;
                lastWasEndElement = false;
                lastWasComment = false;
                break;
            case XMLStreamConstants.CHARACTERS:
                String text = reader.getText();
                if (text.trim().length() > 0) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(text));
                    writer.endElement("span");
                    lastWasEndElement = false;
                    lastWasComment = false;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                depth--;
                if (lastWasEndElement) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                    writer.write(getSpaces(depth));
                }
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;/");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                lastWasEndElement = true;
                lastWasComment = false;
                break;
            case XMLStreamConstants.COMMENT:
                writer.startElement("div", null);
                writer.writeAttribute("class", "comment", null);
                writer.write("&lt;/!--" + reader.getText() + "--&gt;");
                writer.endElement("div");
                lastWasEndElement = false;
                lastWasComment = true;
                break;
            default:
                break;
            }
            reader.next();

        }
        reader.close();
    } catch (Throwable e) {
        if (depth == 0) {
            writer.writeText("Response could not parsed as XML.", null);
        } else {
            writer.writeText("... (if you want the complete document, please click the download button)", null);
        }
    }
}

From source file:org.deegree.commons.config.AbstractResourceManager.java

@Override
protected ExtendedResourceProvider<T> getProvider(URL file) {
    String namespace = null;//from   w  ww.  j a  v a 2  s .c o m
    InputStream is = null;
    try {
        is = new DURL(file.toExternalForm()).openStream();
        XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(is);
        XMLStreamUtils.nextElement(xmlReader);
        namespace = xmlReader.getNamespaceURI();
        LOG.debug("Config namespace: '" + namespace + "'");
        xmlReader.close();
        return nsToProvider.get(namespace);
    } catch (Throwable e) {
        String msg = "Error determining configuration namespace for file '" + file + "'";
        LOG.error(msg);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return null;
}

From source file:org.deegree.cs.persistence.CRSManager.java

/**
 * Returns an uninitialized {@link CRSStore} instance that's created from the specified CRSStore configuration
 * document./*from   www  .ja  v a 2s.  c o  m*/
 * 
 * @param configURL
 *            URL of the configuration document, must not be <code>null</code>
 * @return corresponding {@link CRSStore} instance, not yet initialized, never <code>null</code>
 * @throws CRSStoreException
 *             if the creation fails, e.g. due to a configuration error
 */
public synchronized CRSStore create(URL configURL) throws CRSStoreException {
    String namespace = null;
    XMLStreamReader xmlReader = null;
    InputStream urlStream = null;
    try {
        urlStream = configURL.openStream();
        xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(urlStream);
        XMLStreamUtils.nextElement(xmlReader);
        namespace = xmlReader.getNamespaceURI();
    } catch (Exception e) {
        String msg = Messages.get("CRSManager.CREATING_STORE_FAILED", configURL);
        LOG.error(msg);
        throw new CRSStoreException(msg);
    } finally {
        closeQuietly(xmlReader);
        IOUtils.closeQuietly(urlStream);
    }
    LOG.debug("Config namespace: '" + namespace + "'");
    CRSStoreProvider provider = getProviders().get(namespace);
    if (provider == null) {
        String msg = Messages.get("CRSManager.MISSING_PROVIDER", namespace, configURL);
        LOG.error(msg);
        throw new CRSStoreException(msg);
    }
    return provider.getCRSStore(configURL, workspace);
}

From source file:org.deegree.filter.xml.Filter200XMLEncoderParameterizedTest.java

private Filter parseFilter(String filterAsString) throws XMLStreamException, FactoryConfigurationError {
    XMLStreamReader in = XMLInputFactory.newInstance().createXMLStreamReader(toInputStream(filterAsString));
    in.nextTag();/*  w  ww.j a  v  a 2s . c o m*/
    return Filter200XMLDecoder.parse(in);
}

From source file:org.deegree.maven.ithelper.ServiceIntegrationTestHelper.java

public void testCapabilities(String service) throws MojoFailureException {
    String address = createBaseURL() + "services/" + service.toLowerCase() + "?request=GetCapabilities&service="
            + service;//ww w  .  ja v a 2  s  .com
    try {
        log.info("Reading capabilities from " + address);
        String input = IOUtils.toString(new URL(address).openStream(), "UTF-8");
        XMLInputFactory fac = XMLInputFactory.newInstance();
        XMLStreamReader in = fac.createXMLStreamReader(new StringReader(input));
        in.next();
        if (in.getLocalName().toLowerCase().contains("exception")) {
            log.error("Actual response was:");
            log.error(input);
            throw new MojoFailureException("Retrieving capabilities from " + address + " failed.");
        }
    } catch (Throwable e) {
        log.debug("Failed to retrieve capabilities.", e);
        throw new MojoFailureException(
                "Retrieving capabilities for " + service + " failed: " + e.getLocalizedMessage(), e);
    }
}

From source file:org.deegree.maven.XMLCatalogueMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    File target = new File(project.getBasedir(), "target");
    target.mkdirs();/*from w w w .ja  va  2  s .  c  o m*/
    target = new File(target, "deegree.xmlcatalog");

    PrintStream catalogOut = null;
    try {
        catalogOut = new PrintStream(new FileOutputStream(target), true, "UTF-8");
        final PrintStream catalog = catalogOut;

        addDependenciesToClasspath(project, artifactResolver, artifactFactory, metadataSource, localRepository);

        final XMLInputFactory fac = XMLInputFactory.newInstance();
        final Reflections r = new Reflections("/META-INF/schemas/");

        class CurrentState {
            String location;
        }

        final CurrentState state = new CurrentState();

        r.collect("META-INF/schemas", new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                state.location = input;
                return input != null && input.endsWith(".xsd");
            }
        }, new Serializer() {
            @Override
            public Reflections read(InputStream in) {
                try {
                    XMLStreamReader reader = fac.createXMLStreamReader(in);
                    nextElement(reader);
                    String location = "classpath:META-INF/schemas/" + state.location;
                    String ns = reader.getAttributeValue(null, "targetNamespace");
                    catalog.println("PUBLIC \"" + ns + "\" \"" + location + "\"");
                } catch (Throwable e) {
                    getLog().error(e);
                }
                return r;
            }

            @Override
            public File save(Reflections reflections, String filename) {
                return null;
            }

            @Override
            public String toString(Reflections reflections) {
                return null;
            }
        });
    } catch (Throwable t) {
        throw new MojoFailureException("Creating xml catalog failed: " + t.getLocalizedMessage(), t);
    } finally {
        closeQuietly(catalogOut);
    }
}