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