cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r676523 - in /cocoon/whiteboard/corona/trunk/corona-pipeline/src: main/java/org/apache/cocoon/corona/pipeline/component/ main/java/org/apache/cocoon/corona/pipeline/util/ test/java/org/apache/cocoon/corona/pipeline/ test/resources/
Date Mon, 14 Jul 2008 09:20:36 GMT
Author: reinhard
Date: Mon Jul 14 02:20:36 2008
New Revision: 676523

URL: http://svn.apache.org/viewvc?rev=676523&view=rev
Log:
provide a utility class for chained XSLT transformation starting with a simple String

Added:
    cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
  (with props)
    cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt   (with props)
Modified:
    cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XMLSerializer.java
    cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XSLTTransformer.java
    cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/java/org/apache/cocoon/corona/pipeline/PipelineTest.java

Modified: cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XMLSerializer.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XMLSerializer.java?rev=676523&r1=676522&r2=676523&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XMLSerializer.java
(original)
+++ cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XMLSerializer.java
Mon Jul 14 02:20:36 2008
@@ -40,6 +40,14 @@
 
     private TransformerHandler transformerHandler;
 
+    public XMLSerializer() {
+    }
+
+    public XMLSerializer(Properties format) {
+        super();
+        this.format = format;
+    }
+
     public void init() {
         try {
             this.transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();

Modified: cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XSLTTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XSLTTransformer.java?rev=676523&r1=676522&r2=676523&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XSLTTransformer.java
(original)
+++ cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/component/XSLTTransformer.java
Mon Jul 14 02:20:36 2008
@@ -41,6 +41,21 @@
     private Map<String, Object> parameters;
     private URL source;
 
+    public XSLTTransformer() {
+        super();
+    }
+
+    public XSLTTransformer(URL source) {
+        super();
+        this.source = source;
+    }
+
+    public XSLTTransformer(URL source, Map<String, Object> parameters) {
+        super();
+        this.parameters = parameters;
+        this.source = source;
+    }
+
     /**
      * Test if the name is a valid parameter name for XSLT
      */

Added: cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java?rev=676523&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
(added)
+++ cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
Mon Jul 14 02:20:36 2008
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.corona.pipeline.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.cocoon.corona.pipeline.NonCachingPipeline;
+import org.apache.cocoon.corona.pipeline.Pipeline;
+import org.apache.cocoon.corona.pipeline.component.StringGenerator;
+import org.apache.cocoon.corona.pipeline.component.XMLSerializer;
+import org.apache.cocoon.corona.pipeline.component.XSLTTransformer;
+
+/**
+ * This class contains utility methods to transform an XML string by using XSLT. It hides
all the details of the Java
+ * TRaX API by using SAX pipeline components that are linked to each other. This means that
the SAX events stream
+ * through through the pipeline and are serialized at the end.
+ * 
+ * NOTE: Exception handling needs to be discussed and will probably change.
+ */
+public abstract class StringTransformation {
+
+    /**
+     * Transform an XML string by using XSLT.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @return The transformed result as {@link String}.
+     * 
+     * @throws Exception
+     */
+    public static String transform(String xmlString, URL... xsltUrls) throws Exception {
+        return transform(xmlString, (Map<String, Object>) null, (Properties) null,
xsltUrls);
+    }
+
+    /**
+     * Transform an XML string by using XSLT. This method also allows passing parameters
to the stylesheets.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param xsltParameters Parameters that are available in all XSLT stylesheets.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @return The transformed result as {@link String}.
+     * @throws Exception
+     */
+    public static String transform(String xmlString, Map<String, Object> xsltParameters,
URL... xsltUrls)
+            throws Exception {
+        return transform(xmlString, xsltParameters, (Properties) null, xsltUrls);
+    }
+
+    /**
+     * Transform an XML string by using XSLT. This method allows defining the output properties.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param outputProperties XML output properties. See {@link http://www.w3.org/TR/xslt#output}.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @return The transformed result as {@link String}.
+     * @throws Exception
+     */
+    public static String transform(String xmlString, Properties outputProperties, URL...
xsltUrls) throws Exception {
+        return transform(xmlString, (Map<String, Object>) null, outputProperties, xsltUrls);
+    }
+
+    /**
+     * Transform an XML string by using XSLT. This method allows defining the output properties
and also allows passing
+     * parameters to the stylesheets.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param xsltParameters Parameters that are available in all XSLT stylesheets.
+     * @param outputProperties XML output properties. See {@link http://www.w3.org/TR/xslt#output}.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @return The transformed result as {@link String}.
+     * @throws Exception
+     */
+    public static String transform(String xmlString, Map<String, Object> xsltParameters,
Properties outputProperties,
+            URL... xsltUrls) throws Exception {
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        transform(xmlString, xsltParameters, outputProperties, byteArrayOutputStream, xsltUrls);
+        return new String(byteArrayOutputStream.toByteArray());
+    }
+
+    /**
+     * Transform an XML string by using XSLT and write the result into an {@link OutputStream}.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param outputStream The result is written into this {@link OutputStream}.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @throws Exception
+     */
+    public static void transform(String xmlString, OutputStream outputStream, URL... xsltURLs)
throws Exception {
+        transform(xmlString, (Map<String, Object>) null, (Properties) null, outputStream,
xsltURLs);
+    }
+
+    /**
+     * Transform an XML string by using XSLT and write the result into an {@link OutputStream}.
This method also allows
+     * passing parameters to the stylesheets.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param outputStream The result is written into this {@link OutputStream}.
+     * @param xsltParameters Parameters that are available in all XSLT stylesheets.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @throws Exception
+     */
+    public static void transform(String xmlString, OutputStream outputStream, Map<String,
Object> xsltParameters,
+            URL... xsltURLs) throws Exception {
+        transform(xmlString, xsltParameters, (Properties) null, outputStream, xsltURLs);
+    }
+
+    /**
+     * Transform an XML string by using XSLT and write the result into an {@link OutputStream}.
This method allows
+     * defining the output properties.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param outputStream The result is written into this {@link OutputStream}.
+     * @param outputProperties XML output properties. See {@link http://www.w3.org/TR/xslt#output}.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * @throws Exception
+     */
+    public static void transform(String xmlString, OutputStream outputStream, Properties
outputProperties,
+            URL... xsltURLs) throws Exception {
+        transform(xmlString, (Map<String, Object>) null, outputProperties, outputStream,
xsltURLs);
+    }
+
+    /**
+     * Transform an XML string and write the result into an {@link OutputStream}.
+     * 
+     * @param xmlString The String to be transformed.
+     * @param xsltParameters Parameters that are available in all XSLT stylesheets.
+     * @param outputProperties XML output properties. See {@link http://www.w3.org/TR/xslt#output}.
+     * @param outputStream The result is written into this {@link OutputStream}.
+     * @param xsltUrls The {@link URL}s of all XSLT stylesheets that are used in the order
they are passed to transform
+     *            the passed XML string.
+     * 
+     * @throws Exception
+     */
+    public static void transform(String xmlString, Map<String, Object> xsltParameters,
Properties outputProperties,
+            OutputStream outputStream, URL... xsltUrls) throws Exception {
+        Pipeline pipeline = new NonCachingPipeline();
+
+        pipeline.addComponent(new StringGenerator(xmlString));
+        for (URL xsltUrl : xsltUrls) {
+            pipeline.addComponent(new XSLTTransformer(xsltUrl, xsltParameters));
+        }
+
+        if (outputProperties == null) {
+            outputProperties = new Properties();
+            outputProperties.put("method", "xml");
+        }
+        XMLSerializer xmlSerializer = new XMLSerializer(outputProperties);
+        xmlSerializer.init();
+        pipeline.addComponent(xmlSerializer);
+
+        pipeline.execute(null, outputStream);
+    }
+}
\ No newline at end of file

Propchange: cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/whiteboard/corona/trunk/corona-pipeline/src/main/java/org/apache/cocoon/corona/pipeline/util/StringTransformation.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/java/org/apache/cocoon/corona/pipeline/PipelineTest.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/java/org/apache/cocoon/corona/pipeline/PipelineTest.java?rev=676523&r1=676522&r2=676523&view=diff
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/java/org/apache/cocoon/corona/pipeline/PipelineTest.java
(original)
+++ cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/java/org/apache/cocoon/corona/pipeline/PipelineTest.java
Mon Jul 14 02:20:36 2008
@@ -18,9 +18,14 @@
  */
 package org.apache.cocoon.corona.pipeline;
 
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
 import junit.framework.TestCase;
 
 import org.apache.cocoon.corona.pipeline.component.FileReaderComponent;
+import org.apache.cocoon.corona.pipeline.util.StringTransformation;
 
 public class PipelineTest extends TestCase {
 
@@ -33,4 +38,36 @@
         pipeline.addComponent(new FileReaderComponent(PipelineTest.class.getResource("/test.xml")));
         pipeline.execute(null, System.out);
     }
+
+    public void testStringTransformation() throws Exception {
+        String result = StringTransformation.transform("<x>test</x>", PipelineTest.class.getClassLoader().getResource(
+                "test.xslt"));
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><p/>", result);
+    }
+
+    public void testStringTransformationWithParameter() throws Exception {
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("myParam", "abc");
+        String result = StringTransformation.transform("<x>test</x>", params,
PipelineTest.class.getClassLoader()
+                .getResource("test.xslt"));
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><p>abc</p>",
result);
+    }
+
+    public void testStringTransformationWithProperties() throws Exception {
+        Properties props = new Properties();
+        props.put("encoding", "iso-8859-1");
+        String result = StringTransformation.transform("<x>test</x>", props,
PipelineTest.class.getClassLoader()
+                .getResource("test.xslt"));
+        assertEquals("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><p/>", result);
+    }
+
+    public void testStringTransformationWithPropertiesAndParameters() throws Exception {
+        Properties props = new Properties();
+        props.put("encoding", "iso-8859-1");
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("myParam", "abc");
+        String result = StringTransformation.transform("<x>test</x>", params,
props, PipelineTest.class
+                .getClassLoader().getResource("test.xslt"));
+        assertEquals("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><p>abc</p>",
result);
+    }
 }

Added: cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt?rev=676523&view=auto
==============================================================================
--- cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt (added)
+++ cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt Mon Jul 14
02:20:36 2008
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+  
+  http://www.apache.org/licenses/LICENSE-2.0
+  
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+
+  <xsl:param name="myParam" />
+
+  <xsl:template match="/">
+    <p>
+      <xsl:value-of select="$myParam" />
+    </p>
+  </xsl:template>
+
+</xsl:stylesheet>

Propchange: cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/whiteboard/corona/trunk/corona-pipeline/src/test/resources/test.xslt
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message