Search in sources :

Example 41 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project jackrabbit-oak by apache.

the class WikipediaImport method importWikipedia.

public int importWikipedia(Session session) throws Exception {
    long start = System.currentTimeMillis();
    int count = 0;
    int code = 0;
    if (doReport) {
        System.out.format("Importing %s...%n", dump);
    }
    String type = "nt:unstructured";
    if (session.getWorkspace().getNodeTypeManager().hasNodeType("oak:Unstructured")) {
        type = "oak:Unstructured";
    }
    Node wikipedia = session.getRootNode().addNode("wikipedia", type);
    int levels = 0;
    if (!flat) {
        // estimate that the average XML size of a page is about 1kB
        for (long pages = dump.length() / 1024; pages > 256; pages /= 256) {
            levels++;
        }
    }
    String title = null;
    String text = null;
    XMLInputFactory factory = XMLInputFactory.newInstance();
    StreamSource source;
    if (dump.getName().endsWith(".xml")) {
        source = new StreamSource(dump);
    } else {
        CompressorStreamFactory csf = new CompressorStreamFactory();
        source = new StreamSource(csf.createCompressorInputStream(new BufferedInputStream(new FileInputStream(dump))));
    }
    haltImport = false;
    XMLStreamReader reader = factory.createXMLStreamReader(source);
    while (reader.hasNext() && !haltImport) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                if ("title".equals(reader.getLocalName())) {
                    title = reader.getElementText();
                } else if ("text".equals(reader.getLocalName())) {
                    text = reader.getElementText();
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if ("page".equals(reader.getLocalName())) {
                    String name = Text.escapeIllegalJcrChars(title);
                    Node parent = wikipedia;
                    if (levels > 0) {
                        int n = name.length();
                        for (int i = 0; i < levels; i++) {
                            int hash = name.substring(min(i, n)).hashCode();
                            parent = JcrUtils.getOrAddNode(parent, String.format("%02x", hash & 0xff));
                        }
                    }
                    Node page = parent.addNode(name);
                    page.setProperty("title", title);
                    page.setProperty("text", text);
                    code += title.hashCode();
                    code += text.hashCode();
                    count++;
                    if (count % 1000 == 0) {
                        batchDone(session, start, count);
                    }
                    pageAdded(title, text);
                }
                break;
        }
    }
    session.save();
    if (doReport) {
        long millis = System.currentTimeMillis() - start;
        System.out.format("Imported %d pages in %d seconds (%.2fms/page)%n", count, millis / 1000, (double) millis / count);
    }
    return code;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) BufferedInputStream(java.io.BufferedInputStream) Node(javax.jcr.Node) StreamSource(javax.xml.transform.stream.StreamSource) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) XMLInputFactory(javax.xml.stream.XMLInputFactory) FileInputStream(java.io.FileInputStream)

Example 42 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project karaf by apache.

the class StaxParser method getInputFactory.

private static synchronized XMLInputFactory getInputFactory() {
    if (StaxParser.inputFactory == null) {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        StaxParser.inputFactory = factory;
    }
    return StaxParser.inputFactory;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 43 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project wildfly by wildfly.

the class PersistenceUnitParseProcessor method parse.

private void parse(final VirtualFile persistence_xml, final List<PersistenceUnitMetadataHolder> listPUHolders, final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
    ROOT_LOGGER.tracef("parse checking if %s exists, result = %b", persistence_xml.toString(), persistence_xml.exists());
    if (persistence_xml.exists() && persistence_xml.isFile()) {
        InputStream is = null;
        try {
            is = persistence_xml.openStream();
            final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setXMLResolver(NoopXMLResolver.create());
            XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
            PersistenceUnitMetadataHolder puHolder = PersistenceUnitXmlParser.parse(xmlReader, SpecDescriptorPropertyReplacement.propertyReplacer(deploymentUnit));
            postParseSteps(persistence_xml, puHolder, deploymentUnit);
            listPUHolders.add(puHolder);
        } catch (Exception e) {
            throw new DeploymentUnitProcessingException(JpaLogger.ROOT_LOGGER.failedToParse(persistence_xml), e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) PersistenceUnitMetadataHolder(org.jboss.as.jpa.config.PersistenceUnitMetadataHolder) InputStream(java.io.InputStream) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 44 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project wildfly by wildfly.

the class JSFManagedBeanProcessor method processXmlManagedBeans.

/**
     * Parse the faces config files looking for managed bean classes. The parser is quite
     * simplistic as the only information we need is the managed-bean-class element
     */
private void processXmlManagedBeans(final DeploymentUnit deploymentUnit, final Set<String> managedBeanClasses) {
    for (final VirtualFile facesConfig : getConfigurationFiles(deploymentUnit)) {
        InputStream is = null;
        try {
            is = facesConfig.openStream();
            final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setXMLResolver(NoopXMLResolver.create());
            XMLStreamReader parser = inputFactory.createXMLStreamReader(is);
            StringBuilder className = null;
            int indent = 0;
            boolean managedBean = false;
            boolean managedBeanClass = false;
            while (true) {
                int event = parser.next();
                if (event == XMLStreamConstants.END_DOCUMENT) {
                    parser.close();
                    break;
                }
                if (event == XMLStreamConstants.START_ELEMENT) {
                    indent++;
                    if (indent == 2) {
                        if (parser.getLocalName().equals(MANAGED_BEAN)) {
                            managedBean = true;
                        }
                    } else if (indent == 3 && managedBean) {
                        if (parser.getLocalName().equals(MANAGED_BEAN_CLASS)) {
                            managedBeanClass = true;
                            className = new StringBuilder();
                        }
                    }
                } else if (event == XMLStreamConstants.END_ELEMENT) {
                    indent--;
                    managedBeanClass = false;
                    if (indent == 1) {
                        managedBean = false;
                    }
                    if (className != null) {
                        managedBeanClasses.add(className.toString().trim());
                        className = null;
                    }
                } else if (managedBeanClass && event == XMLStreamConstants.CHARACTERS) {
                    className.append(parser.getText());
                }
            }
        } catch (Exception e) {
            JSFLogger.ROOT_LOGGER.managedBeansConfigParseFailed(facesConfig);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) IOException(java.io.IOException)

Example 45 with XMLInputFactory

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

the class Jaxb2CollectionHttpMessageConverter method createXmlInputFactory.

/**
	 * Create a {@code XMLInputFactory} that this converter will use to create {@link
	 * javax.xml.stream.XMLStreamReader} and {@link javax.xml.stream.XMLEventReader} objects.
	 * <p>Can be overridden in subclasses, adding further initialization of the factory.
	 * The resulting factory is cached, so this method will only be called once.
	 */
protected XMLInputFactory createXmlInputFactory() {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
    return inputFactory;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory)

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