| Commit in servicemix/base/src/main/java/org/servicemix/components/xslt on MAIN | |||
| XsltComponent.java | +66 | -26 | 1.7 -> 1.8 |
Followed Ryan Schweter's suggestions and made the copying of properties the default plus minor refactorings to make it easier to derive and change behaviour
servicemix/base/src/main/java/org/servicemix/components/xslt
diff -u -r1.7 -r1.8 --- XsltComponent.java 31 Aug 2005 10:19:05 -0000 1.7 +++ XsltComponent.java 31 Aug 2005 11:43:25 -0000 1.8 @@ -19,6 +19,7 @@
import org.servicemix.MessageExchangeListener; import org.servicemix.components.util.TransformComponentSupport;
+import org.servicemix.jbi.jaxp.BytesSource;
import org.servicemix.jbi.jaxp.StringSource; import org.springframework.core.io.Resource;
@@ -34,16 +35,19 @@
import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource;
+ +import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.StringWriter; import java.net.URL; import java.util.Iterator; /**
- * An <a href="">XSLT</a> based JBI component using - * <a href="">JAXP</a> to perform the XSLT transformation. - * - * @version $Revision: 1.7 $
+ * An <a href="">XSLT</a> based JBI component using <a + * href="">JAXP</a> to perform the XSLT + * transformation. + * + * @version $Revision: 1.8 $
*/
public class XsltComponent extends TransformComponentSupport implements MessageExchangeListener {
@@ -52,10 +56,11 @@
private Resource xsltResource;
private Templates templates;
private boolean disableOutput;
-
+ private boolean copyProperties = true; + private boolean useStringBuffer = true;
// Properties
- //-------------------------------------------------------------------------
+ // -------------------------------------------------------------------------
public TransformerFactory getTransformerFactory() {
if (transformerFactory == null) {
transformerFactory = TransformerFactory.newInstance();
@@ -96,31 +101,65 @@
this.disableOutput = disableOutput;
}
+ public boolean isCopyProperties() {
+ return copyProperties;
+ }
+
+ public void setCopyProperties(boolean copyProperties) {
+ this.copyProperties = copyProperties;
+ }
+
+ public boolean isUseStringBuffer() {
+ return useStringBuffer;
+ }
+
+ public void setUseStringBuffer(boolean useStringBuffer) {
+ this.useStringBuffer = useStringBuffer;
+ }
+
// Implementation methods
- //-------------------------------------------------------------------------
- protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+ // -------------------------------------------------------------------------
+ protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out)
+ throws MessagingException {
try {
Transformer transformer = createTransformer(exchange, in);
configureTransformer(transformer, exchange, in);
+ copyProperties(exchange, in, out);
+ transformContent(transformer, exchange, in, out);
+ return shouldOutputResult(transformer);
+ }
+ catch (TransformerException e) {
+ e.printStackTrace();
+ throw new MessagingException("Failed to transform: " + e, e);
+ }
+ catch (IOException e) {
+ throw new MessagingException("Failed to load transform: " + e, e);
+ }
+ }
+
+ protected void transformContent(Transformer transformer, MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws TransformerException, MessagingException {
+ if (isUseStringBuffer()) {
StringWriter buffer = new StringWriter();
Result result = new StreamResult(buffer);
transformer.transform(in.getContent(), result);
- String xml = buffer.toString(); - out.setContent(new StringSource(xml)); - /**
+ out.setContent(new StringSource(buffer.toString()));
+ }
+ else {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Result result = new StreamResult(buffer);
transformer.transform(in.getContent(), result);
out.setContent(new BytesSource(buffer.toByteArray()));
- */ - return shouldOutputResult(transformer);
}
- catch (TransformerException e) {
- e.printStackTrace();
- throw new MessagingException("Failed to transform: " + e, e);
- }
- catch (IOException e) {
- throw new MessagingException("Failed to load transform: " + e, e);
+ }
+
+ protected void copyProperties(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) {
+ if (isCopyProperties()) {
+ for (Iterator iter = in.getPropertyNames().iterator(); iter.hasNext();) {
+ String name = (String) iter.next();
+ Object value = in.getProperty(name);
+ out.setProperty(name, value);
+
+ }
}
}
@@ -132,13 +171,12 @@
return false;
}
return true;
- /**
- String value = transformer.getOutputProperty("disableOutput");
- return value == null || !value.equals("true");
- */
+ /**
+ * String value = transformer.getOutputProperty("disableOutput"); return
+ * value == null || !value.equals("true");
+ */
}
-
protected Source createXsltSource() throws IOException {
if (xsltResource != null) {
URL url = ""
@@ -170,7 +208,8 @@
/**
* Factory method to create a new transformer instance
*/
- protected Transformer createTransformer(MessageExchange exchange, NormalizedMessage in) throws TransformerConfigurationException, IOException {
+ protected Transformer createTransformer(MessageExchange exchange, NormalizedMessage in)
+ throws TransformerConfigurationException, IOException {
Source source = getXsltSource();
if (source == null) {
return getTransformerFactory().newTransformer();
@@ -181,7 +220,8 @@
}
/**
- * A hook to allow the transformer to be configured from the current exchange and inbound message
+ * A hook to allow the transformer to be configured from the current + * exchange and inbound message
*/
protected void configureTransformer(Transformer transformer, MessageExchange exchange, NormalizedMessage in) {
for (Iterator iter = exchange.getPropertyNames().iterator(); iter.hasNext();) {
