You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ff...@apache.org on 2008/05/24 12:36:13 UTC

svn commit: r659786 - in /servicemix/smx3/trunk/core/servicemix-core/src: main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java

Author: ffang
Date: Sat May 24 03:36:12 2008
New Revision: 659786

URL: http://svn.apache.org/viewvc?rev=659786&view=rev
Log:
[SM-1358]JAXPXPathExpression subclass returning any kind of object not only String

Added:
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java   (with props)
    servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java   (with props)

Added: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java?rev=659786&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java (added)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java Sat May 24 03:36:12 2008
@@ -0,0 +1,91 @@
+/*
+ * 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.servicemix.expression;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.xpath.XPathConstants;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.xml.DomReader;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.impl.Log4JLogger;
+
+
+/**
+ *
+ * @author Andrew Skiba <sk...@gmail.com>
+ */
+public class JAXPXPathXStreamExpression extends JAXPXPathExpression {
+
+    protected Log logger = new Log4JLogger(JAXPXPathXStreamExpression.class.getName());
+    private XStream xStream;
+
+    public JAXPXPathXStreamExpression() {
+        super();
+    }
+
+    /**
+     * A helper constructor to make a fully created expression.
+     * @param xpath 
+     */
+    public JAXPXPathXStreamExpression(String xpath) {
+        super(xpath);
+    }
+
+
+    @Override
+    public Object evaluate(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
+        Object node = super.evaluate(exchange, message, XPathConstants.NODE);
+        HierarchicalStreamReader streamReader;
+        if (node instanceof Document) {
+            streamReader = new DomReader((Document) node);
+        } else if (node instanceof Element) {
+            streamReader = new DomReader((Element) node);
+        } else {
+            throw new IllegalArgumentException("DOMResult contains neither Document nor Element: " + node.getClass().getName());
+        }
+        return getXStream().unmarshal(streamReader);
+    }
+
+    public XStream getXStream() {
+        if (xStream == null) {
+            xStream = createXStream();
+        }
+        return xStream;
+    }
+
+    public void setXStream(XStream xStream) {
+        this.xStream = xStream;
+    }
+
+    //FIXME: copied as-is from XStreamMarshaler
+    private XStream createXStream() {
+        XStream answer = new XStream();
+        try {
+            answer.alias("invoke", Class.forName("org.logicblaze.lingo.LingoInvocation"));
+        } catch (ClassNotFoundException e) {
+            // Ignore
+        }
+        return answer;
+    }
+}

Propchange: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/expression/JAXPXPathXStreamExpression.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java?rev=659786&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java Sat May 24 03:36:12 2008
@@ -0,0 +1,55 @@
+/*
+ * 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.servicemix.expression;
+
+import java.util.HashMap;
+import java.util.Map;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+import junit.framework.TestCase;
+
+import com.thoughtworks.xstream.XStream;
+
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.messaging.InOnlyImpl;
+import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
+
+
+/**
+ * @version $Revision$
+ */
+public class JAXPXPathXStreamExpressionTest extends TestCase {
+    XStream xStream = new XStream();
+    
+    public void testMap() throws Exception {
+        JAXPXPathXStreamExpression exp = new JAXPXPathXStreamExpression();
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("key1", "value1");
+        exp.setXPath("/header/map");
+        assertExpression(exp, params, "<header>" + xStream.toXML(params) + "</header>");
+    }
+
+    protected void assertExpression(Expression expression, Object expected, String xml) throws MessagingException {
+        InOnlyImpl exchange = new InOnlyImpl("dummy");
+        NormalizedMessage message = new NormalizedMessageImpl(exchange);
+        message.setContent(new StringSource(xml));
+        Object value = expression.evaluate(exchange, message);
+        assertEquals("Expression: " + expression, expected, value);
+    }
+
+}

Propchange: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/expression/JAXPXPathXStreamExpressionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date