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.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.Expression;
027    import org.apache.camel.Predicate;
028    import org.apache.camel.util.ObjectHelper;
029    
030    /**
031     * For XQuery expressions and predicates
032     *
033     * @version 
034     */
035    @XmlRootElement(name = "xquery")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class XQueryExpression extends NamespaceAwareExpression {
038        @XmlAttribute
039        private String type;
040        @XmlTransient
041        private Class<?> resultType;
042    
043        public XQueryExpression() {
044        }
045    
046        public XQueryExpression(String expression) {
047            super(expression);
048        }
049    
050        public String getLanguage() {
051            return "xquery";
052        }
053    
054        public String getType() {
055            return type;
056        }
057    
058        public void setType(String type) {
059            this.type = type;
060        }
061    
062        public Class<?> getResultType() {
063            return resultType;
064        }
065    
066        public void setResultType(Class<?> resultType) {
067            this.resultType = resultType;
068        }
069    
070        @Override
071        public Expression createExpression(CamelContext camelContext) {
072            if (resultType == null && type != null) {
073                try {
074                    resultType = camelContext.getClassResolver().resolveMandatoryClass(type);
075                } catch (ClassNotFoundException e) {
076                    throw ObjectHelper.wrapRuntimeCamelException(e);
077                }
078            }
079    
080            return super.createExpression(camelContext);
081        }
082    
083        @Override
084        protected void configureExpression(CamelContext camelContext, Expression expression) {
085            super.configureExpression(camelContext, expression);
086            if (resultType != null) {
087                setProperty(expression, "resultType", resultType);
088            }
089        }
090    
091        @Override
092        protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
093            super.configurePredicate(camelContext, predicate);
094            if (resultType != null) {
095                setProperty(predicate, "resultType", resultType);
096            }
097        }
098    
099    }