Skip to content

Commit

Permalink
applied patch from Gert for JXPath support with thanks : https://issu…
Browse files Browse the repository at this point in the history
…es.apache.org/activemq/browse/CAMEL-241 I also added the new language into the XSD and DSL

git-svn-id: https://svn.apache.org/repos/asf/activemq/camel/trunk@638833 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jstrachan committed Mar 19, 2008
1 parent 2ebb93f commit a180450
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 7 deletions.
14 changes: 7 additions & 7 deletions apache-camel/pom.xml
Expand Up @@ -84,6 +84,10 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jcr</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
Expand All @@ -94,11 +98,11 @@
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jing</artifactId>
<artifactId>camel-jhc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<artifactId>camel-jing</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
Expand All @@ -112,17 +116,13 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jhc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-juel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jcr</artifactId>
<artifactId>camel-jxpath</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
Expand Down
1 change: 1 addition & 0 deletions apache-camel/src/main/descriptors/unix-bin.xml
Expand Up @@ -99,6 +99,7 @@
<include>org.apache.camel:camel-josql</include>
<include>org.apache.camel:camel-jpa</include>
<include>org.apache.camel:camel-juel</include>
<include>org.apache.camel:camel-jxpath</include>
<include>org.apache.camel:camel-mail</include>
<include>org.apache.camel:camel-msv</include>
<include>org.apache.camel:camel-ognl</include>
Expand Down
1 change: 1 addition & 0 deletions apache-camel/src/main/descriptors/windows-bin.xml
Expand Up @@ -105,6 +105,7 @@
<include>org.apache.camel:camel-josql</include>
<include>org.apache.camel:camel-jpa</include>
<include>org.apache.camel:camel-juel</include>
<include>org.apache.camel:camel-jxpath</include>
<include>org.apache.camel:camel-mail</include>
<include>org.apache.camel:camel-msv</include>
<include>org.apache.camel:camel-ognl</include>
Expand Down
Expand Up @@ -219,6 +219,16 @@ public T javaScript(String text) {
return language("js", text);
}

/**
* Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
*
* @param text the expression to be evaluated
* @return the builder to continue processing the DSL
*/
public T jxpath(String text) {
return language("jxpath", text);
}

/**
* Evaluates an <a href="http://activemq.apache.org/camel/ognl.html">OGNL expression</a>
*
Expand Down
@@ -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.camel.model.language;

import javax.xml.bind.annotation.XmlRootElement;

/**
* For <a href="http://commons.apache.org/jxpath/">JXPath</a> expresions and predicates
*
* @version $Revision$
*/
@XmlRootElement(name = "jxpath")
public class JXPathExpression extends ExpressionType {
public JXPathExpression() {
}

public JXPathExpression(String expression) {
super(expression);
}

public String getLanguage() {
return "jaxpath";
}
}
Expand Up @@ -19,6 +19,7 @@ ExpressionType
GroovyExpression
HeaderExpression
JavaScriptExpression
JXPathExpression
LanguageExpression
MethodCallExpression
OgnlExpression
Expand Down
76 changes: 76 additions & 0 deletions components/camel-jxpath/pom.xml
@@ -0,0 +1,76 @@
<?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.
-->

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.camel</groupId>
<artifactId>camel-parent</artifactId>
<version>1.3-SNAPSHOT</version>
</parent>

<artifactId>camel-jxpath</artifactId>
<packaging>bundle</packaging>
<name>Camel :: JXPath</name>
<description>Camel JXPath support</description>

<properties>
<camel.osgi.export.pkg>org.apache.camel.language.jxpath.*</camel.osgi.export.pkg>
</properties>

<dependencies>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>commons-jxpath</groupId>
<artifactId>commons-jxpath</artifactId>
<version>1.2</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,82 @@
/**
* 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.camel.language.jxpath;

import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.impl.ExpressionSupport;
import org.apache.camel.language.ExpressionEvaluationException;
import org.apache.commons.jxpath.CompiledExpression;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;

/**
* <a href="http://commons.apache.org/jxpath/">JXPath</a> {@link Expression} support
*/
public class JXPathExpression extends ExpressionSupport<Exchange> {

private String expression;
private CompiledExpression compiledExpression;
private final Class<?> type;

/**
* Creates a new JXPathExpression instance
*
* @param expression the JXPath expression to be evaluated
* @param type the expected result type
*/
public JXPathExpression(String expression, Class<?> type) {
super();
this.expression = expression;
this.type = type;
}


public Object evaluate(Exchange exchange) {
try {
Object result = getJXPathExpression().getValue(JXPathContext.newContext(exchange));
assertResultType(exchange, result);
return result;
} catch (JXPathException e) {
throw new ExpressionEvaluationException(this, exchange, e);
}
}

/*
* Check if the result is of the specified type
*/
private void assertResultType(Exchange exchange, Object result) {
if (!type.isAssignableFrom(result.getClass())) {
throw new JXPathException("JXPath result type is " + result.getClass() + " instead of required type " + type);
}
}

@Override
protected String assertionFailureMessage(Exchange exchange) {
return expression.toString();
}

/*
* Get a compiled expression instance for better performance
*/
private synchronized CompiledExpression getJXPathExpression() {
if (compiledExpression == null) {
compiledExpression = JXPathContext.compile(expression);
}
return compiledExpression;
}
}
@@ -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.camel.language.jxpath;

import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.spi.Language;

/**
* <a href="http://commons.apache.org/jxpath/">JXPath</a> {@link Language} provider
*/
public class JXPathLanguage implements Language {

public Expression<Exchange> createExpression(String expression) {
return new JXPathExpression(expression, Object.class);
}

public Predicate<Exchange> createPredicate(String predicate) {
return new JXPathExpression(predicate, Boolean.class);
}

}

0 comments on commit a180450

Please sign in to comment.