Search in sources :

Example 91 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project languagetool by languagetool-org.

the class IpaExtractor method run.

private void run(FileInputStream fis) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(fis);
    String title = null;
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
            String elementName = event.asStartElement().getName().getLocalPart();
            switch(elementName) {
                case "title":
                    XMLEvent nextEvent = reader.nextEvent();
                    title = nextEvent.asCharacters().getData();
                    articleCount++;
                    break;
                case "text":
                    ipaCount += handleTextElement(title, reader);
                    break;
            }
        }
    }
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 92 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project languagetool by languagetool-org.

the class AtomFeedParser method getAtomFeedItems.

List<AtomFeedItem> getAtomFeedItems(InputStream xml) throws XMLStreamException {
    String id = null;
    String title = null;
    Date date = null;
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(xml);
    try {
        List<AtomFeedItem> items = new ArrayList<>();
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                String localPart = event.asStartElement().getName().getLocalPart();
                switch(localPart) {
                    case "id":
                        id = getCharacterData(eventReader);
                        break;
                    case "title":
                        title = getCharacterData(eventReader);
                        break;
                    case "updated":
                        String dateString = getCharacterData(eventReader);
                        try {
                            // e.g. 2013-12-03T09:48:29Z - got this from http://stackoverflow.com/questions/6038136,
                            // with SimpleDateParser the hour is off by one:
                            date = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateString).toGregorianCalendar().getTime();
                        } catch (Exception e) {
                            throw new RuntimeException("Could not parse date string '" + dateString + "'", e);
                        }
                        break;
                    case "summary":
                        if (id == null || title == null || date == null) {
                            throw new RuntimeException("id, title and/or date is null: id=" + id + ", title=" + title + ", date=" + date);
                        }
                        items.add(new AtomFeedItem(id, title, getCharacterData(eventReader), date));
                        id = null;
                        title = null;
                        date = null;
                        break;
                }
            }
        }
        return items;
    } finally {
        eventReader.close();
    }
}
Also used : ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Date(java.util.Date) XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 93 with XMLInputFactory

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

the class LoadTask method readWorkspace.

private WorkspaceImpl readWorkspace(ProjectImpl project, String entryName, ZipFile zipFile) throws Exception {
    ZipEntry entry = zipFile.getEntry(entryName);
    if (entry != null) {
        InputStream is = null;
        try {
            is = zipFile.getInputStream(entry);
            InputStreamReader isReader = null;
            Xml10FilterReader filterReader = null;
            XMLStreamReader reader = null;
            try {
                XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                if (inputFactory.isPropertySupported("javax.xml.stream.isValidating")) {
                    inputFactory.setProperty("javax.xml.stream.isValidating", Boolean.FALSE);
                }
                inputFactory.setXMLReporter(new XMLReporter() {

                    @Override
                    public void report(String message, String errorType, Object relatedInformation, Location location) throws XMLStreamException {
                    }
                });
                isReader = new InputStreamReader(is, "UTF-8");
                filterReader = new Xml10FilterReader(isReader);
                reader = inputFactory.createXMLStreamReader(filterReader);
                return GephiReader.readWorkspace(reader, project);
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (filterReader != null) {
                    filterReader.close();
                }
                if (isReader != null) {
                    isReader.close();
                }
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLReporter(javax.xml.stream.XMLReporter) InputStreamReader(java.io.InputStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) XMLInputFactory(javax.xml.stream.XMLInputFactory) Location(javax.xml.stream.Location)

Example 94 with XMLInputFactory

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

the class ImporterGraphML method execute.

@Override
public boolean execute(ContainerLoader container) {
    this.container = container;
    this.report = new Report();
    Progress.start(progress);
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        if (inputFactory.isPropertySupported("javax.xml.stream.isValidating")) {
            inputFactory.setProperty("javax.xml.stream.isValidating", Boolean.FALSE);
        }
        inputFactory.setXMLReporter(new XMLReporter() {

            @Override
            public void report(String message, String errorType, Object relatedInformation, Location location) throws XMLStreamException {
            }
        });
        xmlReader = inputFactory.createXMLStreamReader(reader);
        while (xmlReader.hasNext()) {
            Integer eventType = xmlReader.next();
            if (eventType.equals(XMLEvent.START_ELEMENT)) {
                String name = xmlReader.getLocalName();
                if (GRAPHML.equalsIgnoreCase(name)) {
                } else if (GRAPH.equalsIgnoreCase(name)) {
                    readGraph(xmlReader);
                } else if (NODE.equalsIgnoreCase(name)) {
                    readNode(xmlReader, null);
                } else if (EDGE.equalsIgnoreCase(name)) {
                    readEdge(xmlReader);
                } else if (ATTRIBUTE.equalsIgnoreCase(name)) {
                    readAttribute(xmlReader);
                }
            } else if (eventType.equals(XMLStreamReader.END_ELEMENT)) {
                String name = xmlReader.getLocalName();
                if (NODE.equalsIgnoreCase(name)) {
                }
            }
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RuntimeException(e);
    } finally {
        try {
            xmlReader.close();
        } catch (XMLStreamException e) {
        }
    }
    Progress.finish(progress);
    return !cancel;
}
Also used : BigInteger(java.math.BigInteger) XMLReporter(javax.xml.stream.XMLReporter) XMLStreamException(javax.xml.stream.XMLStreamException) Report(org.gephi.io.importer.api.Report) XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException) Location(javax.xml.stream.Location)

Example 95 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project hibernate-orm by hibernate.

the class JaxbCfgProcessor method buildStaxFactory.

@SuppressWarnings({ "UnnecessaryLocalVariable" })
private XMLInputFactory buildStaxFactory() {
    XMLInputFactory staxFactory = XMLInputFactory.newInstance();
    staxFactory.setXMLResolver(xmlResourceResolver);
    return staxFactory;
}
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