1 17 package org.apache.servicemix.client; 18 19 import javax.jbi.messaging.NormalizedMessage; 20 import javax.xml.namespace.NamespaceContext ; 21 import javax.xml.namespace.QName ; 22 import javax.xml.transform.Source ; 23 import javax.xml.xpath.XPath ; 24 import javax.xml.xpath.XPathExpressionException ; 25 import javax.xml.xpath.XPathFactory ; 26 import javax.xml.xpath.XPathFunctionResolver ; 27 import javax.xml.xpath.XPathVariableResolver ; 28 29 import java.util.Map ; 30 31 36 public class XPathHelper { 37 private Source content; 38 private XPathFactory xPathFactory; 39 private XPath xPath; 40 41 public XPathHelper() { 42 } 43 44 public XPathHelper(NormalizedMessage message) { 45 setMessage(message); 46 } 47 48 public XPathHelper(Source content) { 49 setContent(content); 50 } 51 52 public XPathHelper(NormalizedMessage message, Map namespaces) { 53 this(message); 54 setNamespaces(namespaces); 55 } 56 57 public XPathHelper(NormalizedMessage message, NamespaceContext namespaces) { 58 this(message); 59 setNamespaceContext(namespaces); 60 } 61 62 public Object evaluate(String expression, QName arg2) throws XPathExpressionException { 63 return getXPath().evaluate(expression, getItem(), arg2); 64 } 65 66 public String evaluate(String expression) throws XPathExpressionException { 67 return getXPath().evaluate(expression, getItem()); 68 } 69 70 public void reset() { 71 if (xPath != null) { 72 getXPath().reset(); 73 } 74 } 75 76 public void setMessage(NormalizedMessage message) { 79 setContent(message.getContent()); 80 } 81 82 public void setContent(Source content) { 83 this.content = content; 84 } 85 86 public NamespaceContext getNamespaceContext() { 87 return getXPath().getNamespaceContext(); 88 } 89 90 public XPathFunctionResolver getXPathFunctionResolver() { 91 return getXPath().getXPathFunctionResolver(); 92 } 93 94 public XPathVariableResolver getXPathVariableResolver() { 95 return getXPath().getXPathVariableResolver(); 96 } 97 98 public void setNamespaceContext(NamespaceContext context) { 99 getXPath().setNamespaceContext(context); 100 } 101 102 public void setXPathFunctionResolver(XPathFunctionResolver resolver) { 103 getXPath().setXPathFunctionResolver(resolver); 104 } 105 106 public void setXPathVariableResolver(XPathVariableResolver resolver) { 107 getXPath().setXPathVariableResolver(resolver); 108 } 109 110 public XPathFactory getXPathFactory() { 111 if (xPathFactory == null) { 112 xPathFactory = XPathFactory.newInstance(); 113 } 114 return xPathFactory; 115 } 116 117 public void setXPathFactory(XPathFactory factory) { 118 this.xPathFactory = factory; 119 } 120 121 public Source getContent() { 122 return content; 123 } 124 125 public XPath getXPath() { 126 if (xPath == null) { 127 xPath = getXPathFactory().newXPath(); 128 } 129 return xPath; 130 } 131 132 136 public void setNamespaces(Map namespaces) { 137 setNamespaceContext(new DefaultNamespaceContext(getNamespaceContext(), namespaces)); 138 139 } 140 141 protected Object getItem() { 144 return content; 145 } 146 } 147 | Popular Tags |