Example usage for javax.xml.transform Templates getOutputProperties

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

Introduction

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

Prototype

Properties getOutputProperties();

Source Link

Document

Get the properties corresponding to the effective xsl:output element.

Usage

From source file:Examples.java

/**
 * Show how to override output properties.
 *//*from  w w  w  . ja  v a2 s . co  m*/
public static void exampleOutputProperties(String sourceID, String xslID)
        throws TransformerException, TransformerConfigurationException {

    TransformerFactory tfactory = TransformerFactory.newInstance();
    Templates templates = tfactory.newTemplates(new StreamSource(xslID));
    Properties oprops = templates.getOutputProperties();

    oprops.put(OutputKeys.INDENT, "yes");

    Transformer transformer = templates.newTransformer();

    transformer.setOutputProperties(oprops);
    transformer.transform(new StreamSource(sourceID), new StreamResult(new OutputStreamWriter(System.out)));
}

From source file:org.fcrepo.localservices.saxon.SaxonServlet.java

/**
 * Apply stylesheet to source document/*from ww w. j a  v a 2 s  . c  o m*/
 */
private void apply(String style, String source, HttpServletRequest req, HttpServletResponse res)
        throws Exception {

    // Validate parameters
    if (style == null) {
        throw new TransformerException("No style parameter supplied");
    }
    if (source == null) {
        throw new TransformerException("No source parameter supplied");
    }

    InputStream sourceStream = null;
    try {
        // Load the stylesheet (adding to cache if necessary)
        Templates pss = tryCache(style);
        Transformer transformer = pss.newTransformer();

        Enumeration<?> p = req.getParameterNames();
        while (p.hasMoreElements()) {
            String name = (String) p.nextElement();
            if (!(name.equals("style") || name.equals("source"))) {
                String value = req.getParameter(name);
                transformer.setParameter(name, new StringValue(value));
            }
        }

        // Start loading the document to be transformed
        sourceStream = getInputStream(source);

        // Set the appropriate output mime type
        String mime = pss.getOutputProperties().getProperty(OutputKeys.MEDIA_TYPE);
        if (mime == null) {
            res.setContentType("text/html");
        } else {
            res.setContentType(mime);
        }

        // Transform
        StreamSource ss = new StreamSource(sourceStream);
        ss.setSystemId(source);
        transformer.transform(ss, new StreamResult(res.getOutputStream()));

    } finally {
        if (sourceStream != null) {
            try {
                sourceStream.close();
            } catch (Exception e) {
            }
        }
    }

}

From source file:org.apache.struts2.views.xslt.XSLTResult.java

public void execute(ActionInvocation invocation) throws Exception {
    long startTime = System.currentTimeMillis();
    String location = getStylesheetLocation();

    if (location == null) {
        throw new IllegalArgumentException("Parameter 'stylesheetLocation' cannot be null!");
    }//w w w  .  j  av a2 s.  c  om

    if (parse) {
        ValueStack stack = ActionContext.getContext().getValueStack();
        location = TextParseUtil.translateVariables(location, stack);
    }

    try {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setStatus(status);
        response.setCharacterEncoding(encoding);
        PrintWriter writer = response.getWriter();

        // Create a transformer for the stylesheet.
        Templates templates = null;
        Transformer transformer;
        if (location != null) {
            templates = getTemplates(location);
            transformer = templates.newTransformer();
        } else {
            transformer = TransformerFactory.newInstance().newTransformer();
        }

        transformer.setURIResolver(getURIResolver());
        transformer.setErrorListener(buildErrorListener());

        String mimeType;
        if (templates == null) {
            mimeType = "text/xml"; // no stylesheet, raw xml
        } else {
            mimeType = templates.getOutputProperties().getProperty(OutputKeys.MEDIA_TYPE);
        }

        if (mimeType == null) {
            // guess (this is a servlet, so text/html might be the best guess)
            mimeType = "text/html";
        }

        response.setContentType(mimeType);

        Object result = invocation.getAction();
        if (exposedValue != null) {
            ValueStack stack = invocation.getStack();
            result = stack.findValue(exposedValue);
        }

        Source xmlSource = getDOMSourceForStack(result);

        // Transform the source XML to System.out.
        LOG.debug("xmlSource = {}", xmlSource);
        transformer.transform(xmlSource, new StreamResult(writer));

        writer.flush(); // ...and flush...

        LOG.debug("Time: {}ms", (System.currentTimeMillis() - startTime));

    } catch (Exception e) {
        LOG.error("Unable to render XSLT Template, '{}'", location, e);
        throw e;
    }
}

From source file:org.mycore.common.content.transformer.MCRXSLTransformer.java

private String getOutputProperty(String propertyName, String defaultValue)
        throws TransformerException, SAXException {
    checkTemplateUptodate();/*from   w  ww. j a  v  a2 s . c  om*/
    Templates lastTemplate = templates[templates.length - 1];
    Properties outputProperties = lastTemplate.getOutputProperties();
    if (outputProperties == null) {
        return defaultValue;
    }
    String value = outputProperties.getProperty(propertyName);
    if (value == null) {
        return defaultValue;
    }
    return value;
}