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.Expression;
026    import org.apache.camel.Predicate;
027    import org.apache.camel.impl.RouteContext;
028    import org.apache.camel.model.OutputType;
029    import org.apache.commons.logging.Log;
030    import org.apache.commons.logging.LogFactory;
031    
032    /**
033     * For XQuery expresions and predicates
034     *
035     * @version $Revision: 36321 $
036     */
037    @XmlRootElement(name = "xquery")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class XQueryExpression extends NamespaceAwareExpression {
040        private static final transient Log LOG = LogFactory.getLog(XQueryExpression.class);
041    
042        @XmlAttribute(required = false)
043        private String type;
044        @XmlTransient
045        private Class resultType;
046    
047        public XQueryExpression() {
048        }
049    
050        public XQueryExpression(String expression) {
051            super(expression);
052        }
053    
054        public String getLanguage() {
055            return "xquery";
056        }
057    
058        public String getType() {
059            return type;
060        }
061    
062        public void setType(String type) {
063            this.type = type;
064        }
065    
066        public Class getResultType() {
067            return resultType;
068        }
069    
070        public void setResultType(Class resultType) {
071            this.resultType = resultType;
072        }
073    
074        @Override
075        protected void configureExpression(RouteContext routeContext, Expression expression) {
076            super.configureExpression(routeContext, expression);
077            updateResultType();
078            if (resultType != null) {
079                setProperty(expression, "resultType", resultType);
080            }
081        }
082    
083        @Override
084        protected void configurePredicate(RouteContext routeContext, Predicate predicate) {
085            super.configurePredicate(routeContext, predicate);
086            updateResultType();
087            if (resultType != null) {
088                setProperty(predicate, "resultType", resultType);
089            }
090        }
091    
092        private void updateResultType() {
093            if (resultType == null && type != null) {
094                try {
095                    resultType = Class.forName(type);
096                } catch (ClassNotFoundException e) {
097                    LOG.error("ClassNotFoundException creating class: " + type);
098                }
099            }
100        }
101    }