001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.builder;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.Expression;
022    import org.apache.camel.builder.xml.Namespaces;
023    import org.apache.camel.model.ExpressionNode;
024    import org.apache.camel.model.language.ExpressionType;
025    import org.apache.camel.model.language.MethodCallExpression;
026    import org.apache.camel.model.language.XPathExpression;
027    import org.apache.camel.model.language.XQueryExpression;
028    
029    /**
030     * Represents an expression clause within the DSL which when the expression is complete
031     * the clause continues to another part of the DSL
032     *
033     * @version $Revision: 1342 $
034     */
035    public class ExpressionClause<T> extends ExpressionType {
036        private T result;
037        private String language;
038    
039        public ExpressionClause(T result) {
040            this.result = result;
041        }
042    
043        public static <T extends ExpressionNode> ExpressionClause<T> createAndSetExpression(T result) {
044            ExpressionClause<T> clause = new ExpressionClause<T>(result);
045            result.setExpression(clause);
046            return clause;
047        }
048    
049    
050        // Helper expressions
051        //-------------------------------------------------------------------------
052    
053        /**
054         * Specify an {@link Expression} instance
055         */
056        public T expression(Expression expression) {
057            setExpressionValue(expression);
058            return result;
059        }
060    
061        /**
062         * Specify the constant expression value
063         */
064        public T constant(Object value) {
065            return expression(ExpressionBuilder.constantExpression(value));
066        }
067    
068        /**
069         * An expression of the exchange
070         */
071        public T exchange() {
072            return expression(ExpressionBuilder.exchangeExpression());
073        }
074    
075        /**
076         * An expression of an inbound message
077         */
078        public T inMessage() {
079            return expression(ExpressionBuilder.inMessageExpression());
080        }
081    
082        /**
083         * An expression of an inbound message
084         */
085        public T outMessage() {
086            return expression(ExpressionBuilder.outMessageExpression());
087        }
088    
089        /**
090         * An expression of an inbound message body
091         */
092        public T body() {
093            return expression(ExpressionBuilder.bodyExpression());
094        }
095    
096        /**
097         * An expression of an inbound message body converted to the expected type
098         */
099        public T body(Class expectedType) {
100            return expression(ExpressionBuilder.bodyExpression(expectedType));
101        }
102    
103        /**
104         * An expression of an outbound message body
105         */
106        public T outBody() {
107            return expression(ExpressionBuilder.outBodyExpression());
108        }
109    
110        /**
111         * An expression of an outbound message body converted to the expected type
112         */
113        public T outBody(Class expectedType) {
114            return expression(ExpressionBuilder.outBodyExpression(expectedType));
115        }
116    
117        /**
118         * An expression of an inbound message header of the given name
119         */
120        public T header(String name) {
121            return expression(ExpressionBuilder.headerExpression(name));
122        }
123    
124        /**
125         * An expression of the inbound headers
126         */
127        public T headers() {
128            return expression(ExpressionBuilder.headersExpression());
129        }
130    
131        /**
132         * An expression of an outbound message header of the given name
133         */
134        public T outHeader(String name) {
135            return expression(ExpressionBuilder.outHeaderExpression(name));
136        }
137    
138        /**
139         * An expression of the outbound headers
140         */
141        public T outHeaders() {
142            return expression(ExpressionBuilder.outHeadersExpression());
143        }
144    
145        /**
146         * An expression of an exchange property of the given name
147         */
148        public T property(String name) {
149            return expression(ExpressionBuilder.propertyExpression(name));
150        }
151    
152        /**
153         * An expression of the exchange properties
154         */
155        public T properties() {
156            return expression(ExpressionBuilder.propertiesExpression());
157        }
158    
159        // Languages
160        //-------------------------------------------------------------------------
161    
162        /**
163         * Evaluates an expression using the
164         * <a href="http://activemq.apache.org/camel/bean-language.html>bean language</a>
165         * which basically means the bean is invoked to determine the expression value.
166         *
167         * @param bean the name of the bean looked up the registry
168         * @return the builder to continue processing the DSL
169         */
170        public T method(String bean) {
171            MethodCallExpression expression = new MethodCallExpression(bean);
172            setExpressionType(expression);
173            return result;
174        }
175    
176        /**
177         * Evaluates an expression using the
178         * <a href="http://activemq.apache.org/camel/bean-language.html>bean language</a>
179         * which basically means the bean is invoked to determine the expression value.
180         *
181         * @param bean   the name of the bean looked up the registry
182         * @param method the name of the method to invoke on the bean
183         * @return the builder to continue processing the DSL
184         */
185        public T method(String bean, String method) {
186            MethodCallExpression expression = new MethodCallExpression(bean, method);
187            setExpressionType(expression);
188            return result;
189        }
190    
191        /**
192         * Evaluates a <a href="http://activemq.apache.org/camel/beanshell.html">BeanShell expression</a>
193         *
194         * @param text the expression to be evaluated
195         * @return the builder to continue processing the DSL
196         * @deprecated use language(<tt>"beanshell"</tt>, text). Will be removed in Camel 2.0.
197         */
198        public T beanShell(String text) {
199            return language("beanshell", text);
200        }
201    
202        /**
203         * Evaluates the  <a href="http://activemq.apache.org/camel/el.html">EL Language from JSP and JSF</a>
204         * using the <a href="http://activemq.apache.org/camel/juel.html">JUEL library</a>
205         *
206         * @param text the expression to be evaluated
207         * @return the builder to continue processing the DSL
208         */
209        public T el(String text) {
210            return language("el", text);
211        }
212    
213        /**
214         * Evaluates a <a href="http://activemq.apache.org/camel/groovy.html">Groovy expression</a>
215         *
216         * @param text the expression to be evaluated
217         * @return the builder to continue processing the DSL
218         */
219        public T groovy(String text) {
220            return language("groovy", text);
221        }
222    
223        /**
224         * Evaluates a <a href="http://activemq.apache.org/camel/java-script.html">JavaScript expression</a>
225         *
226         * @param text the expression to be evaluated
227         * @return the builder to continue processing the DSL
228         */
229        public T javaScript(String text) {
230            return language("js", text);
231        }
232    
233        /**
234         * Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
235         *
236         * @param text the expression to be evaluated
237         * @return the builder to continue processing the DSL
238         */
239        public T jxpath(String text) {
240            return language("jxpath", text);
241        }
242    
243        /**
244         * Evaluates an <a href="http://activemq.apache.org/camel/ognl.html">OGNL expression</a>
245         *
246         * @param text the expression to be evaluated
247         * @return the builder to continue processing the DSL
248         */
249        public T ognl(String text) {
250            return language("ognl", text);
251        }
252    
253        /**
254         * Evaluates a <a href="http://activemq.apache.org/camel/php.html">PHP expression</a>
255         *
256         * @param text the expression to be evaluated
257         * @return the builder to continue processing the DSL
258         */
259        public T php(String text) {
260            return language("php", text);
261        }
262    
263        /**
264         * Evaluates a <a href="http://activemq.apache.org/camel/python.html">Python expression</a>
265         *
266         * @param text the expression to be evaluated
267         * @return the builder to continue processing the DSL
268         */
269        public T python(String text) {
270            return language("python", text);
271        }
272    
273        /**
274         * Evaluates a <a href="http://activemq.apache.org/camel/ruby.html">Ruby expression</a>
275         *
276         * @param text the expression to be evaluated
277         * @return the builder to continue processing the DSL
278         */
279        public T ruby(String text) {
280            return language("ruby", text);
281        }
282    
283        /**
284         * Evaluates an <a href="http://activemq.apache.org/camel/sql.html">SQL expression</a>
285         *
286         * @param text the expression to be evaluated
287         * @return the builder to continue processing the DSL
288         */
289        public T sql(String text) {
290            return language("sql", text);
291        }
292    
293        /**
294         * Evaluates a <a href="http://activemq.apache.org/camel/simple.html">Simple expression</a>
295         *
296         * @param text the expression to be evaluated
297         * @return the builder to continue processing the DSL
298         */
299        public T simple(String text) {
300            return language("simple", text);
301        }
302    
303        /**
304         * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
305         *
306         * @param text the expression to be evaluated
307         * @return the builder to continue processing the DSL
308         */
309        public T xpath(String text) {
310            return language("xpath", text);
311        }
312    
313        /**
314         * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
315         * with the specified result type
316         *
317         * @param text       the expression to be evaluated
318         * @param resultType the return type expected by the expressiopn
319         * @return the builder to continue processing the DSL
320         */
321        public T xpath(String text, Class resultType) {
322            XPathExpression expression = new XPathExpression(text);
323            expression.setResultType(resultType);
324            setExpressionType(expression);
325            return result;
326        }
327    
328        /**
329         * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
330         * with the specified result type and set of namespace prefixes and URIs
331         *
332         * @param text       the expression to be evaluated
333         * @param resultType the return type expected by the expression
334         * @param namespaces the namespace prefix and URIs to use
335         * @return the builder to continue processing the DSL
336         */
337        public T xpath(String text, Class resultType, Namespaces namespaces) {
338            return xpath(text, resultType, namespaces.getNamespaces());
339        }
340    
341        /**
342         * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
343         * with the specified result type and set of namespace prefixes and URIs
344         *
345         * @param text       the expression to be evaluated
346         * @param resultType the return type expected by the expression
347         * @param namespaces the namespace prefix and URIs to use
348         * @return the builder to continue processing the DSL
349         */
350        public T xpath(String text, Class resultType, Map<String, String> namespaces) {
351            XPathExpression expression = new XPathExpression(text);
352            expression.setResultType(resultType);
353            expression.setNamespaces(namespaces);
354            setExpressionType(expression);
355            return result;
356        }
357    
358        /**
359         * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
360         * with the specified set of namespace prefixes and URIs
361         *
362         * @param text       the expression to be evaluated
363         * @param namespaces the namespace prefix and URIs to use
364         * @return the builder to continue processing the DSL
365         */
366        public T xpath(String text, Namespaces namespaces) {
367            return xpath(text, namespaces.getNamespaces());
368        }
369    
370        /**
371         * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
372         * with the specified set of namespace prefixes and URIs
373         *
374         * @param text       the expression to be evaluated
375         * @param namespaces the namespace prefix and URIs to use
376         * @return the builder to continue processing the DSL
377         */
378        public T xpath(String text, Map<String, String> namespaces) {
379            XPathExpression expression = new XPathExpression(text);
380            expression.setNamespaces(namespaces);
381            setExpressionType(expression);
382            return result;
383        }
384    
385        /**
386         * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
387         *
388         * @param text the expression to be evaluated
389         * @return the builder to continue processing the DSL
390         */
391        public T xquery(String text) {
392            return language("xquery", text);
393        }
394    
395        /**
396         * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
397         * with the specified result type
398         *
399         * @param text       the expression to be evaluated
400         * @param resultType the return type expected by the expressiopn
401         * @return the builder to continue processing the DSL
402         */
403        public T xquery(String text, Class resultType) {
404            XQueryExpression expression = new XQueryExpression(text);
405            expression.setResultType(resultType);
406            setExpressionType(expression);
407            return result;
408        }
409    
410        /**
411         * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
412         * with the specified result type and set of namespace prefixes and URIs
413         *
414         * @param text       the expression to be evaluated
415         * @param resultType the return type expected by the expression
416         * @param namespaces the namespace prefix and URIs to use
417         * @return the builder to continue processing the DSL
418         */
419        public T xquery(String text, Class resultType, Namespaces namespaces) {
420            return xquery(text, resultType, namespaces.getNamespaces());
421        }
422    
423        /**
424         * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
425         * with the specified result type and set of namespace prefixes and URIs
426         *
427         * @param text       the expression to be evaluated
428         * @param resultType the return type expected by the expression
429         * @param namespaces the namespace prefix and URIs to use
430         * @return the builder to continue processing the DSL
431         */
432        public T xquery(String text, Class resultType, Map<String, String> namespaces) {
433            XQueryExpression expression = new XQueryExpression(text);
434            expression.setResultType(resultType);
435            expression.setNamespaces(namespaces);
436            setExpressionType(expression);
437            return result;
438        }
439    
440        /**
441         * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
442         * with the specified set of namespace prefixes and URIs
443         *
444         * @param text       the expression to be evaluated
445         * @param namespaces the namespace prefix and URIs to use
446         * @return the builder to continue processing the DSL
447         */
448        public T xquery(String text, Namespaces namespaces) {
449            return xquery(text, namespaces.getNamespaces());
450        }
451    
452        /**
453         * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
454         * with the specified set of namespace prefixes and URIs
455         *
456         * @param text       the expression to be evaluated
457         * @param namespaces the namespace prefix and URIs to use
458         * @return the builder to continue processing the DSL
459         */
460        public T xquery(String text, Map<String, String> namespaces) {
461            XQueryExpression expression = new XQueryExpression(text);
462            expression.setNamespaces(namespaces);
463            setExpressionType(expression);
464            return result;
465        }
466    
467        /**
468         * Evaluates a given language name with the expression text
469         *
470         * @param language   the name of the language
471         * @param expression the expression in the given language
472         * @return the builder to continue processing the DSL
473         */
474        public T language(String language, String expression) {
475            setLanguage(language);
476            setExpression(expression);
477            return result;
478        }
479    
480        // Properties
481        //-------------------------------------------------------------------------
482        public String getLanguage() {
483            return language;
484        }
485    
486        public void setLanguage(String language) {
487            this.language = language;
488        }
489    }