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 */
017package org.apache.camel.model.language;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.Expression;
027import org.apache.camel.Predicate;
028import org.apache.camel.RuntimeCamelException;
029import org.apache.camel.spi.Metadata;
030import org.apache.camel.util.ObjectHelper;
031
032/**
033 * To use XQuery (XML) in Camel expressions or predicates.
034 */
035@Metadata(firstVersion = "1.0.0", label = "language,xml", title = "XQuery")
036@XmlRootElement(name = "xquery")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class XQueryExpression extends NamespaceAwareExpression {
039    @XmlAttribute
040    private String type;
041    @XmlTransient
042    private Class<?> resultType;
043    @XmlAttribute
044    private String headerName;
045
046    public XQueryExpression() {
047    }
048
049    public XQueryExpression(String expression) {
050        super(expression);
051    }
052
053    @Override
054    public String getLanguage() {
055        return "xquery";
056    }
057
058    public String getType() {
059        return type;
060    }
061
062    /**
063     * Sets the class name of the result type (type from output)
064     * <p/>
065     * The default result type is NodeSet
066     */
067    public void setType(String type) {
068        this.type = type;
069    }
070
071    public Class<?> getResultType() {
072        return resultType;
073    }
074
075    /**
076     * Sets the class of the result type (type from output).
077     * <p/>
078     * The default result type is NodeSet
079     */
080    public void setResultType(Class<?> resultType) {
081        this.resultType = resultType;
082    }
083
084    public String getHeaderName() {
085        return headerName;
086    }
087
088    /**
089     * Name of header to use as input, instead of the message body
090     */
091    public void setHeaderName(String headerName) {
092        this.headerName = headerName;
093    }
094
095    @Override
096    public Expression createExpression(CamelContext camelContext) {
097        if (resultType == null && type != null) {
098            try {
099                resultType = camelContext.getClassResolver().resolveMandatoryClass(type);
100            } catch (ClassNotFoundException e) {
101                throw RuntimeCamelException.wrapRuntimeCamelException(e);
102            }
103        }
104
105        return super.createExpression(camelContext);
106    }
107
108    @Override
109    protected void configureExpression(CamelContext camelContext, Expression expression) {
110        if (resultType != null) {
111            setProperty(camelContext, expression, "resultType", resultType);
112        }
113        if (ObjectHelper.isNotEmpty(getHeaderName())) {
114            setProperty(camelContext, expression, "headerName", getHeaderName());
115        }
116        super.configureExpression(camelContext, expression);
117    }
118
119    @Override
120    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
121        if (resultType != null) {
122            setProperty(camelContext, predicate, "resultType", resultType);
123        }
124        if (ObjectHelper.isNotEmpty(getHeaderName())) {
125            setProperty(camelContext, predicate, "headerName", getHeaderName());
126        }
127        super.configurePredicate(camelContext, predicate);
128    }
129
130}