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        @XmlAttribute
043        private String headerName;
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        public String getHeaderName() {
073            return headerName;
074        }
075    
076        public void setHeaderName(String headerName) {
077            this.headerName = headerName;
078        }
079        
080        @Override
081        public Expression createExpression(CamelContext camelContext) {
082            if (resultType == null && type != null) {
083                try {
084                    resultType = camelContext.getClassResolver().resolveMandatoryClass(type);
085                } catch (ClassNotFoundException e) {
086                    throw ObjectHelper.wrapRuntimeCamelException(e);
087                }
088            }
089    
090            return super.createExpression(camelContext);
091        }
092    
093        @Override
094        protected void configureExpression(CamelContext camelContext, Expression expression) {
095            super.configureExpression(camelContext, expression);
096            if (resultType != null) {
097                setProperty(expression, "resultType", resultType);
098            }
099            if (ObjectHelper.isNotEmpty(getHeaderName())) {
100                setProperty(expression, "headerName", getHeaderName());
101            }
102        }
103    
104        @Override
105        protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
106            super.configurePredicate(camelContext, predicate);
107            if (resultType != null) {
108                setProperty(predicate, "resultType", resultType);
109            }
110            if (ObjectHelper.isNotEmpty(getHeaderName())) {
111                setProperty(predicate, "headerName", getHeaderName());
112            }
113        }
114    
115    }