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