Example usage for javax.xml.transform TransformerException toString

List of usage examples for javax.xml.transform TransformerException toString

Introduction

In this page you can find the example usage for javax.xml.transform TransformerException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:Main.java

public static byte[] transform(Transformer transformer, Document useDocument) {
    byte[] returnData = new byte[0];
    try {/* ww  w .j a  v  a  2s.  c o  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(useDocument.getDocumentElement());
        transformer.transform(domSource, new StreamResult(baos));

        returnData = baos.toByteArray();

        baos.close();
    } catch (TransformerException te) {
        String err = new String(te.toString());
        returnData = err.getBytes();
        te.printStackTrace();
    } catch (IOException ie) {
        String err = new String(ie.toString());
        returnData = err.getBytes();
        ie.printStackTrace();
    }
    return returnData;
}

From source file:Main.java

public static String xmlToString(Document doc) {
    try {// w ww.  j  a v  a2 s . com
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (TransformerException ex) {
        System.err.println("Error: Cannot load xml transformer");
        System.err.println("Cause: " + ex.toString());
        System.exit(1);
        return null;
    }
}

From source file:facturas.PDF.InputHandler.java

/**
 * @see javax.xml.transform.ErrorListener#warning(javax.xml.transform.TransformerException)
 */// w  ww  . j  a  v  a2  s .c  om
public void warning(TransformerException exc) {
    log.warn(exc.toString());
}

From source file:facturas.PDF.InputHandler.java

/**
 * @see javax.xml.transform.ErrorListener#error(javax.xml.transform.TransformerException)
 *//*w  ww. j av  a  2s .co  m*/
public void error(TransformerException exc) {
    log.error(exc.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  .ja va 2  s. c  o 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.
 *//*from  www .ja v a 2 s. c  om*/
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 v  a  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.powertac.server.CompetitionSetupService.java

private String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*from  w  ww  .  ja  va 2s  .  c  om*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        log.error("nodeToString Transformer Exception " + te.toString());
    }
    return sw.toString();
}

From source file:dk.defxws.fedoragsearch.server.Config.java

private void checkStylesheet(String configPath, InputStream stylesheet) {
    if (logger.isDebugEnabled())
        logger.debug("checkStylesheet for " + configPath);
    Transformer transformer = null;
    try {// w w w  .  ja  v a2  s  .c om
        //MIH: Explicitly use Xalan Transformer Factory ////////////////////
        TransformerFactoryImpl tfactory = new TransformerFactoryImpl();
        ///////////////////////////////////////////////////////////////////
        //MIH: add hardcoded EscidocUriResolverto avoid more changes in method-signatures
        tfactory.setURIResolver(new de.escidoc.sb.gsearch.xslt.EscidocUriResolver());
        StreamSource xslt = new StreamSource(stylesheet);
        transformer = tfactory.newTransformer(xslt);
        String testSource = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<emptyTestDocumentRoot/>";
        StringReader sr = new StringReader(testSource);
        StreamResult destStream = new StreamResult(new StringWriter());
        try {
            transformer.transform(new StreamSource(sr), destStream);
        } catch (TransformerException e) {
            errors.append("\n*** Stylesheet " + configPath + " error:\n" + e.toString());
        }
    } catch (TransformerConfigurationException e) {
        errors.append("\n*** Stylesheet " + configPath + " error:\n" + e.toString());
    } catch (TransformerFactoryConfigurationError e) {
        errors.append("\n*** Stylesheet " + configPath + " error:\n" + e.toString());
    }
}

From source file:org.cytobank.acs.core.TableOfContents.java

/**
 * Returns a <code>String</code> containing the XML that represents this <code>TableOfContents</code>.
 *
 * @return XML that represents this <code>TableOfContents</code>
 * @throws InvalidIndexException If there is a problem with the <code>TableOfContents</code>
 *///from   w  w w .ja  va 2 s.com
public String toXml() throws InvalidIndexException {
    try {
        Source source = new DOMSource(tableOfContentsDoc);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        String xmlResult = stringWriter.getBuffer().toString();

        return xmlResult;
    } catch (TransformerException te) {
        throw new InvalidIndexException(te.toString());
    }
}