1 10 11 package org.mule.transformers.xml; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.StringReader ; 16 17 import javax.xml.transform.ErrorListener ; 18 import javax.xml.transform.OutputKeys ; 19 import javax.xml.transform.Source ; 20 import javax.xml.transform.Transformer ; 21 import javax.xml.transform.TransformerFactory ; 22 import javax.xml.transform.stream.StreamSource ; 23 24 import org.apache.commons.pool.BasePoolableObjectFactory; 25 import org.apache.commons.pool.ObjectPool; 26 import org.apache.commons.pool.impl.StackObjectPool; 27 import org.mule.config.i18n.Message; 28 import org.mule.config.i18n.Messages; 29 import org.mule.umo.lifecycle.InitialisationException; 30 import org.mule.umo.transformer.TransformerException; 31 import org.mule.umo.transformer.UMOTransformer; 32 import org.mule.util.IOUtils; 33 34 39 40 public class XsltTransformer extends AbstractXmlTransformer 41 { 42 45 private static final long serialVersionUID = -6958917343589717387L; 46 47 private static final int MIN_IDLE = 1; 49 50 private ObjectPool transformerPool; 51 private int maxIdleTransformers = 2; 52 private String xslFile; 53 private String xslt; 54 55 public XsltTransformer() 56 { 57 super(); 58 } 59 60 63 public void initialise() throws InitialisationException 64 { 65 try 66 { 67 transformerPool = new StackObjectPool(new BasePoolableObjectFactory() 68 { 69 public Object makeObject() throws Exception 70 { 71 StreamSource source = getStreamSource(); 72 TransformerFactory factory = TransformerFactory.newInstance(); 73 return factory.newTransformer(source); 74 } 75 }, Math.max(MIN_IDLE, maxIdleTransformers)); 76 transformerPool.addObject(); 77 } 78 catch (Throwable te) 79 { 80 throw new InitialisationException(te, this); 81 } 82 } 83 84 90 public Object doTransform(Object src, String encoding) throws TransformerException 91 { 92 try 93 { 94 Source sourceDoc = getXmlSource(src); 95 if (sourceDoc == null) return null; 96 97 ResultHolder holder = getResultHolder(returnClass); 98 if (holder == null) holder = getResultHolder(src.getClass()); 99 100 DefaultErrorListener errorListener = new DefaultErrorListener(this); 101 Transformer transformer = null; 102 Object result; 103 try 104 { 105 transformer = (Transformer )transformerPool.borrowObject(); 106 107 transformer.setErrorListener(errorListener); 108 transformer.setOutputProperty(OutputKeys.ENCODING, encoding); 109 110 transformer.transform(sourceDoc, holder.getResult()); 111 result = holder.getResultObject(); 112 113 if (errorListener.isError()) 114 { 115 throw errorListener.getException(); 116 } 117 } 118 finally 119 { 120 if (transformer != null) transformerPool.returnObject(transformer); 121 } 122 return result; 123 } 124 catch (Exception e) 125 { 126 throw new TransformerException(this, e); 127 } 128 } 129 130 133 public String getXslFile() 134 { 135 return xslFile; 136 } 137 138 141 public void setXslFile(String xslFile) 142 { 143 this.xslFile = xslFile; 144 } 145 146 152 private StreamSource getStreamSource() throws InitialisationException 153 { 154 if (xslt != null) 155 { 156 return new StreamSource (new StringReader (xslt)); 157 } 158 159 if (xslFile == null) 160 { 161 throw new InitialisationException(new Message(Messages.X_IS_NULL, "xslFile"), this); 162 } 163 164 InputStream is; 165 try 166 { 167 is = IOUtils.getResourceAsStream(xslFile, getClass()); 168 } 169 catch (IOException e) 170 { 171 throw new InitialisationException(e, this); 172 } 173 if (is != null) 174 { 175 return new StreamSource (is); 176 } 177 else 178 { 179 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, xslFile), this); 180 } 181 } 182 183 public Object clone() throws CloneNotSupportedException 184 { 185 XsltTransformer x = (XsltTransformer)super.clone(); 186 try 187 { 188 if (x.nextTransformer == null) 189 { 190 x.initialise(); 191 } 192 } 193 catch (Exception e) 194 { 195 throw new CloneNotSupportedException (e.getMessage()); 196 } 197 return x; 198 } 199 200 private class DefaultErrorListener implements ErrorListener 201 { 202 private TransformerException e = null; 203 204 private UMOTransformer trans; 205 206 public DefaultErrorListener(UMOTransformer trans) 207 { 208 this.trans = trans; 209 } 210 211 public TransformerException getException() 212 { 213 return e; 214 } 215 216 public boolean isError() 217 { 218 return e != null; 219 } 220 221 public void error(javax.xml.transform.TransformerException exception) 222 throws javax.xml.transform.TransformerException 223 { 224 logger.error(exception.getMessage(), exception); 225 e = new TransformerException(trans, exception); 226 } 227 228 public void fatalError(javax.xml.transform.TransformerException exception) 229 throws javax.xml.transform.TransformerException 230 { 231 logger.fatal(exception.getMessage()); 232 e = new TransformerException(trans, exception); 233 } 234 235 public void warning(javax.xml.transform.TransformerException exception) 236 throws javax.xml.transform.TransformerException 237 { 238 logger.warn(exception.getMessage()); 239 } 240 } 241 242 246 public int getMaxIdleTransformers() 247 { 248 return maxIdleTransformers; 249 } 250 251 257 public void setMaxIdleTransformers(int maxIdleTransformers) 258 { 259 this.maxIdleTransformers = maxIdleTransformers; 260 } 261 262 public String getXslt() 263 { 264 return xslt; 265 } 266 267 public void setXslt(String xslt) 268 { 269 this.xslt = xslt; 270 } 271 } 272 | Popular Tags |