use of javax.xml.transform.Transformer in project jangaroo-tools by CoreMedia.
the class PomConverter method writePom.
/**
* Serializes the given DOM document into a POM file within the given directory.
*/
private static void writePom(Document document, File projectBaseDir) throws MojoExecutionException {
try {
File pomFile = new File(projectBaseDir, "pom.xml");
// keep trailing whitespace because it's not reproduced by the transformer and we want to keep the diff small
String pom = readFileToString(pomFile);
String trailingWhitespace = pom.substring(pom.lastIndexOf('>') + 1);
PrintWriter pomWriter = new PrintWriter(pomFile);
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
// the transformer does not reproduce the new line after the XML declaration, so we do it on our own
// see https://bugs.openjdk.java.net/browse/JDK-7150637
transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
if (document.getXmlEncoding() != null) {
pomWriter.print("<?xml version=\"");
pomWriter.print(document.getXmlVersion());
pomWriter.print("\" encoding=\"");
pomWriter.print(document.getXmlEncoding());
pomWriter.println("\"?>");
}
transformer.transform(new DOMSource(document), new StreamResult(pomWriter));
pomWriter.write(trailingWhitespace);
} finally {
pomWriter.close();
}
} catch (IOException e) {
throw new MojoExecutionException("error while generating modified POM", e);
} catch (TransformerException e) {
throw new MojoExecutionException("error while generating modified POM", e);
}
}
use of javax.xml.transform.Transformer 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.Transformer in project OpenClinica by OpenClinica.
the class XalanTransformJob method executeInternal.
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
// need to generate a Locale so that user beans and other things will
// generate normally
// TODO make dynamic?
Locale locale = new Locale("en-US");
ResourceBundleProvider.updateLocale(locale);
ResourceBundle pageMessages = ResourceBundleProvider.getPageMessagesBundle();
JobDataMap dataMap = context.getMergedJobDataMap();
// get the file information from the job
String alertEmail = dataMap.getString(EMAIL);
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
// Use the TransformerFactory to instantiate a Transformer that will work with
// the stylesheet you specify. This method call also processes the stylesheet
// into a compiled Templates object.
java.io.InputStream in = new java.io.FileInputStream(dataMap.getString(XSL_FILE_PATH));
// tFactory.setAttribute("use-classpath", Boolean.TRUE);
// tFactory.setErrorListener(new ListingErrorHandler());
Transformer transformer = tFactory.newTransformer(new StreamSource(in));
// Use the Transformer to apply the associated Templates object to an XML document
// (foo.xml) and write the output to a file (foo.out).
// System.out.println("--> job starting: ");
final long start = System.currentTimeMillis();
transformer.transform(new StreamSource(dataMap.getString(XML_FILE_PATH)), new StreamResult(new FileOutputStream(dataMap.getString(SQL_FILE_PATH))));
final long done = System.currentTimeMillis() - start;
// System.out.println("--> job completed in " + done + " ms");
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of javax.xml.transform.Transformer in project OpenClinica by OpenClinica.
the class OpenRosaServices method getUserXml.
private String getUserXml(ServletContext context) throws Exception {
HashMap<String, String> value = getSubjectContextCacheValue(context);
String studySubjectOid = value.get("studySubjectOID");
StudySubject ssBean = ssDao.findByOcOID(studySubjectOid);
StudyBean study = getStudy(ssBean.getStudy().getOc_oid());
StudyBean parentStudy = getParentStudy(ssBean.getStudy().getOc_oid());
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element root = doc.createElement("root");
doc.appendChild(root);
List<UserAccount> users = userAccountDao.findNonRootNonParticipateUsersByStudyId(study.getId(), parentStudy.getId());
for (UserAccount userAccount : users) {
Element item = doc.createElement("item");
Element userName = doc.createElement("user_name");
userName.appendChild(doc.createTextNode(userAccount.getUserName()));
Element firstName = doc.createElement("first_name");
firstName.appendChild(doc.createTextNode(userAccount.getFirstName()));
Element lastName = doc.createElement("last_name");
lastName.appendChild(doc.createTextNode(userAccount.getLastName()));
item.appendChild(userName);
item.appendChild(firstName);
item.appendChild(lastName);
root.appendChild(item);
}
DOMSource dom = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(dom, result);
String userXml = writer.toString();
return userXml;
}
use of javax.xml.transform.Transformer in project crawler4j by yasserg.
the class BinaryParseData method getTransformerHandler.
/**
* Returns a transformer handler that serializes incoming SAX events to
* XHTML or HTML (depending the given method) using the given output encoding.
*
* @param encoding output encoding, or <code>null</code> for the platform default
*/
private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException {
TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
Transformer transformer = transformerHandler.getTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, method);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
if (encoding != null) {
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
}
transformerHandler.setResult(new StreamResult(new PrintStream(out)));
return transformerHandler;
}
Aggregations