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.language.xpath;
018
019import javax.xml.namespace.QName;
020import javax.xml.xpath.XPathFactory;
021
022import org.apache.camel.Expression;
023import org.apache.camel.Predicate;
024import org.apache.camel.builder.xml.XPathBuilder;
025import org.apache.camel.support.LanguageSupport;
026
027/**
028 * XPath language.
029 */
030public class XPathLanguage extends LanguageSupport {
031    private QName resultType;
032    private XPathFactory xpathFactory;
033    private Boolean useSaxon;
034    private String objectModelUri;
035
036    public Predicate createPredicate(String expression) {
037        expression = loadResource(expression);
038
039        XPathBuilder builder = XPathBuilder.xpath(expression);
040        configureBuilder(builder);
041        return builder;
042    }
043
044    public Expression createExpression(String expression) {
045        expression = loadResource(expression);
046
047        XPathBuilder builder = XPathBuilder.xpath(expression);
048        configureBuilder(builder);
049        return builder;
050    }
051
052    public QName getResultType() {
053        return resultType;
054    }
055
056    public void setResultType(QName resultType) {
057        this.resultType = resultType;
058    }
059
060    public XPathFactory getXpathFactory() {
061        return xpathFactory;
062    }
063
064    public void setXpathFactory(XPathFactory xpathFactory) {
065        this.xpathFactory = xpathFactory;
066    }
067
068    public void setUseSaxon(Boolean useSaxon) {
069        this.useSaxon = useSaxon;
070    }
071
072    public Boolean getUseSaxon() {
073        return useSaxon;
074    }
075
076    public Boolean isUseSaxon() {
077        return useSaxon != null && useSaxon;
078    }
079
080    public String getObjectModelUri() {
081        return objectModelUri;
082    }
083
084    public void setObjectModelUri(String objectModelUri) {
085        this.objectModelUri = objectModelUri;
086    }
087
088    protected void configureBuilder(XPathBuilder builder) {
089        if (resultType != null) {
090            builder.setResultQName(resultType);
091        }
092
093        if (isUseSaxon()) {
094            builder.enableSaxon();
095        } else {
096            if (xpathFactory != null) {
097                builder.setXPathFactory(xpathFactory);
098            }
099            if (objectModelUri != null) {
100                builder.setObjectModelUri(objectModelUri);
101            }
102        }
103    }
104
105    public boolean isSingleton() {
106        return false;
107    }
108}