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:XMLTransform.java

public static void main(String[] args) throws Exception {
    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    XMLReader reader = parser.getXMLReader();
    TransformerFactory factory = TransformerFactory.newInstance();
    System.out.println(factory);/* w ww. ja va2 s  . co m*/
    Transformer transformer = factory.newTransformer(new StreamSource("./xsl/books-sql.xsl"));
    transformer.setParameter("user", "root");
    transformer.setParameter("password", "123456");
    transformer.transform(new StreamSource("./xml/books.xml"), new StreamResult(System.out));
}

From source file:UseStylesheetParam.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, SAXException, IOException {
    if (args.length != 1) {
        System.err.println("Please pass one string to this program");
        return;//  w w  w. j  av a 2s.co  m
    }
    // Get the parameter value from the command line.
    String paramValue = args[0];

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource("foo.xsl"));

    // Set the parameter. I can't get non-null namespaces to work!!
    transformer.setParameter("param1", /* parameter name */
            paramValue /* parameter value */ );

    transformer.transform(new StreamSource("foo.xml"), new StreamResult(new OutputStreamWriter(System.out)));
}

From source file:Main.java

public static String getXmlPage(String xmlString, int page, String xslPath) {
    String styledResponse = "";
    StringReader rd = new StringReader(xmlString);
    StringWriter wrt = new StringWriter();
    TransformerFactory tFac = TransformerFactory.newInstance();
    try {/*w  w  w.j  a  v a  2 s  . co m*/
        File xsl = new File(xslPath);
        Transformer transformer = tFac.newTransformer(new StreamSource(xsl));
        transformer.setParameter("Page", String.format("%s", page));
        transformer.transform(new StreamSource(rd), new StreamResult(wrt));

        styledResponse = wrt.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return styledResponse;
}

From source file:Main.java

private static void setParameter(Map<String, String> parameter, Transformer t) {
    if (parameter != null) {
        for (Map.Entry<String, String> e : parameter.entrySet()) {
            t.setParameter(e.getKey(), e.getValue());
        }/*from   w w  w .ja  v a2s.  co m*/
    }
}

From source file:Main.java

protected static Result internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace,
        String[] params) {/*from w  w w  .  j a  va  2  s. co  m*/
    StringWriter sw = new StringWriter();

    try {
        Transformer transformer = templates.newTransformer();
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++)
                transformer.setParameter("param_" + i, params[i]);
        }

        transformer.transform(new StreamSource(doc), r);

        sw.close();
        return r;
    } catch (Throwable th) {
        th.printStackTrace();
        return r;
    }

}

From source file:net.dinkla.mof2ecore.MOF2Ecore.java

/**
 * //  w  ww.  j  av  a 2s  .  c o m
 * @param fileXSL
 * @param fileInput
 * @param fileOutput
 */
public static void mof2ecore(String fileXSL, String fileInput, String fileOutput) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    try {
        System.out.println("Processing " + fileInput);
        Transformer transformer = tFactory.newTransformer(new StreamSource(fileXSL));
        transformer.setParameter("ecore_toplevel_package", namePackage);
        transformer.setParameter("ecore_container_suffix", suffixContainer);

        // TODO remove broken files and throw error
        transformer.transform(new StreamSource(fileInput), new StreamResult(new FileOutputStream(fileOutput)));
        System.out.println("Finished processing " + fileInput);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean transform(InputStream xsltInputStream, InputStream xmlInputStream,
        OutputStream outputStream, Map<String, Object> parameterMap) {

    boolean result = false;

    try {//from w w w  .j  av  a2  s.  c  om

        Source source = new StreamSource(xsltInputStream);
        Transformer transformer = TransformerFactory.newInstance().newTemplates(source).newTransformer();

        for (Entry<String, Object> entry : parameterMap.entrySet()) {
            transformer.setParameter(entry.getKey(), entry.getValue());
        }

        transformer.transform(new StreamSource(xmlInputStream), new StreamResult(outputStream));

        result = true;

    } catch (TransformerException | TransformerFactoryConfigurationError e) {
    }

    return result;
}

From source file:Main.java

private static OutputStream applyTransformation(Source xsltSource, Map<String, Object> xsltParameters,
        InputStream inputXmlStream, OutputStream outputStream) throws TransformerException {

    // Create a transform factory instance.
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // Create a transformer for the stylesheet.
    Transformer transformer = tfactory.newTransformer(xsltSource);

    if (xsltParameters != null) {
        for (String paramName : xsltParameters.keySet()) {
            transformer.setParameter(paramName, xsltParameters.get(paramName));
        }//from w ww .  ja v  a2  s . c o  m
    }

    // Transform the source XML to outputStream.
    transformer.transform(new StreamSource(inputXmlStream), new StreamResult(outputStream));

    return outputStream;
}

From source file:Main.java

/**
 * @param xsltFile The XSLT stylesheet file
 * @param parms XSL parameters to pass to the stylesheet.
 * @return the configured XSLT transformer
 * @throws TransformerException if there is a problem configuring the transformer
 *//*from w ww  .j av a2 s.  co  m*/
static Transformer createTransformer(final File xsltFile, final Map<String, String> parms)
        throws TransformerException {
    try {
        Source xslSource = new StreamSource(xsltFile);
        TransformerFactory transFact = TransformerFactory.newInstance();
        transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFact.newTransformer(xslSource);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        for (String parm : parms.keySet()) {
            transformer.setParameter(parm, parms.get(parm));
        }
        return transformer;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:Main.java

/**
 * General method for transformation to a DOM Document. Transform a Source
 * document using a Source XSL document and an array of parameters.
 * The parameter array consists of a sequence of pairs of (String parametername)
 * followed by (Object parametervalue) in an Object[].
 * @param doc the XML document to transform.
 * @param xsl the XSL transformation program.
 * @param params the array of transformation parameters.
 * @return the transformed text.//  ww w.j  a v  a  2 s.  co  m
 */
public static Document getTransformedDocument(Source doc, Source xsl, Object[] params) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(xsl);
    if ((params != null) && (params.length > 1)) {
        for (int i = 0; i < params.length; i = i + 2) {
            transformer.setParameter((String) params[i], params[i + 1]);
        }
    }
    DOMResult domResult = new DOMResult();
    transformer.transform(doc, domResult);
    return (Document) domResult.getNode();
}