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.ExpressionDefinition;
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
031     * complete the clause continues to another part of the DSL
032     * 
033     * @version $Revision: 775155 $
034     */
035    public class ExpressionClause<T> extends ExpressionDefinition {
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        // Helper expressions
050        // -------------------------------------------------------------------------
051    
052        /**
053         * Specify an {@link Expression} instance
054         */
055        public T expression(Expression expression) {
056            setExpressionValue(expression);
057            return result;
058        }
059    
060        /**
061         * Specify the constant expression value
062         */
063        public T constant(Object value) {
064            return expression(ExpressionBuilder.constantExpression(value));
065        }
066    
067        /**
068         * An expression of the exchange
069         */
070        public T exchange() {
071            return expression(ExpressionBuilder.exchangeExpression());
072        }
073    
074        /**
075         * An expression of an inbound message
076         */
077        public T inMessage() {
078            return expression(ExpressionBuilder.inMessageExpression());
079        }
080    
081        /**
082         * An expression of an inbound message
083         */
084        public T outMessage() {
085            return expression(ExpressionBuilder.outMessageExpression());
086        }
087    
088        /**
089         * An expression of an inbound message body
090         */
091        public T body() {
092            return expression(ExpressionBuilder.bodyExpression());
093        }
094    
095        /**
096         * An expression of an inbound message body converted to the expected type
097         */
098        public T body(Class expectedType) {
099            return expression(ExpressionBuilder.bodyExpression(expectedType));
100        }
101    
102        /**
103         * An expression of an outbound message body
104         */
105        public T outBody() {
106            return expression(ExpressionBuilder.outBodyExpression());
107        }
108    
109        /**
110         * An expression of an outbound message body converted to the expected type
111         */
112        @SuppressWarnings("unchecked")
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 <a
164         * href="http://camel.apache.org/bean-language.html>bean language</a>
165         * which basically means the bean is invoked to determine the expression
166         * value.
167         * 
168         * @param bean the name of the bean looked up the registry
169         * @return the builder to continue processing the DSL
170         */
171        public T method(String bean) {
172            MethodCallExpression expression = new MethodCallExpression(bean);
173            setExpressionType(expression);
174            return result;
175        }
176        
177        /**
178         * Evaluates an expression using the <a
179         * href="http://camel.apache.org/bean-language.html>bean language</a>
180         * which basically means the bean is invoked to determine the expression
181         * value.
182         * 
183         * @param beanType the Class of the bean which we want to invoke
184         * @return the builder to continue processing the DSL
185         */
186        public T method(Class beanType) {
187            MethodCallExpression expression = new MethodCallExpression(beanType);
188            setExpressionType(expression);
189            return result;
190        }
191    
192        /**
193         * Evaluates an expression using the <a
194         * href="http://camel.apache.org/bean-language.html>bean language</a>
195         * which basically means the bean is invoked to determine the expression
196         * value.
197         * 
198         * @param bean the name of the bean looked up the registry
199         * @param method the name of the method to invoke on the bean
200         * @return the builder to continue processing the DSL
201         */
202        public T method(String bean, String method) {
203            MethodCallExpression expression = new MethodCallExpression(bean, method);
204            setExpressionType(expression);
205            return result;
206        }
207        
208        /**
209         * Evaluates an expression using the <a
210         * href="http://camel.apache.org/bean-language.html>bean language</a>
211         * which basically means the bean is invoked to determine the expression
212         * value.
213         * 
214         * @param beanType the Class of the bean which we want to invoke
215         * @param method the name of the method to invoke on the bean
216         * @return the builder to continue processing the DSL
217         */
218        public T method(Class beanType, String method) {
219            MethodCallExpression expression = new MethodCallExpression(beanType, method);
220            setExpressionType(expression);
221            return result;
222        }
223    
224        /**
225         * Evaluates the <a href="http://camel.apache.org/el.html">EL
226         * Language from JSP and JSF</a> using the <a
227         * href="http://camel.apache.org/juel.html">JUEL library</a>
228         * 
229         * @param text the expression to be evaluated
230         * @return the builder to continue processing the DSL
231         */
232        public T el(String text) {
233            return language("el", text);
234        }
235    
236        /**
237         * Evaluates a <a href="http://camel.apache.org/groovy.html">Groovy
238         * expression</a>
239         * 
240         * @param text the expression to be evaluated
241         * @return the builder to continue processing the DSL
242         */
243        public T groovy(String text) {
244            return language("groovy", text);
245        }
246    
247        /**
248         * Evaluates a <a
249         * href="http://camel.apache.org/java-script.html">JavaScript
250         * expression</a>
251         * 
252         * @param text the expression to be evaluated
253         * @return the builder to continue processing the DSL
254         */
255        public T javaScript(String text) {
256            return language("js", text);
257        }
258    
259        /**
260         * Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
261         * 
262         * @param text the expression to be evaluated
263         * @return the builder to continue processing the DSL
264         */
265        public T jxpath(String text) {
266            return language("jxpath", text);
267        }
268    
269        /**
270         * Evaluates an <a href="http://camel.apache.org/ognl.html">OGNL
271         * expression</a>
272         * 
273         * @param text the expression to be evaluated
274         * @return the builder to continue processing the DSL
275         */
276        public T ognl(String text) {
277            return language("ognl", text);
278        }
279    
280        /**
281         * Evaluates a <a href="http://camel.apache.org/mvel.html">MVEL
282         * expression</a>
283         *
284         * @param text the expression to be evaluated
285         * @return the builder to continue processing the DSL
286         */
287        public T mvel(String text) {
288            return language("mvel", text);
289        }
290    
291        /**
292         * Evaluates a <a href="http://camel.apache.org/php.html">PHP
293         * expression</a>
294         * 
295         * @param text the expression to be evaluated
296         * @return the builder to continue processing the DSL
297         */
298        public T php(String text) {
299            return language("php", text);
300        }
301    
302        /**
303         * Evaluates a <a href="http://camel.apache.org/python.html">Python
304         * expression</a>
305         * 
306         * @param text the expression to be evaluated
307         * @return the builder to continue processing the DSL
308         */
309        public T python(String text) {
310            return language("python", text);
311        }
312    
313        /**
314         * Evaluates a <a href="http://camel.apache.org/ruby.html">Ruby
315         * expression</a>
316         * 
317         * @param text the expression to be evaluated
318         * @return the builder to continue processing the DSL
319         */
320        public T ruby(String text) {
321            return language("ruby", text);
322        }
323    
324        /**
325         * Evaluates an <a href="http://camel.apache.org/sql.html">SQL
326         * expression</a>
327         * 
328         * @param text the expression to be evaluated
329         * @return the builder to continue processing the DSL
330         */
331        public T sql(String text) {
332            return language("sql", text);
333        }
334    
335        /**
336         * Evaluates a <a href="http://camel.apache.org/simple.html">Simple
337         * expression</a>
338         * 
339         * @param text the expression to be evaluated
340         * @return the builder to continue processing the DSL
341         */
342        public T simple(String text) {
343            return language("simple", text);
344        }
345    
346        /**
347         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
348         * expression</a>
349         * 
350         * @param text the expression to be evaluated
351         * @return the builder to continue processing the DSL
352         */
353        public T xpath(String text) {
354            return language("xpath", text);
355        }
356    
357        /**
358         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
359         * expression</a> with the specified result type
360         * 
361         * @param text the expression to be evaluated
362         * @param resultType the return type expected by the expressiopn
363         * @return the builder to continue processing the DSL
364         */
365        public T xpath(String text, Class resultType) {
366            XPathExpression expression = new XPathExpression(text);
367            expression.setResultType(resultType);
368            setExpressionType(expression);
369            return result;
370        }
371    
372        /**
373         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
374         * expression</a> with the specified result type and set of namespace
375         * prefixes and URIs
376         * 
377         * @param text the expression to be evaluated
378         * @param resultType the return type expected by the expression
379         * @param namespaces the namespace prefix and URIs to use
380         * @return the builder to continue processing the DSL
381         */
382        public T xpath(String text, Class resultType, Namespaces namespaces) {
383            return xpath(text, resultType, namespaces.getNamespaces());
384        }
385    
386        /**
387         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
388         * expression</a> with the specified result type and set of namespace
389         * prefixes and URIs
390         * 
391         * @param text the expression to be evaluated
392         * @param resultType the return type expected by the expression
393         * @param namespaces the namespace prefix and URIs to use
394         * @return the builder to continue processing the DSL
395         */
396        public T xpath(String text, Class resultType, Map<String, String> namespaces) {
397            XPathExpression expression = new XPathExpression(text);
398            expression.setResultType(resultType);
399            expression.setNamespaces(namespaces);
400            setExpressionType(expression);
401            return result;
402        }
403    
404        /**
405         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
406         * expression</a> with the specified set of namespace prefixes and URIs
407         * 
408         * @param text the expression to be evaluated
409         * @param namespaces the namespace prefix and URIs to use
410         * @return the builder to continue processing the DSL
411         */
412        public T xpath(String text, Namespaces namespaces) {
413            return xpath(text, namespaces.getNamespaces());
414        }
415    
416        /**
417         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
418         * expression</a> with the specified set of namespace prefixes and URIs
419         * 
420         * @param text the expression to be evaluated
421         * @param namespaces the namespace prefix and URIs to use
422         * @return the builder to continue processing the DSL
423         */
424        public T xpath(String text, Map<String, String> namespaces) {
425            XPathExpression expression = new XPathExpression(text);
426            expression.setNamespaces(namespaces);
427            setExpressionType(expression);
428            return result;
429        }
430    
431        /**
432         * Evaluates an <a
433         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
434         * 
435         * @param text the expression to be evaluated
436         * @return the builder to continue processing the DSL
437         */
438        public T xquery(String text) {
439            return language("xquery", text);
440        }
441    
442        /**
443         * Evaluates an <a
444         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
445         * with the specified result type
446         * 
447         * @param text the expression to be evaluated
448         * @param resultType the return type expected by the expressiopn
449         * @return the builder to continue processing the DSL
450         */
451        public T xquery(String text, Class resultType) {
452            XQueryExpression expression = new XQueryExpression(text);
453            expression.setResultType(resultType);
454            setExpressionType(expression);
455            return result;
456        }
457    
458        /**
459         * Evaluates an <a
460         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
461         * with the specified result type and set of namespace prefixes and URIs
462         * 
463         * @param text the expression to be evaluated
464         * @param resultType the return type expected by the expression
465         * @param namespaces the namespace prefix and URIs to use
466         * @return the builder to continue processing the DSL
467         */
468        public T xquery(String text, Class resultType, Namespaces namespaces) {
469            return xquery(text, resultType, namespaces.getNamespaces());
470        }
471    
472        /**
473         * Evaluates an <a
474         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
475         * with the specified result type and set of namespace prefixes and URIs
476         * 
477         * @param text the expression to be evaluated
478         * @param resultType the return type expected by the expression
479         * @param namespaces the namespace prefix and URIs to use
480         * @return the builder to continue processing the DSL
481         */
482        public T xquery(String text, Class resultType, Map<String, String> namespaces) {
483            XQueryExpression expression = new XQueryExpression(text);
484            expression.setResultType(resultType);
485            expression.setNamespaces(namespaces);
486            setExpressionType(expression);
487            return result;
488        }
489    
490        /**
491         * Evaluates an <a
492         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
493         * with the specified set of namespace prefixes and URIs
494         * 
495         * @param text the expression to be evaluated
496         * @param namespaces the namespace prefix and URIs to use
497         * @return the builder to continue processing the DSL
498         */
499        public T xquery(String text, Namespaces namespaces) {
500            return xquery(text, namespaces.getNamespaces());
501        }
502    
503        /**
504         * Evaluates an <a
505         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
506         * with the specified set of namespace prefixes and URIs
507         * 
508         * @param text the expression to be evaluated
509         * @param namespaces the namespace prefix and URIs to use
510         * @return the builder to continue processing the DSL
511         */
512        public T xquery(String text, Map<String, String> namespaces) {
513            XQueryExpression expression = new XQueryExpression(text);
514            expression.setNamespaces(namespaces);
515            setExpressionType(expression);
516            return result;
517        }
518    
519        /**
520         * Evaluates a given language name with the expression text
521         * 
522         * @param language the name of the language
523         * @param expression the expression in the given language
524         * @return the builder to continue processing the DSL
525         */
526        public T language(String language, String expression) {
527            setLanguage(language);
528            setExpression(expression);
529            return result;
530        }
531    
532        // Properties
533        // -------------------------------------------------------------------------
534        public String getLanguage() {
535            return language;
536        }
537    
538        public void setLanguage(String language) {
539            this.language = language;
540        }
541    }