List of usage examples for javax.xml.transform TransformerConfigurationException toString
public String toString()
From source file:eionet.gdem.conversion.converters.ConvertStrategy.java
/** * Method transforms XML source using XSL stream. * @param in InputStream containing source XML. * @param xslStream InputStream containing XSL content. * @param out OutputStream for conversion result. * @throws GDEMException In case of unexpected XML or XSL errors. *///from w ww . j a v a2 s . co m protected void runXslTransformation(InputStream in, InputStream xslStream, OutputStream out) throws GDEMException { try { TransformerFactory tFactory = transform.getTransformerFactoryInstance(); TransformerErrorListener errors = new TransformerErrorListener(); tFactory.setErrorListener(errors); StreamSource transformerSource = new StreamSource(xslStream); if (getXslPath() != null) { transformerSource.setSystemId(getXslPath()); } Transformer transformer = tFactory.newTransformer(transformerSource); transformer.setErrorListener(errors); transformer.setParameter(DD_DOMAIN_PARAM, Properties.ddURL); setTransformerParameters(transformer); long l = System.currentTimeMillis(); transformer.transform(new StreamSource(in), new StreamResult(out)); if (LOGGER.isDebugEnabled()) { LOGGER.debug((new StringBuilder()).append("generate: transformation needed ") .append(System.currentTimeMillis() - l).append(" ms").toString()); } } catch (TransformerConfigurationException tce) { throw new GDEMException("Error transforming XML - incorrect stylesheet file: " + tce.toString(), tce); } catch (TransformerException tfe) { throw new GDEMException( "Error transforming XML - it's not probably well-formed xml file: " + tfe.toString(), tfe); } catch (Throwable th) { LOGGER.error("Error " + th.toString(), th); th.printStackTrace(System.out); throw new GDEMException("Error transforming XML: " + th.toString()); } }
From source file:eionet.gdem.conversion.converters.ConvertStrategy.java
/** * Method transforms XML source to PDF using XSL-FO stream. * @param in InputStream containing source XML. * @param xsl InputStream containing XSL-FO content. * @param out OutputStream for conversion result. * @throws GDEMException In case of unexpected XML or XSL errors. *//* w w w .ja va2 s . co m*/ protected void runFOPTransformation(InputStream in, InputStream xsl, OutputStream out) throws GDEMException { try { Driver driver = new Driver(); driver.setRenderer(Driver.RENDER_PDF); driver.setOutputStream(out); Result res = new SAXResult(driver.getContentHandler()); Source src = new StreamSource(in); TransformerFactory transformerFactory = transform.getTransformerFactoryInstance(); TransformerErrorListener errors = new TransformerErrorListener(); transformerFactory.setErrorListener(errors); StreamSource transformerSource = new StreamSource(xsl); if (getXslPath() != null) { transformerSource.setSystemId(getXslPath()); } Transformer transformer = transformerFactory.newTransformer(transformerSource); setTransformerParameters(transformer); transformer.setErrorListener(errors); long l = System.currentTimeMillis(); transformer.transform(src, res); if (LOGGER.isDebugEnabled()) { LOGGER.debug((new StringBuilder()).append("generate: transformation needed ") .append(System.currentTimeMillis() - l).append(" ms").toString()); } } catch (TransformerConfigurationException tce) { throw new GDEMException("Error transforming XML to PDF - incorrect stylesheet file: " + tce.toString(), tce); } catch (TransformerException tfe) { throw new GDEMException( "Error transforming XML to PDF - it's not probably well-formed xml file: " + tfe.toString(), tfe); } catch (Throwable e) { LOGGER.error("Error " + e.toString(), e); throw new GDEMException("Error transforming XML to PDF " + e.toString()); } }
From source file:SubmitResults.java
private File populateRequest(final Main parent, String formStatus, String filePath, HttpPost req, final String changeIdXSLT, ContentType ct, MultipartEntityBuilder entityBuilder, final String newIdent) { File ammendedFile = null;//from w w w .ja va 2 s . c om final File instanceFile = new File(filePath); if (formStatus != null) { System.out.println("Setting form status in header: " + formStatus); req.setHeader("form_status", formStatus); // smap add form_status header } else { System.out.println("Form Status null"); } if (newIdent != null) { // Transform the survey ID try { System.out.println("Transformaing Instance file: " + instanceFile); PipedInputStream in = new PipedInputStream(); final PipedOutputStream outStream = new PipedOutputStream(in); new Thread(new Runnable() { public void run() { try { InputStream xslStream = new ByteArrayInputStream(changeIdXSLT.getBytes("UTF-8")); Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(xslStream)); StreamSource source = new StreamSource(instanceFile); StreamResult out = new StreamResult(outStream); transformer.setParameter("surveyId", newIdent); transformer.transform(source, out); outStream.close(); } catch (TransformerConfigurationException e1) { parent.appendToStatus("Error changing ident: " + e1.toString()); } catch (TransformerFactoryConfigurationError e1) { parent.appendToStatus("Error changing ident: " + e1.toString()); } catch (TransformerException e) { parent.appendToStatus("Error changing ident: " + e.toString()); } catch (IOException e) { parent.appendToStatus("Error changing ident: " + e.toString()); } } }).start(); System.out.println("Saving stream to file"); ammendedFile = saveStreamTemp(in); } catch (Exception e) { parent.appendToStatus("Error changing ident: " + e.toString()); } } /* * Add submission file as file body, hence save to temporary file first */ if (newIdent == null) { ct = ContentType.create("text/xml"); entityBuilder.addBinaryBody("xml_submission_file", instanceFile, ct, instanceFile.getPath()); } else { FileBody fb = new FileBody(ammendedFile); entityBuilder.addPart("xml_submission_file", fb); } parent.appendToStatus("Instance file path: " + instanceFile.getPath()); /* * find all files referenced by the survey * Temporarily check to see if the parent directory is "uploadedSurveys". If it is * then we will need to scan the submission file to get the list of attachments to * send. * Alternatively this is a newly submitted survey stored in its own directory just as * surveys are stored on the phone. We can then just add all the surveys that are in * the same directory as the submission file. */ File[] allFiles = instanceFile.getParentFile().listFiles(); // add media files ignoring invisible files and the submission file List<File> files = new ArrayList<File>(); for (File f : allFiles) { String fileName = f.getName(); if (!fileName.startsWith(".") && !fileName.equals(instanceFile.getName())) { // ignore invisible files and instance xml file files.add(f); } } for (int j = 0; j < files.size(); j++) { File f = files.get(j); String fileName = f.getName(); ct = ContentType.create(getContentType(parent, fileName)); FileBody fba = new FileBody(f, ct, fileName); entityBuilder.addPart(fileName, fba); } req.setEntity(entityBuilder.build()); return ammendedFile; }
From source file:org.toobsframework.transformpipeline.domain.TransletTransformer.java
@SuppressWarnings("unchecked") protected void doTransform(String xslTranslet, StreamSource xmlSource, Map params, IXMLTransformerHelper transformerHelper, StreamResult xmlResult) throws XMLTransformerException { try {//ww w. j a v a 2 s. c om // Dont rely on the system property to get the right transformer TransformerFactory tFactory = new org.apache.xalan.xsltc.trax.TransformerFactoryImpl(); // set the URI Resolver for the transformer factory setFactoryResolver(tFactory); Source source = uriResolver.resolve(xslTranslet + ".xsl", ""); String tPkg = source.getSystemId().substring(0, source.getSystemId().lastIndexOf("/")) .replaceAll("/", ".").replaceAll("-", "_"); try { //tFactory.setAttribute("use-classpath", Boolean.TRUE); tFactory.setAttribute("auto-translet", Boolean.TRUE); tFactory.setAttribute("package-name", tPkg); } catch (IllegalArgumentException iae) { log.error("Error setting XSLTC specific attribute", iae); throw new XMLTransformerException(iae); } Transformer transformer = tFactory.newTransformer(source); // 2.2 Set character encoding for all transforms to UTF-8. transformer.setOutputProperty("encoding", "UTF-8"); transformer.setErrorListener(tFactory.getErrorListener()); // 2.5 Set Parameters necessary for transformation. if (params != null) { Iterator paramIt = params.entrySet().iterator(); while (paramIt.hasNext()) { Map.Entry thisParam = (Map.Entry) paramIt.next(); transformer.setParameter((String) thisParam.getKey(), (String) thisParam.getValue()); } } if (transformerHelper != null) { transformer.setParameter(TRANSFORMER_HELPER, transformerHelper); } // 3. Use the Transformer to transform an XML Source and send the // output to a Result object. transformer.transform(xmlSource, xmlResult); } catch (TransformerConfigurationException tce) { log.error(tce.toString(), tce); throw new XMLTransformerException(tce); } catch (TransformerException te) { log.error(te.toString(), te); throw new XMLTransformerException(te); } }