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    private Boolean threadSafety;
036
037    public Predicate createPredicate(String expression) {
038        expression = loadResource(expression);
039
040        XPathBuilder builder = XPathBuilder.xpath(expression);
041        configureBuilder(builder);
042        return builder;
043    }
044
045    public Expression createExpression(String expression) {
046        expression = loadResource(expression);
047
048        XPathBuilder builder = XPathBuilder.xpath(expression);
049        configureBuilder(builder);
050        return builder;
051    }
052
053    public QName getResultType() {
054        return resultType;
055    }
056
057    public void setResultType(QName resultType) {
058        this.resultType = resultType;
059    }
060
061    public XPathFactory getXpathFactory() {
062        return xpathFactory;
063    }
064
065    public void setXpathFactory(XPathFactory xpathFactory) {
066        this.xpathFactory = xpathFactory;
067    }
068
069    public void setUseSaxon(Boolean useSaxon) {
070        this.useSaxon = useSaxon;
071    }
072
073    public Boolean getUseSaxon() {
074        return useSaxon;
075    }
076
077    public Boolean isUseSaxon() {
078        return useSaxon != null && useSaxon;
079    }
080
081    public String getObjectModelUri() {
082        return objectModelUri;
083    }
084
085    public void setObjectModelUri(String objectModelUri) {
086        this.objectModelUri = objectModelUri;
087    }
088
089    public Boolean getThreadSafety() {
090        return threadSafety;
091    }
092
093    public void setThreadSafety(Boolean threadSafety) {
094        this.threadSafety = threadSafety;
095    }
096
097    protected void configureBuilder(XPathBuilder builder) {
098        if (threadSafety != null) {
099            builder.setThreadSafety(threadSafety);
100        }
101        if (resultType != null) {
102            builder.setResultQName(resultType);
103        }
104
105        if (isUseSaxon()) {
106            builder.enableSaxon();
107        } else {
108            if (xpathFactory != null) {
109                builder.setXPathFactory(xpathFactory);
110            }
111            if (objectModelUri != null) {
112                builder.setObjectModelUri(objectModelUri);
113            }
114        }
115    }
116
117    public boolean isSingleton() {
118        return false;
119    }
120}