1 17 package org.apache.servicemix.components.xslt; 18 19 import org.apache.servicemix.MessageExchangeListener; 20 import org.apache.servicemix.components.util.TransformComponentSupport; 21 import org.apache.servicemix.jbi.jaxp.BytesSource; 22 import org.apache.servicemix.jbi.jaxp.StringSource; 23 import org.springframework.core.io.Resource; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Node ; 26 27 import javax.jbi.messaging.MessageExchange; 28 import javax.jbi.messaging.MessagingException; 29 import javax.jbi.messaging.NormalizedMessage; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.Result ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.Templates ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.stream.StreamResult ; 41 import javax.xml.transform.stream.StreamSource ; 42 43 import java.io.ByteArrayOutputStream ; 44 import java.io.IOException ; 45 import java.io.StringWriter ; 46 import java.net.URL ; 47 import java.util.Iterator ; 48 49 56 public class XsltComponent extends TransformComponentSupport implements MessageExchangeListener { 57 58 private TransformerFactory transformerFactory; 59 private Source xsltSource; 60 private Resource xsltResource; 61 private Templates templates; 62 private boolean disableOutput; 63 private boolean useStringBuffer = true; 64 private boolean forceDocIfDom = true; 65 66 69 public boolean isForceDocIfDom() { 70 return forceDocIfDom; 71 } 72 73 76 public void setForceDocIfDom(boolean forceDocIfDom) { 77 this.forceDocIfDom = forceDocIfDom; 78 } 79 80 public TransformerFactory getTransformerFactory() { 83 if (transformerFactory == null) { 84 transformerFactory = TransformerFactory.newInstance(); 85 } 86 return transformerFactory; 87 } 88 89 public void setTransformerFactory(TransformerFactory transformerFactory) { 90 this.transformerFactory = transformerFactory; 91 } 92 93 public Source getXsltSource() throws IOException { 94 if (xsltSource == null) { 95 return createXsltSource(); 98 } 99 return xsltSource; 100 } 101 102 public void setXsltSource(Source xsltSource) { 103 this.xsltSource = xsltSource; 104 } 105 106 public Resource getXsltResource() { 107 return xsltResource; 108 } 109 110 public void setXsltResource(Resource xsltResource) { 111 this.xsltResource = xsltResource; 112 } 113 114 public boolean isDisableOutput() { 115 return disableOutput; 116 } 117 118 public void setDisableOutput(boolean disableOutput) { 119 this.disableOutput = disableOutput; 120 } 121 122 public boolean isUseStringBuffer() { 123 return useStringBuffer; 124 } 125 126 public void setUseStringBuffer(boolean useStringBuffer) { 127 this.useStringBuffer = useStringBuffer; 128 } 129 130 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) 133 throws MessagingException { 134 try { 135 Transformer transformer = createTransformer(exchange, in); 136 configureTransformer(transformer, exchange, in); 137 copyPropertiesAndAttachments(exchange, in, out); 138 transformContent(transformer, exchange, in, out); 139 return shouldOutputResult(transformer); 140 } 141 catch (ParserConfigurationException e) { 142 e.printStackTrace(); 143 throw new MessagingException("Failed to transform: " + e, e); 144 } 145 catch (TransformerException e) { 146 e.printStackTrace(); 147 throw new MessagingException("Failed to transform: " + e, e); 148 } 149 catch (IOException e) { 150 throw new MessagingException("Failed to load transform: " + e, e); 151 } 152 } 153 154 protected void transformContent(Transformer transformer, MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws TransformerException , MessagingException, ParserConfigurationException { 155 Source src = in.getContent(); 156 if (forceDocIfDom && src instanceof DOMSource ) { 157 Node n = ((DOMSource ) src).getNode(); 158 if (n instanceof Document == false) { 159 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 160 doc.appendChild(doc.importNode(n, true)); 161 src = new DOMSource (doc); 162 } 163 } 164 if (isUseStringBuffer()) { 165 StringWriter buffer = new StringWriter (); 166 Result result = new StreamResult (buffer); 167 transformer.transform(src, result); 168 out.setContent(new StringSource(buffer.toString())); 169 } 170 else { 171 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 172 Result result = new StreamResult (buffer); 173 transformer.transform(src, result); 174 out.setContent(new BytesSource(buffer.toByteArray())); 175 } 176 } 177 178 181 protected boolean shouldOutputResult(Transformer transformer) { 182 if (disableOutput) { 183 return false; 184 } 185 return true; 186 190 } 191 192 protected Source createXsltSource() throws IOException { 193 if (xsltResource != null) { 194 URL url = xsltResource.getURL(); 195 if (url == null) { 196 return new StreamSource (xsltResource.getInputStream()); 197 } 198 else { 199 return new StreamSource (xsltResource.getInputStream(), url.toExternalForm()); 200 } 201 } 202 return null; 203 } 204 205 public Templates getTemplates() throws IOException , TransformerConfigurationException { 206 if (templates == null) { 207 templates = createTemplates(); 208 } 209 return templates; 210 } 211 212 215 protected Templates createTemplates() throws TransformerConfigurationException , IOException { 216 Source source = getXsltSource(); 217 return getTransformerFactory().newTemplates(source); 218 } 219 220 223 protected Transformer createTransformer(MessageExchange exchange, NormalizedMessage in) 224 throws TransformerConfigurationException , IOException { 225 Source source = getXsltSource(); 226 if (source == null) { 227 return getTransformerFactory().newTransformer(); 228 } 229 else { 230 return getTemplates().newTransformer(); 231 } 232 } 233 234 238 protected void configureTransformer(Transformer transformer, MessageExchange exchange, NormalizedMessage in) { 239 for (Iterator iter = exchange.getPropertyNames().iterator(); iter.hasNext();) { 240 String name = (String ) iter.next(); 241 Object value = exchange.getProperty(name); 242 transformer.setParameter(name, value); 243 } 244 for (Iterator iter = in.getPropertyNames().iterator(); iter.hasNext();) { 245 String name = (String ) iter.next(); 246 Object value = in.getProperty(name); 247 transformer.setParameter(name, value); 248 } 249 transformer.setParameter("exchange", exchange); 250 transformer.setParameter("in", in); 251 transformer.setParameter("component", this); 252 } 253 254 } 255 | Popular Tags |