You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/04/28 10:57:45 UTC

svn commit: r397808 - in /incubator/servicemix/trunk/servicemix-eip/src: main/java/org/apache/servicemix/eip/support/ test/java/org/apache/servicemix/eip/ test/java/org/apache/servicemix/eip/support/ test/resources/org/apache/servicemix/eip/ test/resou...

Author: gnodet
Date: Fri Apr 28 01:57:42 2006
New Revision: 397808

URL: http://svn.apache.org/viewcvs?rev=397808&view=rev
Log:
SM-415: Add a simple NamespaceContext implementation easily configurable from xbean

Added:
    incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/NamespaceContextImpl.java
    incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/support/
    incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/support/NamespaceContextImplTest.java
    incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/support/
    incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/support/nscontext.xml
Modified:
    incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/SpringConfigurationTest.java
    incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/spring.xml

Added: incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/NamespaceContextImpl.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/NamespaceContextImpl.java?rev=397808&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/NamespaceContextImpl.java (added)
+++ incubator/servicemix/trunk/servicemix-eip/src/main/java/org/apache/servicemix/eip/support/NamespaceContextImpl.java Fri Apr 28 01:57:42 2006
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.eip.support;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+/**
+ * A simple namespace context with a clean xbean configuration.
+ *
+ * @org.apache.xbean.XBean element="namespace-context"
+ *                         description="A NamespaceContext implementation"
+ * @author gnodet
+ * @version $Revision: 397796 $
+ */
+public class NamespaceContextImpl implements NamespaceContext {
+
+    /**
+     * map containing bound namespaces, keyed by their prefix. A LinkedHashMap
+     * is used to ensure that {@link #getPrefix(String)} always returns the same
+     * prefix, unless that prefix is removed.
+     */
+    private Map namespaces = new LinkedHashMap();
+    
+    /**
+     * Constructs a SimpleNamespaceContext with no parent context or namespace
+     * declarations.
+     */
+    public NamespaceContextImpl() {
+    }
+    
+    /**
+     * Constructs a SimpleNamespaceContext with no parent context that contains
+     * the specified prefixes.
+     * 
+     * @param namespaces A Map of namespace URIs, keyed by their prefixes.
+     */
+    public NamespaceContextImpl(Map namespaces) {
+        setNamespaces(namespaces);
+    }
+    
+    /**
+     * @org.apache.xbean.Map entryName="namespace" keyName="prefix"
+     * @return Returns the namespaces.
+     */
+    public Map getNamespaces() {
+        return namespaces;
+    }
+
+    /**
+     * @param namespaces The namespaces to set.
+     */
+    public void setNamespaces(Map namespaces) {
+        this.namespaces.clear();
+        if (namespaces != null) {
+            this.namespaces.putAll(namespaces);
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
+     */
+    public String getNamespaceURI(String prefix) {
+        if (prefix == null) {
+            throw new IllegalArgumentException("prefix argument was null");
+        } else if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
+            return XMLConstants.XML_NS_URI;
+        } else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
+            return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+        } else if (namespaces.containsKey(prefix)) {
+            String uri = (String) namespaces.get(prefix);
+            if (uri.length() == 0) {
+                return null;
+            } else {
+                return uri;
+            }
+        } else {
+            return null;
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
+     */
+    public String getPrefix(String nsURI) {
+        if (nsURI == null) {
+            throw new IllegalArgumentException("nsURI was null");
+        } else if (nsURI.length() == 0) {
+            throw new IllegalArgumentException("nsURI was empty");
+        } else if (nsURI.equals(XMLConstants.XML_NS_URI)) {
+            return XMLConstants.XML_NS_PREFIX;
+        } else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
+            return XMLConstants.XMLNS_ATTRIBUTE;
+        }
+        Iterator iter = namespaces.entrySet().iterator();
+        while (iter.hasNext()) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            String uri = (String) entry.getValue();
+            if (uri.equals(nsURI)) {
+                return (String) entry.getKey();
+            }
+        }
+        if (nsURI.length() == 0) {
+            return "";
+        } else {
+            return null;
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
+     */
+    public Iterator getPrefixes(String nsURI) {
+        if (nsURI == null) {
+            throw new IllegalArgumentException("nsURI was null");
+        } else if (nsURI.length() == 0) {
+            throw new IllegalArgumentException("nsURI was empty");
+        } else if (nsURI.equals(XMLConstants.XML_NS_URI)) {
+            return Collections.singleton(XMLConstants.XML_NS_PREFIX).iterator();
+        } else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
+            return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE).iterator();
+        }
+        Set prefixes = null;
+        Iterator iter = namespaces.entrySet().iterator();
+        while (iter.hasNext()) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            String uri = (String) entry.getValue();
+            if (uri.equals(nsURI)) {
+                if (prefixes == null) {
+                    prefixes = new HashSet();
+                }
+                prefixes.add(entry.getKey());
+            }
+        }
+        if (prefixes != null) {
+            return Collections.unmodifiableSet(prefixes).iterator();
+        } else if (nsURI.length() == 0) {
+            return Collections.singleton("").iterator();
+        } else {
+            return Collections.EMPTY_LIST.iterator();
+        }
+    }
+    
+}

Modified: incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/SpringConfigurationTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/SpringConfigurationTest.java?rev=397808&r1=397807&r2=397808&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/SpringConfigurationTest.java (original)
+++ incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/SpringConfigurationTest.java Fri Apr 28 01:57:42 2006
@@ -35,7 +35,7 @@
         ServiceMixClient client = new DefaultServiceMixClient(jbi, as);
         InOnly me = client.createInOnlyExchange();
         me.setService(new QName("http://test", "wireTap"));
-        me.getInMessage().setContent(new StringSource("<test><echo/><world/><earth/></test>"));
+        me.getInMessage().setContent(new StringSource("<test xmlns=\"http://test\"><echo/><world/><earth/></test>"));
         client.sendSync(me);
         
         ((Receiver) getBean("trace1")).getMessageList().assertMessagesReceived(1);

Added: incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/support/NamespaceContextImplTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/support/NamespaceContextImplTest.java?rev=397808&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/support/NamespaceContextImplTest.java (added)
+++ incubator/servicemix/trunk/servicemix-eip/src/test/java/org/apache/servicemix/eip/support/NamespaceContextImplTest.java Fri Apr 28 01:57:42 2006
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.servicemix.eip.support;
+
+import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
+import org.springframework.context.support.AbstractApplicationContext;
+
+import junit.framework.TestCase;
+
+public class NamespaceContextImplTest extends TestCase {
+
+    public void testNsContext() throws Exception {
+        AbstractApplicationContext springContext = new ClassPathXmlApplicationContext("org/apache/servicemix/eip/support/nscontext.xml");
+        Object objNsContext = springContext.getBean("nsContext");
+        assertNotNull(objNsContext);
+        assertTrue(objNsContext instanceof NamespaceContextImpl);
+        NamespaceContextImpl nsContext = (NamespaceContextImpl) objNsContext;
+        assertEquals("urn:test", nsContext.getNamespaceURI("test"));
+        assertEquals("test", nsContext.getPrefix("urn:test"));
+    }
+    
+}

Modified: incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/spring.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/spring.xml?rev=397808&r1=397807&r2=397808&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/spring.xml (original)
+++ incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/spring.xml Fri Apr 28 01:57:42 2006
@@ -29,10 +29,11 @@
               <!-- END SNIPPET: wire-tap -->
               
               <!-- START SNIPPET: xpath-splitter -->
-              <eip:xpath-splitter service="test:xpathSplitter" endpoint="endpoint" xpath="/*/*">
+              <eip:xpath-splitter service="test:xpathSplitter" endpoint="endpoint" 
+                                  xpath="/*/*" namespaceContext="#nsContext">
                 <eip:target>
                   <eip:exchange-target service="test:router" />
-                </eip:target>
+                </eip:target>
               </eip:xpath-splitter>
               <!-- END SNIPPET: xpath-splitter -->
               
@@ -41,7 +42,7 @@
                 <eip:rules>
                   <eip:routing-rule>
                     <eip:predicate>
-                      <eip:xpath-predicate xpath="count(/echo) = 1" />
+                      <eip:xpath-predicate xpath="count(/test:echo) = 1" namespaceContext="#nsContext" />
                     </eip:predicate>
                     <eip:target>
                       <eip:exchange-target service="test:pipeline" />
@@ -92,7 +93,7 @@
                   <eip:exchange-target service="test:trace3" />
                 </eip:target>
                 <eip:filter>
-                  <eip:xpath-predicate xpath="count(/world) = 1" />
+                  <eip:xpath-predicate xpath="count(/test:world) = 1" namespaceContext="#nsContext"/>
                 </eip:filter>
               </eip:message-filter>
               <!-- END SNIPPET: message-filter -->
@@ -130,6 +131,14 @@
       </sm:activationSpec>
       
     </sm:activationSpecs>
-  </sm:container>
+  </sm:container>
+  
+  <!-- START SNIPPET: namespace-context -->
+  <eip:namespace-context id="nsContext">
+    <eip:namespaces>
+      <eip:namespace prefix="test">http://test</eip:namespace>
+    </eip:namespaces>
+  </eip:namespace-context>
+  <!-- END SNIPPET: namespace-context -->
 
 </beans>

Added: incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/support/nscontext.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/support/nscontext.xml?rev=397808&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/support/nscontext.xml (added)
+++ incubator/servicemix/trunk/servicemix-eip/src/test/resources/org/apache/servicemix/eip/support/nscontext.xml Fri Apr 28 01:57:42 2006
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:eip="http://servicemix.apache.org/eip/1.0">
+
+  <eip:namespace-context id="nsContext">
+    <eip:namespaces>
+      <eip:namespace prefix="test">urn:test</eip:namespace>
+    </eip:namespaces>
+  </eip:namespace-context>
+
+</beans>