Example usage for javax.xml.stream XMLInputFactory newFactory

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

Introduction

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

Prototype

public static XMLInputFactory newFactory() throws FactoryConfigurationError 

Source Link

Document

Create a new instance of the factory.

Usage

From source file:com.cisco.cta.taxii.adapter.AdapterConfiguration.java

@Bean
public XMLInputFactory inputFactory() {
    return XMLInputFactory.newFactory();
}

From source file:net.landora.anidb.mylist.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;/* w ww .j av  a2  s .co  m*/
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}

From source file:org.javelin.sws.ext.bind.SweJaxbUnmarshallerTest.java

@Test(expected = UnmarshalException.class)
public void unmarshalVeryComplexContent() throws Exception {
    JAXBContext context = JAXBContext.newInstance("org.javelin.sws.ext.bind.context1");
    // ClassWithVeryComplexContent value = new ClassWithVeryComplexContent("test", "str", new ClassWithComplexContent("test", 42, "inside"));
    Unmarshaller um = context.createUnmarshaller();
    InputStream inputStream = new ClassPathResource("very-complex-content-01.xml", this.getClass())
            .getInputStream();//from w  ww  .  java2  s  .  c o  m
    um.unmarshal(XMLInputFactory.newFactory().createXMLEventReader(inputStream));
}

From source file:com.streamsets.pipeline.lib.xml.StreamingXmlParser.java

public StreamingXmlParser(Reader reader, String recordElement, Map<String, String> namespaces,
        long initialPosition, boolean useFieldAttributesInsteadOfFields)
        throws IOException, XMLStreamException {
    this.reader = reader;
    this.useFieldAttributesInsteadOfFields = useFieldAttributesInsteadOfFields;
    if (Strings.isNullOrEmpty(recordElement)) {
        this.recordElement = Constants.ROOT_ELEMENT_PATH;
    } else {/*from w w  w  .  j a va 2s  . co m*/
        this.recordElement = recordElement;
    }
    XMLInputFactory factory = XMLInputFactory.newFactory();
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    factory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
    factory.setProperty("javax.xml.stream.supportDTD", false);
    this.xmlEventReader = new XPathMatchingEventReader(factory.createXMLEventReader(reader), this.recordElement,
            namespaces);
    while (hasNext(xmlEventReader) && !peek(xmlEventReader).isEndDocument()
            && !peek(xmlEventReader).isStartElement()) {
        read(xmlEventReader);
    }
    if (recordElement == null || recordElement.isEmpty()) {
        StartElement startE = (StartElement) peek(xmlEventReader);
        this.recordElement = startE.getName().getLocalPart();
    } else {
        //consuming root
        StartElement startE = (StartElement) read(xmlEventReader);
        elementNameStack.addFirst(getNameAndTrackNs(startE.getName()));
    }
    if (initialPosition > 0) {
        //fastforward to initial position
        while (hasNext(xmlEventReader)
                && peek(xmlEventReader).getLocation().getCharacterOffset() < initialPosition) {
            read(xmlEventReader);
            fastForwardLeaseReader();
        }
        xmlEventReader.clearLastMatch();
    }
}

From source file:com.adobe.acs.commons.it.build.ScrMetadataIT.java

public ScrMetadataIT() {
    this.xmlInputFactory = XMLInputFactory.newFactory();
    this.xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
}

From source file:org.fcrepo.serialization.JcrXmlSerializer.java

private void validateJCRXML(final File file) throws InvalidSerializationFormatException, IOException {
    int depth = 0;
    try (final FileInputStream fis = new FileInputStream(file)) {
        final XMLEventReader reader = XMLInputFactory.newFactory().createXMLEventReader(fis);
        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                depth++;/*from w  w  w.j a v  a 2 s .co  m*/
                final StartElement startElement = event.asStartElement();
                final Attribute nameAttribute = startElement
                        .getAttributeByName(new QName("http://www.jcp.org/jcr/sv/1.0", "name"));
                if (depth == 1 && nameAttribute != null && "jcr:content".equals(nameAttribute.getValue())) {
                    throw new InvalidSerializationFormatException(
                            "Cannot import JCR/XML starting with content node.");
                }
                if (depth == 1 && nameAttribute != null && "jcr:frozenNode".equals(nameAttribute.getValue())) {
                    throw new InvalidSerializationFormatException("Cannot import historic versions.");
                }
                final QName name = startElement.getName();
                if (!(name.getNamespaceURI().equals("http://www.jcp.org/jcr/sv/1.0")
                        && (name.getLocalPart().equals("node") || name.getLocalPart().equals("property")
                                || name.getLocalPart().equals("value")))) {
                    throw new InvalidSerializationFormatException(
                            "Unrecognized element \"" + name.toString() + "\", in import XML.");
                }
            } else {
                if (event.isEndElement()) {
                    depth--;
                }
            }
        }
        reader.close();
    } catch (XMLStreamException e) {
        throw new InvalidSerializationFormatException(
                "Unable to parse XML" + (e.getMessage() != null ? " (" + e.getMessage() + ")." : "."));
    }
}

From source file:org.javelin.sws.ext.bind.SweJaxbUnmarshallerTest.java

@Test
public void unmarshalVeryComplexContentExplicit() throws Exception {
    JAXBContext context = JAXBContext.newInstance("org.javelin.sws.ext.bind.context1");
    // ClassWithVeryComplexContent value = new ClassWithVeryComplexContent("test", "str", new ClassWithComplexContent("test", 42, "inside"));
    Unmarshaller um = context.createUnmarshaller();
    InputStream inputStream = new ClassPathResource("very-complex-content-01.xml", this.getClass())
            .getInputStream();/*from   ww  w . ja  v a  2  s.  c o  m*/
    JAXBElement<ClassWithVeryComplexContent> je = um.unmarshal(
            XMLInputFactory.newFactory().createXMLEventReader(inputStream), ClassWithVeryComplexContent.class);
    assertTrue(je.getDeclaredType() == ClassWithVeryComplexContent.class);
    assertFalse("Should not be nil", je.isNil());
    assertTrue(je.getValue() instanceof ClassWithVeryComplexContent);
    ClassWithVeryComplexContent ob = (ClassWithVeryComplexContent) je.getValue();
    assertThat(ob.getStr(), equalTo("test-1"));
    assertThat(ob.getInside(), equalTo("str"));
    assertThat(ob.getInside2().getNumber(), equalTo(42));
    assertThat(ob.getInside2().getStr(), equalTo("test-2"));
    assertThat(ob.getInside2().getInside(), equalTo("inside"));
}

From source file:com.googlesource.gerrit.plugins.supermanifest.JiriManifestParser.java

private static JiriManifest parseManifest(Repository repo, String ref, String file)
        throws JAXBException, IOException, XMLStreamException {
    byte[] b = Utils.readBlob(repo, ref + ":" + file);
    JAXBContext jc = JAXBContext.newInstance(JiriManifest.class);

    XMLInputFactory inf = XMLInputFactory.newFactory();
    inf.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    inf.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    XMLStreamReader sr = inf.createXMLStreamReader(new StreamSource(new ByteArrayInputStream(b)));

    return (JiriManifest) jc.createUnmarshaller().unmarshal(sr);
}

From source file:net.sf.jabref.importer.fileformat.MedlineImporter.java

@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
    Objects.requireNonNull(reader);

    List<BibEntry> bibItems = new ArrayList<>();

    try {/*from   w  w  w .  j  a v  a 2s  .  c o  m*/
        JAXBContext context = JAXBContext.newInstance("net.sf.jabref.importer.fileformat.medline");
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);

        //go to the root element
        while (!xmlStreamReader.isStartElement()) {
            xmlStreamReader.next();
        }

        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object unmarshalledObject = unmarshaller.unmarshal(xmlStreamReader);

        //check whether we have an article set, an article, a book article or a book article set
        if (unmarshalledObject instanceof PubmedArticleSet) {
            PubmedArticleSet articleSet = (PubmedArticleSet) unmarshalledObject;
            for (Object article : articleSet.getPubmedArticleOrPubmedBookArticle()) {
                if (article instanceof PubmedArticle) {
                    PubmedArticle currentArticle = (PubmedArticle) article;
                    parseArticle(currentArticle, bibItems);
                }
                if (article instanceof PubmedBookArticle) {
                    PubmedBookArticle currentArticle = (PubmedBookArticle) article;
                    parseBookArticle(currentArticle, bibItems);
                }
            }
        } else if (unmarshalledObject instanceof PubmedArticle) {
            PubmedArticle article = (PubmedArticle) unmarshalledObject;
            parseArticle(article, bibItems);
        } else if (unmarshalledObject instanceof PubmedBookArticle) {
            PubmedBookArticle currentArticle = (PubmedBookArticle) unmarshalledObject;
            parseBookArticle(currentArticle, bibItems);
        } else {
            PubmedBookArticleSet bookArticleSet = (PubmedBookArticleSet) unmarshalledObject;
            for (PubmedBookArticle bookArticle : bookArticleSet.getPubmedBookArticle()) {
                parseBookArticle(bookArticle, bibItems);
            }
        }
    } catch (JAXBException | XMLStreamException e) {
        LOGGER.debug("could not parse document", e);
        return ParserResult.fromErrorMessage(e.getLocalizedMessage());
    }
    return new ParserResult(bibItems);
}