use of javax.xml.transform.sax.SAXResult in project series-rest-api by 52North.
the class PDFReportGenerator method encodeAndWriteTo.
@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
try {
generateOutput(data);
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(document.newInputStream());
FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
Fop fop = fopFactory.newFop(mimeType, stream);
//FopFactory fopFactory = FopFactory.newInstance(cfg);
//Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
//FopFactory fopFactory = fopFactoryBuilder.build();
//Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
// Create PDF via XSLT transformation
TransformerFactory transFact = TransformerFactory.newInstance();
StreamSource transformationRule = getTransforamtionRule();
Transformer transformer = transFact.newTransformer(transformationRule);
Source source = new StreamSource(document.newInputStream());
Result result = new SAXResult(fop.getDefaultHandler());
if (LOGGER.isDebugEnabled()) {
try {
File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
StreamResult debugResult = new StreamResult(tempFile);
transformer.transform(source, debugResult);
String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
} catch (IOException | TransformerException | XmlException e) {
LOGGER.error("Could not debug XSL result output!", e);
}
}
// XXX debug, diagram is not embedded
transformer.transform(source, result);
} catch (FOPException e) {
throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
} catch (SAXException | ConfigurationException | IOException e) {
throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
} catch (TransformerConfigurationException e) {
throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
} catch (TransformerException e) {
throw new IoParseException("Could not generate PDF report!", e);
}
}
use of javax.xml.transform.sax.SAXResult in project jackrabbit by apache.
the class ClientSession method exportDocumentView.
/**
* Exports the XML document view of the specified repository location
* to the given XML content handler. This method first requests the
* raw XML data from the remote session, and then uses an identity
* transformation to feed the data to the given XML content handler.
* Possible IO and transformer exceptions are thrown as SAXExceptions.
*
* {@inheritDoc}
*/
public void exportDocumentView(String path, ContentHandler handler, boolean binaryAsLink, boolean noRecurse) throws SAXException, RepositoryException {
try {
byte[] xml = remote.exportDocumentView(path, binaryAsLink, noRecurse);
Source source = new StreamSource(new ByteArrayInputStream(xml));
Result result = new SAXResult(handler);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
} catch (RemoteException ex) {
throw new RemoteRepositoryException(ex);
} catch (IOException ex) {
throw new SAXException(ex);
} catch (TransformerConfigurationException ex) {
throw new SAXException(ex);
} catch (TransformerException ex) {
throw new SAXException(ex);
}
}
use of javax.xml.transform.sax.SAXResult in project jena by apache.
the class DOM2Model method load.
/**
* Parse a DOM Node with the RDF/XML parser, loading the triples into the
* associated Model. Known not to work with Java 1.4.1.
*
* @param document
*/
public void load(Node document) {
Source input = new DOMSource(document);
// Make a SAXResult object using this handler
SAXResult output = new SAXResult(this);
output.setLexicalHandler(this);
// Run transform
TransformerFactory xformFactory = TransformerFactory.newInstance();
try {
Transformer idTransform = xformFactory.newTransformer();
idTransform.transform(input, output);
} catch (FatalParsingErrorException e) {
// Old code ignored this,
// given difficult bug report, don't be silent.
logger.error("Unexpected exception in DOM2Model", e);
} catch (RuntimeException rte) {
throw rte;
} catch (Exception nrte) {
throw new JenaException(nrte);
} finally {
close();
}
}
use of javax.xml.transform.sax.SAXResult 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.transform.sax.SAXResult in project tika by apache.
the class MimeTypesReader method read.
public void read(Document document) throws MimeTypeException {
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(document), new SAXResult(this));
} catch (TransformerException e) {
throw new MimeTypeException("Failed to parse type registry", e);
}
}
Aggregations