Example usage for javax.xml.transform Templates newTransformer

List of usage examples for javax.xml.transform Templates newTransformer

Introduction

In this page you can find the example usage for javax.xml.transform Templates newTransformer.

Prototype

Transformer newTransformer() throws TransformerConfigurationException;

Source Link

Document

Create a new transformation context for this Templates object.

Usage

From source file:servlet.PdfServlet2.java

/**
 * Processes requests for both HTTP/*from w  ww  . java  2s .  c  o  m*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    MyQuery mq = new MyQuery();

    ServletContext webApp = this.getServletContext();

    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();

        FopFactory fopFactory = FopFactory.newInstance();

        //Setup a buffer to obtain the content length
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        Templates templates = tFactory.newTemplates(new StreamSource(webApp.getRealPath(XSLT_PATH)));
        // Create a transformer
        Transformer transformer = templates.newTransformer();

        // Get concrete implementation
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        // Need a parser that support namespaces
        dFactory.setNamespaceAware(true);
        // Create the parser
        DocumentBuilder parser = dFactory.newDocumentBuilder();
        // Parse the XML document
        InputSource is = new InputSource(new StringReader(mq.getPayments()));
        // Parse the XML document

        Document doc = parser.parse(is);

        //Setup FOP
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

        //Make sure the XSL transformation's result is piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        //Setup input
        Source src = new DOMSource(doc);

        //Start the transformation and rendering process
        transformer.transform(src, res);

        //Prepare response
        response.setContentType("application/pdf");
        response.setContentLength(out.size());

        //Send content to Browser
        response.getOutputStream().write(out.toByteArray());
        response.getOutputStream().flush();
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}

From source file:Servlet.ServletFOP.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  w  w .  j a v a  2s.c o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ServletContext webApp = this.getServletContext();

    try {

        TransformerFactory tFactory = TransformerFactory.newInstance();

        FopFactory fopFactory = FopFactory.newInstance();

        //Setup a buffer to obtain the content length
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        Templates templates = tFactory.newTemplates(new StreamSource(webApp.getRealPath(XSLT_PATH)));
        // Create a transformer
        Transformer transformer = templates.newTransformer();

        // Get concrete implementation
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        // Need a parser that support namespaces
        dFactory.setNamespaceAware(true);
        // Create the parser
        DocumentBuilder parser = dFactory.newDocumentBuilder();

        InputSource is = new InputSource(new StringReader(new Query().chartFOP()));
        // Parse the XML document
        Document doc = parser.parse(is);

        //Setup FOP
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

        //Make sure the XSL transformation's result is piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        //Setup input
        Source src = new DOMSource(doc);

        //Start the transformation and rendering process
        transformer.transform(src, res);

        //Prepare response
        response.setContentType("application/pdf");
        response.setContentLength(out.size());

        //Send content to Browser
        response.getOutputStream().write(out.toByteArray());
        response.getOutputStream().flush();
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}