Here's an example from my code that sets a parameter in the stylesheet to an
externally-computed value. Also includes a simple cache for Templates.
Ken
/*
* Utils.java
*
* Created on March 18, 2002, 11:01 AM
*
*2002 04 11: KJM Changed tool to cache templates, not transformer objects.
* Apparently, transformers aren't thread-safe.
* Also, moved template/transformer caching to a separate method
getTransformer
*/
package com.ca.portal.components;
// General classes
import java.io.File;
// Import dom4j classes
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
// Classes needed for XSL
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import javax.xml.transform.stream.StreamSource;
// Import log4j classes.
import org.apache.log4j.Category;
/**
*
* @author melke01
*/
public class Utils {
private static String libStyles = "c:\\add\\FastFolder\\stylesheets\\";
private static TransformerFactory factory = TransformerFactory.newInstance();
private static java.util.Map templates = new java.util.Hashtable();
private static Global props = Global.getInstance();
private static Category cat = Category.getInstance("Utils");
// I could put an API here, but not yet....
private static boolean xslCache = true ;
// Get a starting category
/** Creates a new instance of Utils */
public Utils() {
}
public static void setStyleLib (String lib)
{ if ((lib == null) || (lib.equals(libStyles))) {
cat.debug("Not setting stylesheet library.");
return;
}
cat.debug("Setting stylesheet library to " + lib);
libStyles = lib;
templates = new java.util.Hashtable();
}
private static Transformer getTransformer(String style) {
Templates templ;
try{
templ = (Templates) templates.get(style);
if (templ == null)
{
// create a template for the transformer using JAXP
cat.debug("Creating template from stylesheet: " + libStyles + style + ".xsl");
templ = factory.newTemplates(
new StreamSource( new File ( libStyles + style + ".xsl" ) ));
if (xslCache) templates.put(style, templ);
cat.info("XSL templ created");
}
return templ.newTransformer();
}
catch (Exception e)
{
cat.warn("Couldn't create a stylesheet for " + style + ".");
cat.warn(e.toString());
return null;
}
}
private static Transformer getNewTransformer(String style) {
// create a transformer for the transformer using JAXP
try {
cat.debug("Creating a new transformer from stylesheet: " + libStyles + style +
".xsl");
Transformer trans = factory.newTransformer(
new StreamSource( new File ( libStyles + style + ".xsl" ) ));
cat.debug("XSL transformer created");
return trans;
}
catch (Exception e)
{
cat.warn("Couldn't create a tranformer for " + style + ".");
cat.warn(e.toString());
return null;
}
}
public static String styleType(String style) {
return getTransformer(style).getOutputProperty
(javax.xml.transform.OutputKeys.METHOD);
}
public static void styleOutput(Document document, String style, java.io.Writer
out) {
cat.debug(document.asXML());
Transformer transformer = getTransformer(style);
try{
// now lets style the given document
DocumentSource source = new DocumentSource( document );
javax.xml.transform.stream.StreamResult result = new
javax.xml.transform.stream.StreamResult(out);
String hostPortal = "";
if (! props.isPortal)
hostPortal = props.protocol + "://" + props.host +":" + props.port;
cat.debug("Setting host portal address to " + hostPortal);
transformer.setParameter("hostPortal", hostPortal);
transformer.transform( source, result );
cat.info("Transformed");
return;
}
catch (Exception e)
{
System.out.flush();
cat.warn (e.toString());
return;
}
}
}
-------------------------------------------------------
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone? Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user