use of javax.xml.transform.sax.SAXResult in project OpenAM by OpenRock.
the class MarshallerImpl method marshal.
public void marshal(Object obj, Result result) throws JAXBException {
//XMLSerializable so = Util.toXMLSerializable(obj);
XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
if (so == null)
throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
if (result instanceof SAXResult) {
write(so, ((SAXResult) result).getHandler());
return;
}
if (result instanceof DOMResult) {
Node node = ((DOMResult) result).getNode();
if (node == null) {
try {
DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
Document doc = db.newDocument();
((DOMResult) result).setNode(doc);
write(so, new SAX2DOMEx(doc));
} catch (ParserConfigurationException pce) {
throw new JAXBAssertionError(pce);
}
} else {
write(so, new SAX2DOMEx(node));
}
return;
}
if (result instanceof StreamResult) {
StreamResult sr = (StreamResult) result;
XMLWriter w = null;
if (sr.getWriter() != null)
w = createWriter(sr.getWriter());
else if (sr.getOutputStream() != null)
w = createWriter(sr.getOutputStream());
else if (sr.getSystemId() != null) {
String fileURL = sr.getSystemId();
if (fileURL.startsWith("file:///")) {
if (fileURL.substring(8).indexOf(":") > 0)
fileURL = fileURL.substring(8);
else
fileURL = fileURL.substring(7);
}
try {
w = createWriter(new FileOutputStream(fileURL));
} catch (IOException e) {
throw new MarshalException(e);
}
}
if (w == null)
throw new IllegalArgumentException();
write(so, w);
return;
}
// unsupported parameter type
throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
use of javax.xml.transform.sax.SAXResult in project opennms by OpenNMS.
the class OnmsPdfViewResolver method resolveView.
@Override
public void resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception {
InputStream is = new ByteArrayInputStream(((String) viewData).getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream out = new ByteArrayOutputStream();
FopFactory fopFactory = FopFactory.newInstance();
fopFactory.setStrictValidation(false);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory tfact = TransformerFactory.newInstance();
Transformer transformer = tfact.newTransformer();
Source src = new StreamSource(is);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
byte[] contents = out.toByteArray();
response.setContentLength(contents.length);
response.getOutputStream().write(contents);
}
use of javax.xml.transform.sax.SAXResult in project pcgen by PCGen.
the class FopTask method run.
/**
* Run the FO to PDF/AWT conversion. This automatically closes any provided OutputStream for
* this FopTask.
*/
@Override
public void run() {
try (OutputStream out = outputStream) {
userAgent.setProducer("PC Gen Character Generator");
userAgent.setAuthor(System.getProperty("user.name"));
userAgent.setCreationDate(new Date());
userAgent.getEventBroadcaster().addEventListener(new FOPEventListener());
String mimeType;
if (renderer != null) {
userAgent.setKeywords("PCGEN FOP PREVIEW");
mimeType = MimeConstants.MIME_FOP_AWT_PREVIEW;
} else {
userAgent.setKeywords("PCGEN FOP PDF");
mimeType = MimeConstants.MIME_PDF;
}
Fop fop;
if (out != null) {
fop = FOP_FACTORY.newFop(mimeType, userAgent, out);
} else {
fop = FOP_FACTORY.newFop(mimeType, userAgent);
}
Transformer transformer;
if (xsltSource != null) {
transformer = TRANS_FACTORY.newTransformer(xsltSource);
} else {
// identity transformer
transformer = TRANS_FACTORY.newTransformer();
}
transformer.setErrorListener(new FOPErrorListener());
transformer.transform(inputSource, new SAXResult(fop.getDefaultHandler()));
} catch (TransformerException | FOPException | IOException e) {
errorBuilder.append(e.getMessage()).append(Constants.LINE_SEPARATOR);
Logging.errorPrint("Exception in FopTask:run", e);
} catch (RuntimeException ex) {
errorBuilder.append(ex.getMessage()).append(Constants.LINE_SEPARATOR);
Logging.errorPrint("Unexpected exception in FopTask:run: ", ex);
}
}
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);
}
}
Aggregations