Example usage for javax.xml.transform TransformerException printStackTrace

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

Introduction

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

Prototype

@Override
public void printStackTrace(java.io.PrintWriter s) 

Source Link

Document

Print the the trace of methods from where the error originated.

Usage

From source file:com.iflytek.spider.util.DomUtil.java

/**
 * save dom into ouputstream/*from w  w  w  .  j a va  2 s . c o m*/
 * 
 * @param os
 * @param e
 */
public static void saveDom(OutputStream os, Element e) {

    DOMSource source = new DOMSource(e);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = transFactory.newTransformer();
        transformer.setOutputProperty("indent", "yes");
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
        os.flush();
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace(LogUtil.getWarnStream(LOG));
    } catch (IOException e1) {
        e1.printStackTrace(LogUtil.getWarnStream(LOG));
    } catch (TransformerConfigurationException e2) {
        e2.printStackTrace(LogUtil.getWarnStream(LOG));
    } catch (TransformerException ex) {
        ex.printStackTrace(LogUtil.getWarnStream(LOG));
    }
}

From source file:Main.java

/**
 * Print a DOM tree to an output stream or if there is an exception while doing so, print the
 * stack trace./* w w w . j av a 2  s  .com*/
 *
 * @param dom
 * @param os
 */
public static void printDom(Node dom, OutputStream os) {
    Transformer trans;
    PrintWriter w = new PrintWriter(os);
    try {
        TransformerFactory fact = TransformerFactory.newInstance();
        trans = fact.newTransformer();
        trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os)));
    } catch (TransformerException e) {
        w.println("An error ocurred while transforming the given DOM:");
        e.printStackTrace(w);
    }
}

From source file:org.openmrs.module.mksreports.web.controller.MKSReportsReportsManageController.java

/**
 * Receives requests to run a patient summary.
 * /*from www .  ja v a  2  s. com*/
 * @param patientId the id of patient whose summary you wish to view
 * @param summaryId the id of the patientsummary you wish to view
 */
@RequestMapping(value = "/module/mksreports/renderSummary")
public void renderSummary(ModelMap model, HttpServletRequest request, HttpServletResponse response,
        @RequestParam("patientId") Integer patientId,
        @RequestParam(value = "download", required = false) boolean download,
        @RequestParam(value = "print", required = false) boolean print) throws IOException {
    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");

    FopFactory fopFactory = FopFactory.newInstance();

    try {
        PatientSummaryService pss = Context.getService(PatientSummaryService.class);

        ReportService rs = Context.getService(ReportService.class);
        ReportDesign psrd = null;
        for (ReportDesign rd : rs.getAllReportDesigns(false)) {
            if ("mekomPatientSummary.xml_".equals(rd.getName())) {
                psrd = rd;
            }
        }

        PatientSummaryTemplate ps = pss.getPatientSummaryTemplate(psrd.getId());

        HashMap<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("patientSummaryMode", print ? "print" : "download");
        PatientSummaryResult result = pss.evaluatePatientSummaryTemplate(ps, patientId, parameters);
        if (result.getErrorDetails() != null) {
            result.getErrorDetails().printStackTrace(response.getWriter());
        } else {

            /*We shouldn't be getting this from a file! We'll use 
             * StreamSource src = new StreamSource(new ByteArrayInputStream(result.getRawContents())); instead 
             * to get the content from the datasets rows. This should be done once we finish building a suitable
             * xsl style (...this file will replace sampleStylesheet.xsl in the api's resource folder) 
             * that can correctly work with the xml containing the datasets*/
            StreamSource source = new StreamSource(
                    OpenmrsClassLoader.getInstance().getResourceAsStream("sample.xml"));
            StreamSource transformSource = new StreamSource(
                    OpenmrsClassLoader.getInstance().getResourceAsStream("sampleStylesheet.xsl"));
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();

            Transformer xslfoTransformer;
            try {
                xslfoTransformer = getTransformer(transformSource);
                Fop fop;
                try {
                    fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
                    Result res = new SAXResult(fop.getDefaultHandler());
                    try {
                        xslfoTransformer.transform(source, res);
                        byte[] pdfBytes = outStream.toByteArray();
                        response.setContentLength(pdfBytes.length);
                        response.setContentType("application/pdf");
                        response.addHeader("Content-Disposition", "attachment;filename=patientHistory.pdf");
                        response.getOutputStream().write(pdfBytes);
                        response.getOutputStream().flush();
                    } catch (TransformerException e) {
                        throw e;
                    }
                } catch (FOPException e) {
                    throw e;
                }
            } catch (TransformerConfigurationException e) {
                throw e;
            } catch (TransformerFactoryConfigurationError e) {
                throw e;
            }

        }
    } catch (Exception e) {
        e.printStackTrace(response.getWriter());
    }
}