use of javax.xml.transform.Transformer 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.Transformer in project tdi-studio-se by Talend.
the class FeaturesModel method internalGetContent.
private InputStream internalGetContent() throws Exception {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element features = document.createElement("features");
features.setAttribute("xmlns", "http://karaf.apache.org/xmlns/features/v1.0.0");
features.setAttribute("name", getArtifactId());
document.appendChild(features);
Element feature = document.createElement("feature");
feature.setAttribute("name", getArtifactId());
feature.setAttribute("version", getVersion());
features.appendChild(feature);
// add sub features
for (FeatureModel fm : subFeatures) {
Element subFeature = document.createElement("feature");
if (null != fm.getVersion() && !fm.getVersion().isEmpty()) {
subFeature.setAttribute("version", fm.getVersion());
}
subFeature.appendChild(document.createTextNode(fm.getArtifactId()));
feature.appendChild(subFeature);
}
// add sub bundles
for (BundleModel bm : subBundles) {
Element bundle = document.createElement("bundle");
bundle.appendChild(document.createTextNode(toBundleString(bm)));
feature.appendChild(bundle);
}
if (null == contexts || contexts.isEmpty()) {
// add config
Element config = document.createElement("config");
config.setAttribute("name", configName);
StringBuilder sb = new StringBuilder("talendcontext=\"");
for (int i = 0; i < contextList.length; i++) {
if (i != 0) {
sb.append(',');
}
sb.append(contextList[i]);
}
sb.append('"');
config.appendChild(document.createTextNode(sb.toString()));
feature.appendChild(config);
} else {
// add contexts config
for (Map.Entry<String, Map<String, String>> context : contexts.entrySet()) {
Element config = document.createElement("config");
config.setAttribute("name", name + ".talendcontext." + context.getKey());
StringBuilder sb = new StringBuilder("\n");
for (Map.Entry<String, String> property : context.getValue().entrySet()) {
sb.append(property.getKey());
sb.append('=');
sb.append(property.getValue());
sb.append('\n');
}
config.appendChild(document.createTextNode(sb.toString()));
feature.appendChild(config);
}
}
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
ByteArrayOutputStream os = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(os));
return new ByteArrayInputStream(os.toByteArray());
}
use of javax.xml.transform.Transformer in project voltdb by VoltDB.
the class JDBCSQLXML method init.
/**
* Initializes this object's SQLXML value from the given Source
* object. <p>
*
* @param source the Source representing the SQLXML value
* @throws SQLException if the argument does not represent a
* valid SQLXML value
*/
protected void init(Source source) throws SQLException {
if (source == null) {
throw Util.nullArgument("source");
}
Transformer transformer = JDBCSQLXML.getIdentityTransformer();
StreamResult result = new StreamResult();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos;
try {
gzos = new GZIPOutputStream(baos);
} catch (IOException ex) {
throw Exceptions.transformFailed(ex);
}
result.setOutputStream(gzos);
try {
transformer.transform(source, result);
} catch (TransformerException ex) {
throw Exceptions.transformFailed(ex);
}
try {
gzos.close();
} catch (IOException ex) {
throw Exceptions.transformFailed(ex);
}
byte[] data = baos.toByteArray();
setGZipData(data);
setReadable(true);
setWritable(false);
}
use of javax.xml.transform.Transformer in project voltdb by VoltDB.
the class JDBCSQLXML method createDOMSource.
/**
* Retrieves a new DOMSource for reading the XML value designated by this
* SQLXML instance. <p>
*
* @param sourceClass The class of the source
* @throws java.sql.SQLException if there is an error processing the XML
* value or if the given <tt>sourceClass</tt> is not supported.
* @return a new DOMSource for reading the XML value designated by this
* SQLXML instance
*/
@SuppressWarnings("unchecked")
protected <T extends Source> T createDOMSource(Class<T> sourceClass) throws SQLException {
DOMSource source = null;
try {
source = (sourceClass == null) ? new DOMSource() : (DOMSource) sourceClass.newInstance();
} catch (SecurityException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (IllegalAccessException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (InstantiationException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (ClassCastException ex) {
throw Exceptions.sourceInstantiation(ex);
}
Transformer transformer = JDBCSQLXML.getIdentityTransformer();
InputStream inputStream = this.getBinaryStreamImpl();
StreamSource streamSource = new StreamSource();
DOMResult domResult = new DOMResult();
streamSource.setInputStream(inputStream);
try {
transformer.transform(streamSource, domResult);
} catch (TransformerException ex) {
throw Exceptions.transformFailed(ex);
}
source.setNode(domResult.getNode());
return (T) source;
}
use of javax.xml.transform.Transformer 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);
}
}
Aggregations