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: 750806 $
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 bean the name of the bean looked up the registry
184         * @param method the name of the method to invoke on the bean
185         * @return the builder to continue processing the DSL
186         */
187        public T method(String bean, String method) {
188            MethodCallExpression expression = new MethodCallExpression(bean, method);
189            setExpressionType(expression);
190            return result;
191        }
192    
193        /**
194         * Evaluates the <a href="http://camel.apache.org/el.html">EL
195         * Language from JSP and JSF</a> using the <a
196         * href="http://camel.apache.org/juel.html">JUEL library</a>
197         * 
198         * @param text the expression to be evaluated
199         * @return the builder to continue processing the DSL
200         */
201        public T el(String text) {
202            return language("el", text);
203        }
204    
205        /**
206         * Evaluates a <a href="http://camel.apache.org/groovy.html">Groovy
207         * expression</a>
208         * 
209         * @param text the expression to be evaluated
210         * @return the builder to continue processing the DSL
211         */
212        public T groovy(String text) {
213            return language("groovy", text);
214        }
215    
216        /**
217         * Evaluates a <a
218         * href="http://camel.apache.org/java-script.html">JavaScript
219         * expression</a>
220         * 
221         * @param text the expression to be evaluated
222         * @return the builder to continue processing the DSL
223         */
224        public T javaScript(String text) {
225            return language("js", text);
226        }
227    
228        /**
229         * Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
230         * 
231         * @param text the expression to be evaluated
232         * @return the builder to continue processing the DSL
233         */
234        public T jxpath(String text) {
235            return language("jxpath", text);
236        }
237    
238        /**
239         * Evaluates an <a href="http://camel.apache.org/ognl.html">OGNL
240         * expression</a>
241         * 
242         * @param text the expression to be evaluated
243         * @return the builder to continue processing the DSL
244         */
245        public T ognl(String text) {
246            return language("ognl", text);
247        }
248    
249        /**
250         * Evaluates a <a href="http://camel.apache.org/mvel.html">MVEL
251         * expression</a>
252         *
253         * @param text the expression to be evaluated
254         * @return the builder to continue processing the DSL
255         */
256        public T mvel(String text) {
257            return language("mvel", text);
258        }
259    
260        /**
261         * Evaluates a <a href="http://camel.apache.org/php.html">PHP
262         * expression</a>
263         * 
264         * @param text the expression to be evaluated
265         * @return the builder to continue processing the DSL
266         */
267        public T php(String text) {
268            return language("php", text);
269        }
270    
271        /**
272         * Evaluates a <a href="http://camel.apache.org/python.html">Python
273         * expression</a>
274         * 
275         * @param text the expression to be evaluated
276         * @return the builder to continue processing the DSL
277         */
278        public T python(String text) {
279            return language("python", text);
280        }
281    
282        /**
283         * Evaluates a <a href="http://camel.apache.org/ruby.html">Ruby
284         * expression</a>
285         * 
286         * @param text the expression to be evaluated
287         * @return the builder to continue processing the DSL
288         */
289        public T ruby(String text) {
290            return language("ruby", text);
291        }
292    
293        /**
294         * Evaluates an <a href="http://camel.apache.org/sql.html">SQL
295         * expression</a>
296         * 
297         * @param text the expression to be evaluated
298         * @return the builder to continue processing the DSL
299         */
300        public T sql(String text) {
301            return language("sql", text);
302        }
303    
304        /**
305         * Evaluates a <a href="http://camel.apache.org/simple.html">Simple
306         * expression</a>
307         * 
308         * @param text the expression to be evaluated
309         * @return the builder to continue processing the DSL
310         */
311        public T simple(String text) {
312            return language("simple", text);
313        }
314    
315        /**
316         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
317         * expression</a>
318         * 
319         * @param text the expression to be evaluated
320         * @return the builder to continue processing the DSL
321         */
322        public T xpath(String text) {
323            return language("xpath", text);
324        }
325    
326        /**
327         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
328         * expression</a> with the specified result type
329         * 
330         * @param text the expression to be evaluated
331         * @param resultType the return type expected by the expressiopn
332         * @return the builder to continue processing the DSL
333         */
334        public T xpath(String text, Class resultType) {
335            XPathExpression expression = new XPathExpression(text);
336            expression.setResultType(resultType);
337            setExpressionType(expression);
338            return result;
339        }
340    
341        /**
342         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
343         * expression</a> with the specified result type and set of namespace
344         * prefixes and URIs
345         * 
346         * @param text the expression to be evaluated
347         * @param resultType the return type expected by the expression
348         * @param namespaces the namespace prefix and URIs to use
349         * @return the builder to continue processing the DSL
350         */
351        public T xpath(String text, Class resultType, Namespaces namespaces) {
352            return xpath(text, resultType, namespaces.getNamespaces());
353        }
354    
355        /**
356         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
357         * expression</a> with the specified result type and set of namespace
358         * prefixes and URIs
359         * 
360         * @param text the expression to be evaluated
361         * @param resultType the return type expected by the expression
362         * @param namespaces the namespace prefix and URIs to use
363         * @return the builder to continue processing the DSL
364         */
365        public T xpath(String text, Class resultType, Map<String, String> namespaces) {
366            XPathExpression expression = new XPathExpression(text);
367            expression.setResultType(resultType);
368            expression.setNamespaces(namespaces);
369            setExpressionType(expression);
370            return result;
371        }
372    
373        /**
374         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
375         * expression</a> with the specified set of namespace prefixes and URIs
376         * 
377         * @param text the expression to be evaluated
378         * @param namespaces the namespace prefix and URIs to use
379         * @return the builder to continue processing the DSL
380         */
381        public T xpath(String text, Namespaces namespaces) {
382            return xpath(text, namespaces.getNamespaces());
383        }
384    
385        /**
386         * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
387         * expression</a> with the specified set of namespace prefixes and URIs
388         * 
389         * @param text the expression to be evaluated
390         * @param namespaces the namespace prefix and URIs to use
391         * @return the builder to continue processing the DSL
392         */
393        public T xpath(String text, Map<String, String> namespaces) {
394            XPathExpression expression = new XPathExpression(text);
395            expression.setNamespaces(namespaces);
396            setExpressionType(expression);
397            return result;
398        }
399    
400        /**
401         * Evaluates an <a
402         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
403         * 
404         * @param text the expression to be evaluated
405         * @return the builder to continue processing the DSL
406         */
407        public T xquery(String text) {
408            return language("xquery", text);
409        }
410    
411        /**
412         * Evaluates an <a
413         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
414         * with the specified result type
415         * 
416         * @param text the expression to be evaluated
417         * @param resultType the return type expected by the expressiopn
418         * @return the builder to continue processing the DSL
419         */
420        public T xquery(String text, Class resultType) {
421            XQueryExpression expression = new XQueryExpression(text);
422            expression.setResultType(resultType);
423            setExpressionType(expression);
424            return result;
425        }
426    
427        /**
428         * Evaluates an <a
429         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
430         * with the specified result type and set of namespace prefixes and URIs
431         * 
432         * @param text the expression to be evaluated
433         * @param resultType the return type expected by the expression
434         * @param namespaces the namespace prefix and URIs to use
435         * @return the builder to continue processing the DSL
436         */
437        public T xquery(String text, Class resultType, Namespaces namespaces) {
438            return xquery(text, resultType, namespaces.getNamespaces());
439        }
440    
441        /**
442         * Evaluates an <a
443         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
444         * with the specified result type and set of namespace prefixes and URIs
445         * 
446         * @param text the expression to be evaluated
447         * @param resultType the return type expected by the expression
448         * @param namespaces the namespace prefix and URIs to use
449         * @return the builder to continue processing the DSL
450         */
451        public T xquery(String text, Class resultType, Map<String, String> namespaces) {
452            XQueryExpression expression = new XQueryExpression(text);
453            expression.setResultType(resultType);
454            expression.setNamespaces(namespaces);
455            setExpressionType(expression);
456            return result;
457        }
458    
459        /**
460         * Evaluates an <a
461         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
462         * with the specified set of namespace prefixes and URIs
463         * 
464         * @param text the expression to be evaluated
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, Namespaces namespaces) {
469            return xquery(text, namespaces.getNamespaces());
470        }
471    
472        /**
473         * Evaluates an <a
474         * href="http://camel.apache.org/xquery.html">XQuery expression</a>
475         * with the specified set of namespace prefixes and URIs
476         * 
477         * @param text the expression to be evaluated
478         * @param namespaces the namespace prefix and URIs to use
479         * @return the builder to continue processing the DSL
480         */
481        public T xquery(String text, Map<String, String> namespaces) {
482            XQueryExpression expression = new XQueryExpression(text);
483            expression.setNamespaces(namespaces);
484            setExpressionType(expression);
485            return result;
486        }
487    
488        /**
489         * Evaluates a given language name with the expression text
490         * 
491         * @param language the name of the language
492         * @param expression the expression in the given language
493         * @return the builder to continue processing the DSL
494         */
495        public T language(String language, String expression) {
496            setLanguage(language);
497            setExpression(expression);
498            return result;
499        }
500    
501        // Properties
502        // -------------------------------------------------------------------------
503        public String getLanguage() {
504            return language;
505        }
506    
507        public void setLanguage(String language) {
508            this.language = language;
509        }
510    }