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();
}
}
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);
}
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);
}
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;
}
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;
}
Aggregations