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.CamelContext;
022 import org.apache.camel.Expression;
023 import org.apache.camel.builder.xml.Namespaces;
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 import org.apache.camel.spi.Language;
029 import org.apache.camel.util.ObjectHelper;
030
031 /**
032 * @version $Revision: 906378 $
033 */
034 public class ExpressionClauseSupport<T> {
035
036 private T result;
037 private String language;
038 private String expression;
039 private Expression expressionValue;
040 private ExpressionDefinition expressionType;
041
042 public ExpressionClauseSupport(T result) {
043 this.result = result;
044 }
045
046 // Helper expressions
047 // -------------------------------------------------------------------------
048
049 /**
050 * Specify an {@link org.apache.camel.Expression} instance
051 */
052 public T expression(Expression expression) {
053 setExpressionValue(expression);
054 return result;
055 }
056
057 /**
058 * Specify the constant expression value
059 */
060 public T constant(Object value) {
061 return expression(ExpressionBuilder.constantExpression(value));
062 }
063
064 /**
065 * An expression of the exchange
066 */
067 public T exchange() {
068 return expression(ExpressionBuilder.exchangeExpression());
069 }
070
071 /**
072 * An expression of an inbound message
073 */
074 public T inMessage() {
075 return expression(ExpressionBuilder.inMessageExpression());
076 }
077
078 /**
079 * An expression of an inbound message
080 */
081 public T outMessage() {
082 return expression(ExpressionBuilder.outMessageExpression());
083 }
084
085 /**
086 * An expression of an inbound message body
087 */
088 public T body() {
089 return expression(ExpressionBuilder.bodyExpression());
090 }
091
092 /**
093 * An expression of an inbound message body converted to the expected type
094 */
095 @SuppressWarnings("unchecked")
096 public T body(Class expectedType) {
097 return expression(ExpressionBuilder.bodyExpression(expectedType));
098 }
099
100 /**
101 * An expression of an outbound message body
102 */
103 public T outBody() {
104 return expression(ExpressionBuilder.outBodyExpression());
105 }
106
107 /**
108 * An expression of an outbound message body converted to the expected type
109 */
110 @SuppressWarnings("unchecked")
111 public T outBody(Class expectedType) {
112 return expression(ExpressionBuilder.outBodyExpression(expectedType));
113 }
114
115 /**
116 * An expression of an inbound message header of the given name
117 */
118 public T header(String name) {
119 return expression(ExpressionBuilder.headerExpression(name));
120 }
121
122 /**
123 * An expression of the inbound headers
124 */
125 public T headers() {
126 return expression(ExpressionBuilder.headersExpression());
127 }
128
129 /**
130 * An expression of an outbound message header of the given name
131 */
132 public T outHeader(String name) {
133 return expression(ExpressionBuilder.outHeaderExpression(name));
134 }
135
136 /**
137 * An expression of the outbound headers
138 */
139 public T outHeaders() {
140 return expression(ExpressionBuilder.outHeadersExpression());
141 }
142
143 /**
144 * An expression of an exchange property of the given name
145 */
146 public T property(String name) {
147 return expression(ExpressionBuilder.propertyExpression(name));
148 }
149
150 /**
151 * An expression of the exchange properties
152 */
153 public T properties() {
154 return expression(ExpressionBuilder.propertiesExpression());
155 }
156
157 // Languages
158 // -------------------------------------------------------------------------
159
160 /**
161 * Evaluates an expression using the <a
162 * href="http://camel.apache.org/bean-language.html>bean language</a>
163 * which basically means the bean is invoked to determine the expression
164 * value.
165 *
166 * @param bean the name of the bean looked up the registry
167 * @return the builder to continue processing the DSL
168 */
169 public T method(String bean) {
170 MethodCallExpression expression = new MethodCallExpression(bean);
171 setExpressionType(expression);
172 return result;
173 }
174
175 /**
176 * Evaluates an expression using the <a
177 * href="http://camel.apache.org/bean-language.html>bean language</a>
178 * which basically means the bean is invoked to determine the expression
179 * value.
180 *
181 * @param instance the instance of the bean
182 * @return the builder to continue processing the DSL
183 */
184 public T method(Object instance) {
185 MethodCallExpression expression = new MethodCallExpression(instance);
186 setExpressionType(expression);
187 return result;
188 }
189
190 /**
191 * Evaluates an expression using the <a
192 * href="http://camel.apache.org/bean-language.html>bean language</a>
193 * which basically means the bean is invoked to determine the expression
194 * value.
195 *
196 * @param beanType the Class of the bean which we want to invoke
197 * @return the builder to continue processing the DSL
198 */
199 public T method(Class<?> beanType) {
200 MethodCallExpression expression = new MethodCallExpression(beanType);
201 setExpressionType(expression);
202 return result;
203 }
204
205 /**
206 * Evaluates an expression using the <a
207 * href="http://camel.apache.org/bean-language.html>bean language</a>
208 * which basically means the bean is invoked to determine the expression
209 * value.
210 *
211 * @param bean the name of the bean looked up the registry
212 * @param method the name of the method to invoke on the bean
213 * @return the builder to continue processing the DSL
214 */
215 public T method(String bean, String method) {
216 MethodCallExpression expression = new MethodCallExpression(bean, method);
217 setExpressionType(expression);
218 return result;
219 }
220
221 /**
222 * Evaluates an expression using the <a
223 * href="http://camel.apache.org/bean-language.html>bean language</a>
224 * which basically means the bean is invoked to determine the expression
225 * value.
226 *
227 * @param instance the instance of the bean
228 * @param method the name of the method to invoke on the bean
229 * @return the builder to continue processing the DSL
230 */
231 public T method(Object instance, String method) {
232 MethodCallExpression expression = new MethodCallExpression(instance, method);
233 setExpressionType(expression);
234 return result;
235 }
236
237 /**
238 * Evaluates an expression using the <a
239 * href="http://camel.apache.org/bean-language.html>bean language</a>
240 * which basically means the bean is invoked to determine the expression
241 * value.
242 *
243 * @param beanType the Class of the bean which we want to invoke
244 * @param method the name of the method to invoke on the bean
245 * @return the builder to continue processing the DSL
246 */
247 public T method(Class<?> beanType, String method) {
248 MethodCallExpression expression = new MethodCallExpression(beanType, method);
249 setExpressionType(expression);
250 return result;
251 }
252
253 /**
254 * Evaluates the <a href="http://camel.apache.org/el.html">EL
255 * Language from JSP and JSF</a> using the <a
256 * href="http://camel.apache.org/juel.html">JUEL library</a>
257 *
258 * @param text the expression to be evaluated
259 * @return the builder to continue processing the DSL
260 */
261 public T el(String text) {
262 return language("el", text);
263 }
264
265 /**
266 * Evaluates a <a href="http://camel.apache.org/groovy.html">Groovy
267 * expression</a>
268 *
269 * @param text the expression to be evaluated
270 * @return the builder to continue processing the DSL
271 */
272 public T groovy(String text) {
273 return language("groovy", text);
274 }
275
276 /**
277 * Evaluates a <a
278 * href="http://camel.apache.org/java-script.html">JavaScript
279 * expression</a>
280 *
281 * @param text the expression to be evaluated
282 * @return the builder to continue processing the DSL
283 */
284 public T javaScript(String text) {
285 return language("js", text);
286 }
287
288 /**
289 * Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
290 *
291 * @param text the expression to be evaluated
292 * @return the builder to continue processing the DSL
293 */
294 public T jxpath(String text) {
295 return language("jxpath", text);
296 }
297
298 /**
299 * Evaluates an <a href="http://camel.apache.org/ognl.html">OGNL
300 * expression</a>
301 *
302 * @param text the expression to be evaluated
303 * @return the builder to continue processing the DSL
304 */
305 public T ognl(String text) {
306 return language("ognl", text);
307 }
308
309 /**
310 * Evaluates a <a href="http://camel.apache.org/mvel.html">MVEL
311 * expression</a>
312 *
313 * @param text the expression to be evaluated
314 * @return the builder to continue processing the DSL
315 */
316 public T mvel(String text) {
317 return language("mvel", text);
318 }
319
320 /**
321 * Evaluates a <a href="http://camel.apache.org/php.html">PHP
322 * expression</a>
323 *
324 * @param text the expression to be evaluated
325 * @return the builder to continue processing the DSL
326 */
327 public T php(String text) {
328 return language("php", text);
329 }
330
331 /**
332 * Evaluates a <a href="http://camel.apache.org/python.html">Python
333 * expression</a>
334 *
335 * @param text the expression to be evaluated
336 * @return the builder to continue processing the DSL
337 */
338 public T python(String text) {
339 return language("python", text);
340 }
341
342 /**
343 * Evaluates a <a href="http://camel.apache.org/ruby.html">Ruby
344 * expression</a>
345 *
346 * @param text the expression to be evaluated
347 * @return the builder to continue processing the DSL
348 */
349 public T ruby(String text) {
350 return language("ruby", text);
351 }
352
353 /**
354 * Evaluates an <a href="http://camel.apache.org/sql.html">SQL
355 * expression</a>
356 *
357 * @param text the expression to be evaluated
358 * @return the builder to continue processing the DSL
359 */
360 public T sql(String text) {
361 return language("sql", text);
362 }
363
364 /**
365 * Evaluates a <a href="http://camel.apache.org/simple.html">Simple
366 * expression</a>
367 *
368 * @param text the expression to be evaluated
369 * @return the builder to continue processing the DSL
370 */
371 public T simple(String text) {
372 return language("simple", text);
373 }
374
375 /**
376 * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
377 * expression</a>
378 *
379 * @param text the expression to be evaluated
380 * @return the builder to continue processing the DSL
381 */
382 public T xpath(String text) {
383 return language("xpath", text);
384 }
385
386 /**
387 * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
388 * expression</a> with the specified result type
389 *
390 * @param text the expression to be evaluated
391 * @param resultType the return type expected by the expression
392 * @return the builder to continue processing the DSL
393 */
394 public T xpath(String text, Class<?> resultType) {
395 XPathExpression expression = new XPathExpression(text);
396 expression.setResultType(resultType);
397 setExpressionType(expression);
398 return result;
399 }
400
401 /**
402 * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
403 * expression</a> with the specified result type and set of namespace
404 * prefixes and URIs
405 *
406 * @param text the expression to be evaluated
407 * @param resultType the return type expected by the expression
408 * @param namespaces the namespace prefix and URIs to use
409 * @return the builder to continue processing the DSL
410 */
411 public T xpath(String text, Class<?> resultType, Namespaces namespaces) {
412 return xpath(text, resultType, namespaces.getNamespaces());
413 }
414
415 /**
416 * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
417 * expression</a> with the specified result type and set of namespace
418 * prefixes and URIs
419 *
420 * @param text the expression to be evaluated
421 * @param resultType the return type expected by the expression
422 * @param namespaces the namespace prefix and URIs to use
423 * @return the builder to continue processing the DSL
424 */
425 public T xpath(String text, Class<?> resultType, Map<String, String> namespaces) {
426 XPathExpression expression = new XPathExpression(text);
427 expression.setResultType(resultType);
428 expression.setNamespaces(namespaces);
429 setExpressionType(expression);
430 return result;
431 }
432
433 /**
434 * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
435 * expression</a> with the specified set of namespace prefixes and URIs
436 *
437 * @param text the expression to be evaluated
438 * @param namespaces the namespace prefix and URIs to use
439 * @return the builder to continue processing the DSL
440 */
441 public T xpath(String text, Namespaces namespaces) {
442 return xpath(text, namespaces.getNamespaces());
443 }
444
445 /**
446 * Evaluates an <a href="http://camel.apache.org/xpath.html">XPath
447 * expression</a> with the specified set of namespace prefixes and URIs
448 *
449 * @param text the expression to be evaluated
450 * @param namespaces the namespace prefix and URIs to use
451 * @return the builder to continue processing the DSL
452 */
453 public T xpath(String text, Map<String, String> namespaces) {
454 XPathExpression expression = new XPathExpression(text);
455 expression.setNamespaces(namespaces);
456 setExpressionType(expression);
457 return result;
458 }
459
460 /**
461 * Evaluates an <a
462 * href="http://camel.apache.org/xquery.html">XQuery expression</a>
463 *
464 * @param text the expression to be evaluated
465 * @return the builder to continue processing the DSL
466 */
467 public T xquery(String text) {
468 return language("xquery", text);
469 }
470
471 /**
472 * Evaluates an <a
473 * href="http://camel.apache.org/xquery.html">XQuery expression</a>
474 * with the specified result type
475 *
476 * @param text the expression to be evaluated
477 * @param resultType the return type expected by the expressiopn
478 * @return the builder to continue processing the DSL
479 */
480 public T xquery(String text, Class<?> resultType) {
481 XQueryExpression expression = new XQueryExpression(text);
482 expression.setResultType(resultType);
483 setExpressionType(expression);
484 return result;
485 }
486
487 /**
488 * Evaluates an <a
489 * href="http://camel.apache.org/xquery.html">XQuery expression</a>
490 * with the specified result type and set of namespace prefixes and URIs
491 *
492 * @param text the expression to be evaluated
493 * @param resultType the return type expected by the expression
494 * @param namespaces the namespace prefix and URIs to use
495 * @return the builder to continue processing the DSL
496 */
497 public T xquery(String text, Class<?> resultType, Namespaces namespaces) {
498 return xquery(text, resultType, namespaces.getNamespaces());
499 }
500
501 /**
502 * Evaluates an <a
503 * href="http://camel.apache.org/xquery.html">XQuery expression</a>
504 * with the specified result type and set of namespace prefixes and URIs
505 *
506 * @param text the expression to be evaluated
507 * @param resultType the return type expected by the expression
508 * @param namespaces the namespace prefix and URIs to use
509 * @return the builder to continue processing the DSL
510 */
511 public T xquery(String text, Class<?> resultType, Map<String, String> namespaces) {
512 XQueryExpression expression = new XQueryExpression(text);
513 expression.setResultType(resultType);
514 expression.setNamespaces(namespaces);
515 setExpressionType(expression);
516 return result;
517 }
518
519 /**
520 * Evaluates an <a
521 * href="http://camel.apache.org/xquery.html">XQuery expression</a>
522 * with the specified set of namespace prefixes and URIs
523 *
524 * @param text the expression to be evaluated
525 * @param namespaces the namespace prefix and URIs to use
526 * @return the builder to continue processing the DSL
527 */
528 public T xquery(String text, Namespaces namespaces) {
529 return xquery(text, namespaces.getNamespaces());
530 }
531
532 /**
533 * Evaluates an <a
534 * href="http://camel.apache.org/xquery.html">XQuery expression</a>
535 * with the specified set of namespace prefixes and URIs
536 *
537 * @param text the expression to be evaluated
538 * @param namespaces the namespace prefix and URIs to use
539 * @return the builder to continue processing the DSL
540 */
541 public T xquery(String text, Map<String, String> namespaces) {
542 XQueryExpression expression = new XQueryExpression(text);
543 expression.setNamespaces(namespaces);
544 setExpressionType(expression);
545 return result;
546 }
547
548 /**
549 * Evaluates a given language name with the expression text
550 *
551 * @param language the name of the language
552 * @param expression the expression in the given language
553 * @return the builder to continue processing the DSL
554 */
555 public T language(String language, String expression) {
556 setLanguage(language);
557 setExpression(expression);
558 return result;
559 }
560
561 // Properties
562 // -------------------------------------------------------------------------
563 public String getLanguage() {
564 return language;
565 }
566
567 public void setLanguage(String language) {
568 this.language = language;
569 }
570
571 public String getExpression() {
572 return expression;
573 }
574
575 public void setExpression(String expression) {
576 this.expression = expression;
577 }
578
579 public Expression getExpressionValue() {
580 return expressionValue;
581 }
582
583 public void setExpressionValue(Expression expressionValue) {
584 this.expressionValue = expressionValue;
585 }
586
587 public ExpressionDefinition getExpressionType() {
588 return expressionType;
589 }
590
591 public void setExpressionType(ExpressionDefinition expressionType) {
592 this.expressionType = expressionType;
593 }
594
595 protected Expression createExpression(CamelContext camelContext) {
596 if (getExpressionValue() == null) {
597 if (getExpressionType() != null) {
598 setExpressionValue(getExpressionType().createExpression(camelContext));
599 } else if (getExpression() != null) {
600 ObjectHelper.notNull("language", getLanguage());
601 Language language = camelContext.resolveLanguage(getLanguage());
602 setExpressionValue(language.createExpression(getExpression()));
603 configureExpression(camelContext, getExpressionValue());
604 }
605 }
606 return getExpressionValue();
607 }
608
609 protected void configureExpression(CamelContext camelContext, Expression expression) {
610 }
611
612 }