1 package org.xmlBlaster.util;
  2 
  3 import java.io.ByteArrayInputStream;
  4 import java.io.ByteArrayOutputStream;
  5 import java.io.StringReader;
  6 import java.net.URL;
  7 import java.util.Iterator;
  8 import java.util.Map;
  9 import java.util.Map.Entry;
 10 
 11 import javax.xml.transform.Transformer;
 12 import javax.xml.transform.TransformerException;
 13 import javax.xml.transform.TransformerFactory;
 14 import javax.xml.transform.URIResolver;
 15 import javax.xml.transform.stream.StreamResult;
 16 import javax.xml.transform.stream.StreamSource;
 17 
 18 import org.xmlBlaster.util.def.ErrorCode;
 19 
 20 /**
 21  * Holds Transformers cached.
 22  * 
 23  * @author Michele Laghi / Marcel Ruff
 24  */
 25 public class XslTransformer {
 26    private Global glob;
 27    private Transformer transformer;
 28    
 29    /**
 30     * Constructs a transformer instance. It is stateful since it holds an own transformer, i.e. it is dedicated to a
 31     * single xsl (with parameters).
 32     * 
 33     * @param xslFilename The mandatory relative name of the xsl file. It is searched in the Classpath. Can not be null. 
 34     * @param systemId The systemId to associate to the given xsl file. You can pass null here.
 35     * @param uriResolver A custom uri resolver to be used to find included xsl stylesheets. If you pass null, the default
 36     * uriResolver will be used.
 37     * @param props A map containing parameters (or variables) to be passed to the stylesheet. These can be used inside
 38     * the stylesheet.
 39     * 
 40     * @throws XmlBlasterException If an exception occurs when creating the transformer object.
 41     */
 42    public XslTransformer(Global glob, String xslFilename, String systemId, URIResolver uriResolver, Map props) throws XmlBlasterException {
 43       this.glob = glob;
 44       try {
 45          FileLocator locator = new FileLocator(glob);
 46          URL url = locator.findFileInXmlBlasterSearchPath("dummydummy", xslFilename);
 47          String xslLiteral = locator.read(url);
 48          this.transformer = newTransformer(systemId, xslLiteral, uriResolver, props);
 49       }
 50       catch (XmlBlasterException e) {
 51          throw e;
 52       }
 53       catch (Throwable e) {
 54          e.printStackTrace();
 55          throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION,
 56                "XslTransformer", "constructor.", e);
 57       }
 58       
 59    }
 60 
 61    /**
 62     * @param systemId
 63     * @param xslString
 64     * @param uriResolver
 65     * @param props Map<String, String>
 66     * @return
 67     * @throws Exception
 68     */
 69    private static Transformer newTransformer(String systemId, String xslString, URIResolver uriResolver, Map props) throws Exception {
 70       TransformerFactory transformerFactory = TransformerFactory.newInstance();
 71       if (uriResolver != null)
 72          transformerFactory.setURIResolver(uriResolver);
 73       StreamSource xslStreamSource = null;
 74       if(systemId != null)
 75           xslStreamSource = new StreamSource(new StringReader(xslString), systemId);
 76       else
 77           xslStreamSource = new StreamSource(new StringReader(xslString));
 78 
 79       Transformer transformer = transformerFactory.newTransformer(xslStreamSource);
 80       if(props != null) {
 81           Iterator iter = props.entrySet().iterator();
 82           while(iter.hasNext()) {
 83               Entry entry = (Entry)iter.next();
 84               transformer.setParameter((String)entry.getKey(), (String)entry.getValue());
 85           }
 86       }
 87       return transformer;
 88    }
 89        
 90    public byte[] doXSLTransformation(byte[] xmlLiteral) throws XmlBlasterException {
 91       if (this.transformer == null) {
 92          return xmlLiteral;
 93       }
 94       else {
 95          try {
 96             StreamSource xmlStreamSource = new StreamSource(new ByteArrayInputStream(xmlLiteral));
 97             ByteArrayOutputStream os = new ByteArrayOutputStream();
 98             StreamResult resultStream = new StreamResult(os);
 99             this.transformer.transform(xmlStreamSource, resultStream);
100             return os.toByteArray();
101          }
102          catch (TransformerException ex) {
103             throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, "XslTransformer", "An exception occured when transforming '" + xmlLiteral + "'", ex);
104          }
105       }
106    }
107    
108 }


syntax highlighted by Code2HTML, v. 0.9.1