Search in sources :

Example 71 with XMLInputFactory

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

the class EventSpecificTestCase method runTest.

protected final void runTest() throws Throwable {
    XMLInputFactory factory = staxImpl.getDialect().enableCDataReporting(staxImpl.newNormalizedXMLInputFactory());
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
    InputStream in = IllegalStateExceptionTestCase.class.getResourceAsStream("alleventtypes.xml");
    try {
        XMLStreamReader reader = factory.createXMLStreamReader(in);
        while (true) {
            if (reader.getEventType() == event) {
                break;
            } else if (reader.hasNext()) {
                reader.next();
            } else {
                fail("Internal error: didn't encounter event " + event);
            }
        }
        runTest(reader);
    } finally {
        in.close();
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 72 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project jabref by JabRef.

the class MedlineImporter method importDatabase.

@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
    Objects.requireNonNull(reader);
    List<BibEntry> bibItems = new ArrayList<>();
    try {
        JAXBContext context = JAXBContext.newInstance("org.jabref.logic.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.fromError(e);
    }
    return new ParserResult(bibItems);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) XMLStreamReader(javax.xml.stream.XMLStreamReader) PubmedBookArticleSet(org.jabref.logic.importer.fileformat.medline.PubmedBookArticleSet) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) ParserResult(org.jabref.logic.importer.ParserResult) PubmedBookArticle(org.jabref.logic.importer.fileformat.medline.PubmedBookArticle) XMLStreamException(javax.xml.stream.XMLStreamException) PubmedArticleSet(org.jabref.logic.importer.fileformat.medline.PubmedArticleSet) Unmarshaller(javax.xml.bind.Unmarshaller) PubmedArticle(org.jabref.logic.importer.fileformat.medline.PubmedArticle) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 73 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.

the class ImportExportController method createSourceXmlEventReader.

protected BufferedXMLEventReader createSourceXmlEventReader(MultipartFile multipartFile) throws IOException {
    final InputStream inputStream = multipartFile.getInputStream();
    final String name = multipartFile.getOriginalFilename();
    final XMLInputFactory xmlInputFactory = this.xmlUtilities.getXmlInputFactory();
    final XMLEventReader xmlEventReader;
    try {
        xmlEventReader = xmlInputFactory.createXMLEventReader(name, inputStream);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XML Event Reader for data Source", e);
    }
    return new BufferedXMLEventReader(xmlEventReader, -1);
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 74 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.

the class XSLTComponentTest method getXmlEventReader.

protected XMLEventReader getXmlEventReader(String file) throws SAXException, IOException, ParserConfigurationException {
    final InputStream xmlStream = this.getClass().getResourceAsStream(file);
    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    final Document document = documentBuilder.parse(xmlStream);
    final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    final XMLEventReader xmlEventReader;
    try {
        xmlEventReader = inputFactory.createXMLEventReader(new DOMSource(document, "foobar"));
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLStreamReader for file '" + file + "'", e);
    }
    return xmlEventReader;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) XMLEventReader(javax.xml.stream.XMLEventReader) Document(org.w3c.dom.Document) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 75 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project opennms by OpenNMS.

the class HypericAckProcessor method parseHypericAlerts.

/**
 * <p>parseHypericAlerts</p>
 *
 * @param reader a {@link java.io.Reader} object.
 * @return a {@link java.util.List} object.
 * @throws javax.xml.bind.JAXBException if any.
 * @throws javax.xml.stream.XMLStreamException if any.
 */
public static List<HypericAlertStatus> parseHypericAlerts(Reader reader) throws JAXBException, XMLStreamException {
    List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>();
    // Instantiate a JAXB context to parse the alert status
    JAXBContext context = JAXBContext.newInstance(new Class[] { HypericAlertStatuses.class, HypericAlertStatus.class });
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLEventReader xmler = xmlif.createXMLEventReader(reader);
    EventFilter filter = new EventFilter() {

        @Override
        public boolean accept(XMLEvent event) {
            return event.isStartElement();
        }
    };
    XMLEventReader xmlfer = xmlif.createFilteredReader(xmler, filter);
    // Read up until the beginning of the root element
    StartElement startElement = (StartElement) xmlfer.nextEvent();
    // Fetch the root element name for {@link HypericAlertStatus} objects
    String rootElementName = context.createJAXBIntrospector().getElementName(new HypericAlertStatuses()).getLocalPart();
    if (rootElementName.equals(startElement.getName().getLocalPart())) {
        Unmarshaller unmarshaller = context.createUnmarshaller();
        // Use StAX to pull parse the incoming alert statuses
        while (xmlfer.peek() != null) {
            Object object = unmarshaller.unmarshal(xmler);
            if (object instanceof HypericAlertStatus) {
                HypericAlertStatus alertStatus = (HypericAlertStatus) object;
                retval.add(alertStatus);
            }
        }
    } else {
        // Try to pull in the HTTP response to give the user a better idea of what went wrong
        final StringBuilder errorContent = new StringBuilder();
        LineNumberReader lineReader = new LineNumberReader(reader);
        try {
            String line;
            while (true) {
                line = lineReader.readLine();
                if (line == null) {
                    break;
                } else {
                    errorContent.append(line.trim());
                }
            }
        } catch (IOException e) {
            errorContent.append("Exception while trying to print out message content: " + e.getMessage());
        }
        // Throw an exception and include the erroneous HTTP response in the exception text
        throw new JAXBException("Found wrong root element in Hyperic XML document, expected: \"" + rootElementName + "\", found \"" + startElement.getName().getLocalPart() + "\"\n" + errorContent.toString());
    }
    return retval;
}
Also used : JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) EventFilter(javax.xml.stream.EventFilter) LineNumberReader(java.io.LineNumberReader) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(javax.xml.bind.Unmarshaller) 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