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.model.language;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlID;
023    import javax.xml.bind.annotation.XmlTransient;
024    import javax.xml.bind.annotation.XmlType;
025    import javax.xml.bind.annotation.XmlValue;
026    import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
027    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028    
029    import org.apache.camel.CamelContext;
030    import org.apache.camel.Exchange;
031    import org.apache.camel.Expression;
032    import org.apache.camel.Predicate;
033    import org.apache.camel.impl.RouteContext;
034    import org.apache.camel.spi.Language;
035    
036    /**
037     * A useful base class for an expression
038     *
039     * @version $Revision: 1.1 $
040     */
041    @XmlType(name = "expressionType")
042    @XmlAccessorType(XmlAccessType.FIELD)
043    public class ExpressionType {
044        @XmlAttribute
045        @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
046        @XmlID
047        private String id;
048        @XmlValue
049        private String expression;
050        @XmlTransient
051        private Predicate predicate;
052        @XmlTransient
053        private Expression expressionValue;
054    
055        public ExpressionType() {
056        }
057    
058        public ExpressionType(String expression) {
059            this.expression = expression;
060        }
061    
062        public ExpressionType(Predicate predicate) {
063            this.predicate = predicate;
064        }
065    
066        public ExpressionType(Expression expression) {
067            this.expressionValue = expression;
068        }
069    
070        @Override
071        public String toString() {
072            return getLanguage() + "Expression[" + getExpression() + "]";
073        }
074    
075        public String getLanguage() {
076            return "";
077        }
078    
079        public Predicate<Exchange> createPredicate(RouteContext route) {
080            if (predicate == null) {
081                CamelContext camelContext = route.getCamelContext();
082                Language language = camelContext.resolveLanguage(getLanguage());
083                predicate = language.createPredicate(getExpression());
084            }
085            return predicate;
086        }
087    
088        public Expression createExpression(RouteContext routeContext) {
089            if (expressionValue == null) {
090                CamelContext camelContext = routeContext.getCamelContext();
091                Language language = camelContext.resolveLanguage(getLanguage());
092                expressionValue = language.createExpression(getExpression());
093            }
094            return expressionValue;
095        }
096    
097        public String getExpression() {
098            return expression;
099        }
100    
101        public void setExpression(String expression) {
102            this.expression = expression;
103        }
104    
105        /**
106         * Gets the value of the id property.
107         *
108         * @return possible object is
109         *         {@link String }
110         */
111        public String getId() {
112            return id;
113        }
114    
115        /**
116         * Sets the value of the id property.
117         *
118         * @param value allowed object is
119         *              {@link String }
120         */
121        public void setId(String value) {
122            this.id = value;
123        }
124    }