Example usage for javax.xml.transform Transformer setParameter

List of usage examples for javax.xml.transform Transformer setParameter

Introduction

In this page you can find the example usage for javax.xml.transform Transformer setParameter.

Prototype

public abstract void setParameter(String name, Object value);

Source Link

Document

Add a parameter for the transformation.

Usage

From source file:Main.java

public static Transformer setStdTransParamsProps(final Transformer renderer, final Properties params) {
    if (params != null) {
        for (Enumeration<Object> e = params.keys(); e.hasMoreElements();) {

            String localkey = (String) e.nextElement();
            String val = params.getProperty(localkey);
            renderer.setParameter(localkey, val);
        }//w ww. j av a  2  s . c o  m
    }
    return renderer;
}

From source file:io.selendroid.server.inspector.TreeUtil.java

public static String getXMLSource(JSONObject source) {
    Document document = JsonXmlUtil.buildXmlDocument(source);

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from ww  w  . j  a  va  2s.  c  o  m*/
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new SelendroidException(e);
    }

    transformer.setParameter("encoding", "UTF-8");

    DOMSource domSource = new DOMSource(document);
    Writer outWriter = new StringWriter();

    StreamResult result = new StreamResult(outWriter);
    try {
        transformer.transform(domSource, result);
    } catch (TransformerException e) {
        throw new SelendroidException(e);
    }

    return outWriter.toString();
}

From source file:Main.java

public static void style(Reader xsl, Reader xml, Writer out, Map params) throws TransformerException {
    Source xmlSource = new javax.xml.transform.stream.StreamSource(xml);
    TransformerFactory factory = TransformerFactory.newInstance();
    Source xslSource = new javax.xml.transform.stream.StreamSource(xsl);
    Transformer transformer;
    transformer = factory.newTransformer(xslSource);
    if (params != null && !params.isEmpty()) {
        Iterator entries = params.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            transformer.setParameter((String) entry.getKey(), entry.getValue());
        }/* w ww . j ava 2s.  com*/
    }
    StreamResult result = new StreamResult(out);
    transformer.transform(xmlSource, result);
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 * The resulting XML document is converted to a string after transformation.
 * /*from   w  ww .jav a 2s  .  c o  m*/
 * @param xmlDocument
 *            the xml document to be transformed
 * @param parameters
 *            the hashtable with the parameters to be passed to the
 *            stylesheet
 * @param xsltFilename
 *            the filename of the stylesheet
 * @return the transformed xml document as a string
 * @throws Exception
 */
public static String transformDocumentAsString(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws Exception {

    // Generate a Transformer.
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename));

    // set transformation parameters
    if (parameters != null) {
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            transformer.setParameter(param.getKey(), param.getValue());
        }
    }

    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);

    // Perform the transformation.
    transformer.transform(new DOMSource(xmlDocument), streamResult);
    // Now you can get the output Node from the DOMResult.
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 * //from  ww w.j  a v a2s . c  om
 * @param xmlDocument
 *            the xml document to be transformed
 * @param parameters
 *            the hashtable with the parameters to be passed to the
 *            stylesheet
 * @param xsltFilename
 *            the filename of the stylesheet
 * @return the transformed xml document
 * @throws Exception
 */
public static Document transformDocument(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws Exception {

    // Generate a Transformer.
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename));

    // set transformation parameters
    if (parameters != null) {
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            transformer.setParameter(param.getKey(), param.getValue());
        }

    }

    // Create an empty DOMResult object for the output.
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    Document dstDocument = dBuilder.newDocument();

    DOMResult domResult = new DOMResult(dstDocument);

    // Perform the transformation.
    transformer.transform(new DOMSource(xmlDocument), domResult);
    // Now you can get the output Node from the DOMResult.
    return dstDocument;
}

From source file:Main.java

public static String applyXSLT(Source xml, URL xsltFilename, Map params) {
    try {/*from  w  w  w . j a  v  a2s .c  o  m*/
        TransformerFactory f = TransformerFactory.newInstance();
        StreamSource xsltSrc = new StreamSource(xsltFilename.openConnection().getInputStream());
        StringWriter out = new StringWriter();
        StreamResult res = new StreamResult(out);
        Transformer t = f.newTransformer(xsltSrc);

        if (params != null) {
            Iterator i = params.keySet().iterator();
            while (i.hasNext()) {
                Object obj = i.next();
                t.setParameter(obj.toString(), params.get(obj));
            }
        }
        t.transform(xml, res);

        return out.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Applies an XSLT.//  w w w  .j a v  a  2 s.c o  m
 *
 * @param xml          The soucrce to translate.
 * @param xsltFilename The filename of the xsl file to use.
 * @param params       A map of parameters to pass to the transformation.
 *                     if null is passed none are used.
 *
 * @return The string representing of the result of the transformation.
 */
public static String applyXSLT(Source xml, String xsltFilename, Map params) {
    try {
        String fullPath = xsltFilename;
        File xslt = new File(fullPath);
        TransformerFactory f = TransformerFactory.newInstance();
        StreamSource xsltSrc = new StreamSource(xslt);
        StringWriter out = new StringWriter();
        StreamResult res = new StreamResult(out);
        Transformer t = f.newTransformer(xsltSrc);

        if (params != null) {
            Iterator i = params.keySet().iterator();
            while (i.hasNext()) {
                Object obj = i.next();
                t.setParameter(obj.toString(), params.get(obj));
            }
        }
        t.transform(xml, res);

        return out.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Node applyXslToDocument2(Source xslt, Source doc, URIResolver resolver,
        Properties transformerProperties, HashMap<String, String> params, String transformerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException,
        NoSuchMethodException, TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = null;
    if (transformerClassName == null)
        transformerFactory = TransformerFactory.newInstance();
    else {/*from  ww  w .  j  a  v a 2  s  .c  om*/
        Class transformerClass = Class.forName(transformerClassName);
        Constructor defaultConstructor = transformerClass.getConstructor(null);
        transformerFactory = (TransformerFactory) transformerClass.newInstance();
    }

    if (resolver != null)
        transformerFactory.setURIResolver(resolver);

    Transformer transformer = transformerFactory.newTransformer(xslt);
    if (transformerFactory != null)
        transformer.setOutputProperties(transformerProperties);

    if (params != null) {
        for (Map.Entry<String, String> cursor : params.entrySet()) {
            transformer.setParameter(cursor.getKey(), cursor.getValue());
        }
    }

    DOMResult result = new DOMResult();
    transformer.transform(doc, result);

    return (result.getNode());
}

From source file:Main.java

/**
 * All the standard parameters must be set in the properties file
 * //  w  ww  .  jav  a2s .c o  m
 * @param renderer
 * @param params
 * @return
 */
public static Transformer setStdTransParamsProps(Transformer renderer, Properties params) {

    // log.severe("setStdTransParamsProps params = "+params);

    if (params != null) {
        for (Enumeration e = params.keys(); e.hasMoreElements();) {

            String localkey = (String) e.nextElement();
            String val = params.getProperty(localkey);
            renderer.setParameter(localkey, val);
        }
    }
    return renderer;
}

From source file:Main.java

public static String applyXslToDocument(Source xslt, Source doc, URIResolver resolver,
        Properties transformerProperties, HashMap<String, String> params, String transformerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException,
        NoSuchMethodException, TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = null;
    if (transformerClassName == null)
        transformerFactory = TransformerFactory.newInstance();
    else {/*  ww w  .  j a  v  a 2s.  c o  m*/
        Class transformerClass = Class.forName(transformerClassName);
        Constructor defaultConstructor = transformerClass.getConstructor(null);
        transformerFactory = (TransformerFactory) transformerClass.newInstance();
    }

    if (resolver != null)
        transformerFactory.setURIResolver(resolver);

    Transformer transformer = transformerFactory.newTransformer(xslt);
    if (transformerFactory != null)
        transformer.setOutputProperties(transformerProperties);

    if (params != null) {
        for (Map.Entry<String, String> cursor : params.entrySet()) {
            transformer.setParameter(cursor.getKey(), cursor.getValue());
        }
    }

    StringWriter strWriter = new StringWriter();
    StreamResult result = new StreamResult(strWriter);
    transformer.transform(doc, result);

    return (strWriter.toString());
}