KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > expression > ExpressionHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.expression;
18
19 import javax.jbi.messaging.MessageExchange;
20 import javax.jbi.messaging.MessagingException;
21 import javax.jbi.messaging.NormalizedMessage;
22
23 /**
24  * A helper class for working with expressions.
25  *
26  * @version $Revision: 426415 $
27  */

28 public class ExpressionHelper {
29
30     /**
31      * Evaluates the given expression as a string value.
32      *
33      * @param expression the expression to evaluate
34      * @param exchange the current exchange
35      * @param message the current message
36      * @param defaultValue the default value to use if the expression is null or the value of the expression is null
37      * @return the value of the expression as a string if it is not null or the defaultValue
38      * @throws MessagingException if the expression failed to be evaluated
39      */

40     public static String JavaDoc asString(Expression expression, MessageExchange exchange, NormalizedMessage message, String JavaDoc defaultValue) throws MessagingException {
41         if (expression != null) {
42             Object JavaDoc answer = expression.evaluate(exchange, message);
43             if (answer != null) {
44                 return answer.toString();
45             }
46         }
47         return defaultValue;
48     }
49 }
50
Popular Tags