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.servicemix.client;
018    
019    import java.util.Map;
020    
021    import javax.jbi.messaging.NormalizedMessage;
022    import javax.xml.namespace.NamespaceContext;
023    import javax.xml.namespace.QName;
024    import javax.xml.transform.Source;
025    import javax.xml.xpath.XPath;
026    import javax.xml.xpath.XPathExpressionException;
027    import javax.xml.xpath.XPathFactory;
028    import javax.xml.xpath.XPathFunctionResolver;
029    import javax.xml.xpath.XPathVariableResolver;
030    
031    /**
032     * A helper class for working with XPath and {@link Message} instances.
033     * 
034     * @version $Revision: $
035     */
036    public class XPathHelper {
037    
038        private Source content;
039        private XPathFactory xPathFactory;
040        private XPath xPath;
041    
042        public XPathHelper() {
043        }
044    
045        public XPathHelper(NormalizedMessage message) {
046            this.content = message.getContent();
047        }
048    
049        public XPathHelper(Source content) {
050            this.content = content;
051        }
052    
053        public XPathHelper(NormalizedMessage message, Map namespaces) {
054            this(message);
055            setNamespaces(namespaces);
056        }
057    
058        public XPathHelper(NormalizedMessage message, NamespaceContext namespaces) {
059            this(message);
060            setNamespaceContext(namespaces);
061        }
062    
063        public Object evaluate(String expression, QName arg2) throws XPathExpressionException {
064            return getXPath().evaluate(expression, getItem(), arg2);
065        }
066    
067        public String evaluate(String expression) throws XPathExpressionException {
068            return getXPath().evaluate(expression, getItem());
069        }
070    
071        public void reset() {
072            if (xPath != null) {
073                getXPath().reset();
074            }
075        }
076    
077        // Properties
078        // -------------------------------------------------------------------------
079        public void setMessage(NormalizedMessage message) {
080            setContent(message.getContent());
081        }
082    
083        public void setContent(Source content) {
084            this.content = content;
085        }
086    
087        public final NamespaceContext getNamespaceContext() {
088            return getXPath().getNamespaceContext();
089        }
090    
091        public XPathFunctionResolver getXPathFunctionResolver() {
092            return getXPath().getXPathFunctionResolver();
093        }
094    
095        public XPathVariableResolver getXPathVariableResolver() {
096            return getXPath().getXPathVariableResolver();
097        }
098    
099        public final void setNamespaceContext(NamespaceContext context) {
100            getXPath().setNamespaceContext(context);
101        }
102    
103        public void setXPathFunctionResolver(XPathFunctionResolver resolver) {
104            getXPath().setXPathFunctionResolver(resolver);
105        }
106    
107        public void setXPathVariableResolver(XPathVariableResolver resolver) {
108            getXPath().setXPathVariableResolver(resolver);
109        }
110    
111        public XPathFactory getXPathFactory() {
112            if (xPathFactory == null) {
113                xPathFactory = XPathFactory.newInstance();
114            }
115            return xPathFactory;
116        }
117    
118        public void setXPathFactory(XPathFactory factory) {
119            this.xPathFactory = factory;
120        }
121    
122        public Source getContent() {
123            return content;
124        }
125    
126        public final XPath getXPath() {
127            if (xPath == null) {
128                xPath = getXPathFactory().newXPath();
129            }
130            return xPath;
131        }
132    
133        /**
134         * Sets the namespace context to the given map where the keys are namespace
135         * prefixes and the values are the URIs
136         */
137        public final void setNamespaces(Map namespaces) {
138            setNamespaceContext(new DefaultNamespaceContext(getNamespaceContext(), namespaces));
139        }
140    
141        // Implementation methods
142        // -------------------------------------------------------------------------
143        protected Object getItem() {
144            return content;
145        }
146    }