use of javax.xml.stream.XMLStreamReader in project uPortal by Jasig.
the class XSLTComponent method getEventReader.
/* (non-Javadoc)
* @see org.apereo.portal.rendering.StAXPipelineComponent#getXmlStreamReader(java.lang.Object, java.lang.Object)
*/
@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
final Transformer transformer = this.transformerSource.getTransformer(request, response);
//Setup a URIResolver based on the current resource loader
transformer.setURIResolver(this.uriResolver);
//Configure the Transformer via injected class
if (this.xsltParameterSource != null) {
final Map<String, Object> transformerParameters = this.xsltParameterSource.getParameters(request, response);
if (transformerParameters != null) {
this.logger.debug("{} - Setting Transformer Parameters: ", this.beanName, transformerParameters);
for (final Map.Entry<String, Object> transformerParametersEntry : transformerParameters.entrySet()) {
final String name = transformerParametersEntry.getKey();
final Object value = transformerParametersEntry.getValue();
if (value != null) {
transformer.setParameter(name, value);
}
}
}
final Properties outputProperties = this.xsltParameterSource.getOutputProperties(request, response);
if (outputProperties != null) {
this.logger.debug("{} - Setting Transformer Output Properties: ", this.beanName, outputProperties);
transformer.setOutputProperties(outputProperties);
}
}
//The event reader from the previous component in the pipeline
final XMLEventReader eventReader = pipelineEventReader.getEventReader();
//Wrap the event reader in a stream reader to avoid a JDK bug
final XMLStreamReader streamReader;
try {
streamReader = new FixedXMLEventStreamReader(eventReader);
} catch (XMLStreamException e) {
throw new RuntimeException("Failed to create XMLStreamReader from XMLEventReader", e);
}
final Source xmlReaderSource = new StAXSource(streamReader);
//Setup logging for the transform
transformer.setErrorListener(this.errorListener);
//Transform to a SAX ContentHandler to avoid JDK bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6775588
final XMLEventBufferWriter eventWriterBuffer = new XMLEventBufferWriter();
final ContentHandler contentHandler = StaxUtils.createLexicalContentHandler(eventWriterBuffer);
contentHandler.setDocumentLocator(new LocatorImpl());
final SAXResult outputTarget = new SAXResult(contentHandler);
try {
this.logger.debug("{} - Begining XML Transformation", this.beanName);
transformer.transform(xmlReaderSource, outputTarget);
this.logger.debug("{} - XML Transformation complete", this.beanName);
} catch (TransformerException e) {
throw new RuntimeException("Failed to transform document", e);
}
final String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
final List<XMLEvent> eventBuffer = eventWriterBuffer.getEventBuffer();
final XMLEventReader outputEventReader = new XMLEventBufferReader(eventBuffer.listIterator());
final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
final PipelineEventReaderImpl<XMLEventReader, XMLEvent> pipelineEventReaderImpl = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(outputEventReader, outputProperties);
pipelineEventReaderImpl.setOutputProperty(OutputKeys.MEDIA_TYPE, mediaType);
return pipelineEventReaderImpl;
}
use of javax.xml.stream.XMLStreamReader in project uPortal by Jasig.
the class XsltPortalUrlProviderTest method setup.
@Before
public void setup() throws Exception {
final ResourceLoaderURIResolver resolver = new ResourceLoaderURIResolver(new ClassRelativeResourceLoader(getClass()));
this.tFactory = TransformerFactory.newInstance();
this.tFactory.setURIResolver(resolver);
this.tFactory.setErrorListener(new SimpleTransformErrorListener(LogFactory.getLog(getClass())));
//Load the XML document so it reads the same way the rendering pipeline reads XML
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(this.getClass().getResourceAsStream("test.xml"));
final XMLStreamReader streamReader = new FixedXMLEventStreamReader(eventReader);
xmlSource = new StAXSource(streamReader);
xslTemplate = tFactory.newTemplates(new StreamSource(this.getClass().getResourceAsStream("test.xsl")));
expected = IOUtils.toString(this.getClass().getResourceAsStream("result.xml"));
}
use of javax.xml.stream.XMLStreamReader 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.XMLStreamReader in project uPortal by Jasig.
the class FragmentDefinitionImporter method fixStAXSource.
protected Source fixStAXSource(StAXSource staxSource) {
XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
if (xmlStreamReader != null) {
return staxSource;
}
final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
//Wrap the event reader in a stream reader to avoid a JDK bug
final XMLStreamReader streamReader;
try {
streamReader = new VersionCachingXMLEventStreamReader(xmlEventReader);
} catch (XMLStreamException e) {
throw new RuntimeException("Failed to create XMLStreamReader from XMLEventReader", e);
}
return new StAXSource(streamReader);
}
use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.
the class TestGetVersion method runTest.
protected void runTest() throws Throwable {
XMLInputFactory factory = staxImpl.newNormalizedXMLInputFactory();
XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<?xml version='1.0'?><root/>"));
assertEquals("1.0", reader.getVersion());
reader.next();
try {
reader.getVersion();
fail("Expected IllegalStateException");
} catch (IllegalStateException ex) {
// Expected
}
reader.close();
}
Aggregations