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