KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > client > XPathHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.client;
18
19 import javax.jbi.messaging.NormalizedMessage;
20 import javax.xml.namespace.NamespaceContext JavaDoc;
21 import javax.xml.namespace.QName JavaDoc;
22 import javax.xml.transform.Source JavaDoc;
23 import javax.xml.xpath.XPath JavaDoc;
24 import javax.xml.xpath.XPathExpressionException JavaDoc;
25 import javax.xml.xpath.XPathFactory JavaDoc;
26 import javax.xml.xpath.XPathFunctionResolver JavaDoc;
27 import javax.xml.xpath.XPathVariableResolver JavaDoc;
28
29 import java.util.Map JavaDoc;
30
31 /**
32  * A helper class for working with XPath and {@link Message} instances.
33  *
34  * @version $Revision: $
35  */

36 public class XPathHelper {
37     private Source JavaDoc content;
38     private XPathFactory JavaDoc xPathFactory;
39     private XPath JavaDoc xPath;
40
41     public XPathHelper() {
42     }
43
44     public XPathHelper(NormalizedMessage message) {
45         setMessage(message);
46     }
47
48     public XPathHelper(Source JavaDoc content) {
49         setContent(content);
50     }
51
52     public XPathHelper(NormalizedMessage message, Map JavaDoc namespaces) {
53         this(message);
54         setNamespaces(namespaces);
55     }
56
57     public XPathHelper(NormalizedMessage message, NamespaceContext JavaDoc namespaces) {
58         this(message);
59         setNamespaceContext(namespaces);
60     }
61
62     public Object JavaDoc evaluate(String JavaDoc expression, QName JavaDoc arg2) throws XPathExpressionException JavaDoc {
63         return getXPath().evaluate(expression, getItem(), arg2);
64     }
65
66     public String JavaDoc evaluate(String JavaDoc expression) throws XPathExpressionException JavaDoc {
67         return getXPath().evaluate(expression, getItem());
68     }
69
70     public void reset() {
71         if (xPath != null) {
72             getXPath().reset();
73         }
74     }
75
76     // Properties
77
// -------------------------------------------------------------------------
78
public void setMessage(NormalizedMessage message) {
79         setContent(message.getContent());
80     }
81
82     public void setContent(Source JavaDoc content) {
83         this.content = content;
84     }
85
86     public NamespaceContext JavaDoc getNamespaceContext() {
87         return getXPath().getNamespaceContext();
88     }
89
90     public XPathFunctionResolver JavaDoc getXPathFunctionResolver() {
91         return getXPath().getXPathFunctionResolver();
92     }
93
94     public XPathVariableResolver JavaDoc getXPathVariableResolver() {
95         return getXPath().getXPathVariableResolver();
96     }
97
98     public void setNamespaceContext(NamespaceContext JavaDoc context) {
99         getXPath().setNamespaceContext(context);
100     }
101
102     public void setXPathFunctionResolver(XPathFunctionResolver JavaDoc resolver) {
103         getXPath().setXPathFunctionResolver(resolver);
104     }
105
106     public void setXPathVariableResolver(XPathVariableResolver JavaDoc resolver) {
107         getXPath().setXPathVariableResolver(resolver);
108     }
109
110     public XPathFactory JavaDoc getXPathFactory() {
111         if (xPathFactory == null) {
112             xPathFactory = XPathFactory.newInstance();
113         }
114         return xPathFactory;
115     }
116
117     public void setXPathFactory(XPathFactory JavaDoc factory) {
118         this.xPathFactory = factory;
119     }
120
121     public Source JavaDoc getContent() {
122         return content;
123     }
124
125     public XPath JavaDoc getXPath() {
126         if (xPath == null) {
127             xPath = getXPathFactory().newXPath();
128         }
129         return xPath;
130     }
131
132     /**
133      * Sets the namespace context to the given map where the keys are namespace
134      * prefixes and the values are the URIs
135      */

136     public void setNamespaces(Map JavaDoc namespaces) {
137         setNamespaceContext(new DefaultNamespaceContext(getNamespaceContext(), namespaces));
138
139     }
140
141     // Implementation methods
142
// -------------------------------------------------------------------------
143
protected Object JavaDoc getItem() {
144         return content;
145     }
146 }
147
Popular Tags