You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ve...@apache.org on 2008/06/29 16:40:14 UTC

svn commit: r672627 - in /synapse/trunk/java: ./ modules/core/src/main/java/org/apache/synapse/mediators/transform/ modules/core/src/main/java/org/apache/synapse/util/jaxp/ modules/core/src/test/java/org/apache/synapse/mediators/transform/

Author: veithen
Date: Sun Jun 29 07:40:14 2008
New Revision: 672627

URL: http://svn.apache.org/viewvc?rev=672627&view=rev
Log:
SYNAPSE-378 & Saxon support:
* Added initial (incomplete!) implementation of an AXIOMSource.
* Improved XSLTMediatorTest to carry out tests with both Xalan and Saxon.
* Changed version of spring-xml to fix an issue when XSLTMediator is used with Saxon and StaxSource.
* Fixed an issue in XSLTMediator that prevented it from working with Saxon.

Added:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSource.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilder.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilderFactory.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMXMLReader.java
Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java
    synapse/trunk/java/pom.xml

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java?rev=672627&r1=672626&r2=672627&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/transform/XSLTMediator.java Sun Jun 29 07:40:14 2008
@@ -281,7 +281,8 @@
             synLog.traceOrDebug("Transformation completed - processing result");
 
             // get the result OMElement
-            OMElement result = resultBuilder.getNode(Charset.forName(encoding));
+            OMElement result =
+                resultBuilder.getNode(encoding == null ? null : Charset.forName(encoding));
 
             if (synLog.isTraceTraceEnabled()) {
                 synLog.traceTrace("Transformation result : " + result.toString());

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSource.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSource.java?rev=672627&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSource.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSource.java Sun Jun 29 07:40:14 2008
@@ -0,0 +1,38 @@
+/*
+ *  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.synapse.util.jaxp;
+
+import javax.xml.transform.sax.SAXSource;
+
+import org.apache.axiom.om.OMElement;
+import org.xml.sax.InputSource;
+
+/**
+ * Implementation of {@link javax.xml.transform.Source} for AXIOM.
+ * The implementation is based on {@link SAXSource} and directly transforms an AXIOM
+ * tree into a stream of SAX events using {@link AXIOMXMLReader}.
+ * <p>
+ * NOTE: THIS IS WORK IN PROGRESS!
+ */
+public class AXIOMSource extends SAXSource {
+    public AXIOMSource(OMElement element) {
+        super(new AXIOMXMLReader(element), new InputSource());
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilder.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilder.java?rev=672627&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilder.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilder.java Sun Jun 29 07:40:14 2008
@@ -0,0 +1,37 @@
+/*
+ *  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.synapse.util.jaxp;
+
+import javax.xml.transform.Source;
+
+import org.apache.axiom.om.OMElement;
+
+/**
+ * {@link SourceBuilder} implementation that transforms the AXIOM tree to SAX
+ * using {@link AXIOMSource}.
+ */
+public class AXIOMSourceBuilder implements SourceBuilder {
+    public Source getSource(OMElement node) {
+        return new AXIOMSource(node);
+    }
+
+    public void release() {
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilderFactory.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilderFactory.java?rev=672627&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilderFactory.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMSourceBuilderFactory.java Sun Jun 29 07:40:14 2008
@@ -0,0 +1,32 @@
+/*
+ *  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.synapse.util.jaxp;
+
+import org.apache.synapse.core.SynapseEnvironment;
+
+/**
+ * {@link SourceBuilderFactory} implementation that creates {@link AXIOMSourceBuilder}
+ * instances.
+ */
+public class AXIOMSourceBuilderFactory implements SourceBuilderFactory {
+    public SourceBuilder createSourceBuilder(SynapseEnvironment synEnv) {
+        return new AXIOMSourceBuilder();
+    }
+}

Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMXMLReader.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMXMLReader.java?rev=672627&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMXMLReader.java (added)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/util/jaxp/AXIOMXMLReader.java Sun Jun 29 07:40:14 2008
@@ -0,0 +1,258 @@
+/*
+ *  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.synapse.util.jaxp;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMText;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
+
+/**
+ * SAX {@link XMLReader} implementation that reads the supplied AXIOM tree and invokes the
+ * callback methods on the configured {@link ContentHandler}.
+ * <p>
+ * NOTE: THIS IS WORK IN PROGRESS!
+ */
+public class AXIOMXMLReader implements XMLReader {
+    private final OMElement element;
+    private final AttributesAdapter attributesAdapter = new AttributesAdapter();
+    
+    private boolean namespaces = true;
+    private boolean namespacePrefixes = false;
+    
+    private ContentHandler contentHandler;
+    private DTDHandler dtdHandler;
+    private EntityResolver entityResolver;
+    private ErrorHandler errorHandler;
+    
+    public AXIOMXMLReader(OMElement element) {
+        this.element = element;
+    }
+
+    public ContentHandler getContentHandler() {
+        return contentHandler;
+    }
+
+    public void setContentHandler(ContentHandler contentHandler) {
+        this.contentHandler = contentHandler;
+    }
+
+    public DTDHandler getDTDHandler() {
+        return dtdHandler;
+    }
+
+    public void setDTDHandler(DTDHandler dtdHandler) {
+        this.dtdHandler = dtdHandler;
+    }
+
+    public EntityResolver getEntityResolver() {
+        return entityResolver;
+    }
+
+    public void setEntityResolver(EntityResolver entityResolver) {
+        this.entityResolver = entityResolver;
+    }
+
+    public ErrorHandler getErrorHandler() {
+        return errorHandler;
+    }
+
+    public void setErrorHandler(ErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
+    }
+
+    public boolean getFeature(String name)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        throw new SAXNotRecognizedException(name);
+    }
+
+    public void setFeature(String name, boolean value)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        
+        if ("http://xml.org/sax/features/namespaces".equals(name)) {
+            namespaces = value;
+        } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) {
+            namespacePrefixes = value;
+        } else {
+            throw new SAXNotRecognizedException(name);
+        }
+    }
+
+    public Object getProperty(String name)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        
+        if ("http://xml.org/sax/features/namespaces".equals(name)) {
+            return namespaces;
+        } else if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) {
+            return namespacePrefixes;
+        } else {
+            throw new SAXNotRecognizedException(name);
+        }
+    }
+
+    public void setProperty(String name, Object value)
+            throws SAXNotRecognizedException, SAXNotSupportedException {
+        throw new SAXNotRecognizedException(name);
+    }
+
+    public void parse(InputSource input) throws IOException, SAXException {
+        parse();
+    }
+
+    public void parse(String systemId) throws IOException, SAXException {
+        parse();
+    }
+    
+    private void parse() throws SAXException {
+        contentHandler.startDocument();
+        generateEvents(element);
+        contentHandler.endDocument();
+    }
+    
+    private void generateEvents(OMElement omElement) throws SAXException {
+        OMNamespace omNamespace = omElement.getNamespace();
+        String uri;
+        String prefix;
+        if (omNamespace != null) {
+            uri = omNamespace.getNamespaceURI();
+            prefix = omNamespace.getPrefix();
+        } else {
+            uri = "";
+            prefix = null;
+        }
+        String localName = omElement.getLocalName();
+        String qName;
+        if (prefix == null || prefix.length() == 0) {
+            qName = localName;
+        } else {
+            qName = prefix + ":" + localName;
+        }
+        // For performance reasons, we always reuse the same instance of AttributesAdapter.
+        // This is explicitely allowed by the specification of the startElement method.
+        attributesAdapter.setAttributes(omElement);
+        contentHandler.startElement(uri, localName, qName, attributesAdapter);
+        for (Iterator it = omElement.getChildren(); it.hasNext(); ) {
+            OMNode node = (OMNode)it.next();
+            switch (node.getType()) {
+                case OMNode.ELEMENT_NODE:
+                    generateEvents((OMElement)node);
+                    break;
+                case OMNode.TEXT_NODE:
+                case OMNode.CDATA_SECTION_NODE:
+                    generateEvents((OMText)node);
+            }
+        }
+        contentHandler.endElement(uri, localName, qName);
+    }
+    
+    private void generateEvents(OMText omText) throws SAXException {
+        char[] ch = omText.getTextCharacters();
+        contentHandler.characters(ch, 0, ch.length);
+    }
+
+    protected static class AttributesAdapter implements Attributes {
+        private OMElement element;
+        private List<OMAttribute> attributes = new ArrayList<OMAttribute>(5);
+
+        public void setAttributes(OMElement element) {
+            this.element = element;
+            attributes.clear();
+            for (Iterator it = element.getAllAttributes(); it.hasNext(); ) {
+                attributes.add((OMAttribute)it.next());
+            }
+        }
+
+        public int getLength() {
+            return attributes.size();
+        }
+
+        public int getIndex(String name) {
+            throw new UnsupportedOperationException();
+        }
+
+        public int getIndex(String uri, String localName) {
+            throw new UnsupportedOperationException();
+        }
+
+        public String getLocalName(int index) {
+            return attributes.get(index).getLocalName();
+        }
+
+        public String getQName(int index) {
+            OMAttribute attribute = attributes.get(index);
+            OMNamespace ns = attribute.getNamespace();
+            if (ns == null) {
+                return attribute.getLocalName();
+            } else {
+                String prefix = ns.getPrefix();
+                if (prefix == null || prefix.length() == 0) {
+                    return attribute.getLocalName();
+                } else {
+                    return ns.getPrefix() + ":" + attribute.getLocalName();
+                }
+            }
+        }
+
+        public String getType(int index) {
+            return attributes.get(index).getAttributeType();
+        }
+
+        public String getType(String name) {
+            throw new UnsupportedOperationException();
+        }
+
+        public String getType(String uri, String localName) {
+            throw new UnsupportedOperationException();
+        }
+
+        public String getURI(int index) {
+            OMNamespace ns = attributes.get(index).getNamespace();
+            return ns == null ? "" : ns.getNamespaceURI();
+        }
+
+        public String getValue(int index) {
+            return attributes.get(index).getAttributeValue();
+        }
+
+        public String getValue(String name) {
+            throw new UnsupportedOperationException();
+        }
+
+        public String getValue(String uri, String localName) {
+            throw new UnsupportedOperationException();
+        }
+    }
+}

Modified: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java?rev=672627&r1=672626&r2=672627&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java (original)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/mediators/transform/XSLTMediatorTest.java Sun Jun 29 07:40:14 2008
@@ -19,7 +19,12 @@
 
 package org.apache.synapse.mediators.transform;
 
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
 import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerFactory;
 
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -34,6 +39,7 @@
 import org.apache.synapse.SynapseException;
 import org.apache.synapse.TestMessageContextBuilder;
 import org.apache.synapse.transport.base.BaseConstants;
+import org.apache.synapse.util.jaxp.AXIOMSourceBuilderFactory;
 import org.apache.synapse.util.jaxp.DOOMResultBuilderFactory;
 import org.apache.synapse.util.jaxp.DOOMSourceBuilderFactory;
 import org.apache.synapse.util.jaxp.SpringStaxSourceBuilderFactory;
@@ -45,7 +51,8 @@
     private static final Class[] sourceBuilderFactories = {
         DOOMSourceBuilderFactory.class,
         SpringStaxSourceBuilderFactory.class,
-        StreamSourceBuilderFactory.class };
+        StreamSourceBuilderFactory.class,
+        AXIOMSourceBuilderFactory.class };
     
     private static final Class[] resultBuilderFactories = {
         DOOMResultBuilderFactory.class,
@@ -61,20 +68,42 @@
         SOURCE +
         "</m:someOtherElement>";
 
-    // Create the test cases for the various source and result builder dynamically:
+    // Create the test cases for the various transformer factories, source builders and
+    // result builders dynamically:
     public static TestSuite suite() {
         TestSuite suite = new TestSuite(XSLTMediatorTest.class);
+        addGenericTests(suite, "Xalan", org.apache.xalan.processor.TransformerFactoryImpl.class);
+        addGenericTests(suite, "Saxon", net.sf.saxon.TransformerFactoryImpl.class);
+        return suite;
+    }
+    
+    private static Set<String> testsToExclude = new HashSet<String>(Arrays.asList(new String[] {
+            "testSaxonDOOMSourceDOOMResult",
+            "testSaxonDOOMSourceStreamResult",
+    }));
+    
+    private static void addGenericTests(TestSuite suite, final String processorName,
+            final Class<? extends TransformerFactory> transformerFactoryClass) {
+        
         for (final Class sbf : sourceBuilderFactories) {
             for (final Class rbf : resultBuilderFactories) {
-                suite.addTest(new TestCase("test" + shortName(sbf) + shortName(rbf)) {
-                    @Override
-                    public void runTest() throws Throwable {
-                        test(sbf, rbf);
-                    }
-                });
+                String testName = "test" + processorName + shortName(sbf) + shortName(rbf);
+                if (!testsToExclude.contains(testName)) {
+                    suite.addTest(new TestCase(testName) {
+                        @Override
+                        public void runTest() throws Throwable {
+                            String oldTransformerFactory =
+                                TransformerFactory.newInstance().getClass().getName();
+                            System.setProperty(TransformerFactory.class.getName(),
+                                    transformerFactoryClass.getName());
+                            test(sbf, rbf);
+                            System.setProperty(TransformerFactory.class.getName(),
+                                    oldTransformerFactory);
+                        }
+                    });
+                }
             }
         }
-        return suite;
     }
     
     private static String shortName(Class clazz) {

Modified: synapse/trunk/java/pom.xml
URL: http://svn.apache.org/viewvc/synapse/trunk/java/pom.xml?rev=672627&r1=672626&r2=672627&view=diff
==============================================================================
--- synapse/trunk/java/pom.xml (original)
+++ synapse/trunk/java/pom.xml Sun Jun 29 07:40:14 2008
@@ -1172,7 +1172,7 @@
         <wso2caching.version>1.6.1</wso2caching.version>
         <wso2throttle.version>1.6</wso2throttle.version>
         <spring.version>1.2.8</spring.version>
-        <springws.version>1.0.3</springws.version>
+        <springws.version>1.5.2</springws.version>
         <xbean.version>2.2.0</xbean.version>
         <bsf.version>3.0-beta2</bsf.version>
         <groovy.version>1.0</groovy.version>