| 1 |  |  | 
| 2 |  |  | 
| 3 |  |  | 
| 4 |  |  | 
| 5 |  |  | 
| 6 |  |  | 
| 7 |  |  | 
| 8 |  | package org.dom4j.xpath; | 
| 9 |  |  | 
| 10 |  | import java.util.ArrayList; | 
| 11 |  |  | 
| 12 |  | import org.dom4j.InvalidXPathException; | 
| 13 |  | import org.dom4j.Node; | 
| 14 |  | import org.dom4j.XPathException; | 
| 15 |  |  | 
| 16 |  | import org.jaxen.Context; | 
| 17 |  | import org.jaxen.ContextSupport; | 
| 18 |  | import org.jaxen.JaxenException; | 
| 19 |  | import org.jaxen.SimpleNamespaceContext; | 
| 20 |  | import org.jaxen.SimpleVariableContext; | 
| 21 |  | import org.jaxen.VariableContext; | 
| 22 |  | import org.jaxen.XPathFunctionContext; | 
| 23 |  | import org.jaxen.dom4j.DocumentNavigator; | 
| 24 |  | import org.jaxen.pattern.Pattern; | 
| 25 |  | import org.jaxen.pattern.PatternParser; | 
| 26 |  | import org.jaxen.saxpath.SAXPathException; | 
| 27 |  |  | 
| 28 |  |  | 
| 29 |  |  | 
| 30 |  |  | 
| 31 |  |  | 
| 32 |  |  | 
| 33 |  |  | 
| 34 |  |  | 
| 35 |  |  | 
| 36 |  |  | 
| 37 |  | public class XPathPattern implements org.dom4j.rule.Pattern { | 
| 38 |  | private String text; | 
| 39 |  |  | 
| 40 |  | private Pattern pattern; | 
| 41 |  |  | 
| 42 |  | private Context context; | 
| 43 |  |  | 
| 44 | 0 | public XPathPattern(Pattern pattern) { | 
| 45 | 0 | this.pattern = pattern; | 
| 46 | 0 | this.text = pattern.getText(); | 
| 47 | 0 | this.context = new Context(getContextSupport()); | 
| 48 |  | } | 
| 49 |  |  | 
| 50 | 37 | public XPathPattern(String text) { | 
| 51 | 37 | this.text = text; | 
| 52 | 37 | this.context = new Context(getContextSupport()); | 
| 53 |  |  | 
| 54 | 37 | try { | 
| 55 | 37 | this.pattern = PatternParser.parse(text); | 
| 56 |  | } catch (SAXPathException e) { | 
| 57 | 0 | throw new InvalidXPathException(text, e.getMessage()); | 
| 58 |  | } catch (Throwable t) { | 
| 59 | 0 | throw new InvalidXPathException(text, t); | 
| 60 |  | } | 
| 61 |  | } | 
| 62 |  |  | 
| 63 | 47 | public boolean matches(Node node) { | 
| 64 | 47 | try { | 
| 65 | 47 | ArrayList list = new ArrayList(1); | 
| 66 | 47 | list.add(node); | 
| 67 | 47 | context.setNodeSet(list); | 
| 68 |  |  | 
| 69 | 47 | return pattern.matches(node, context); | 
| 70 |  | } catch (JaxenException e) { | 
| 71 | 0 | handleJaxenException(e); | 
| 72 |  |  | 
| 73 | 0 | return false; | 
| 74 |  | } | 
| 75 |  | } | 
| 76 |  |  | 
| 77 | 0 | public String getText() { | 
| 78 | 0 | return text; | 
| 79 |  | } | 
| 80 |  |  | 
| 81 | 22 | public double getPriority() { | 
| 82 | 22 | return pattern.getPriority(); | 
| 83 |  | } | 
| 84 |  |  | 
| 85 | 14 | public org.dom4j.rule.Pattern[] getUnionPatterns() { | 
| 86 | 14 | Pattern[] patterns = pattern.getUnionPatterns(); | 
| 87 |  |  | 
| 88 | 14 | if (patterns != null) { | 
| 89 | 0 | int size = patterns.length; | 
| 90 | 0 | XPathPattern[] answer = new XPathPattern[size]; | 
| 91 |  |  | 
| 92 | 0 | for (int i = 0; i < size; i++) { | 
| 93 | 0 | answer[i] = new XPathPattern(patterns[i]); | 
| 94 |  | } | 
| 95 |  |  | 
| 96 | 0 | return answer; | 
| 97 |  | } | 
| 98 |  |  | 
| 99 | 14 | return null; | 
| 100 |  | } | 
| 101 |  |  | 
| 102 | 14 | public short getMatchType() { | 
| 103 | 14 | return pattern.getMatchType(); | 
| 104 |  | } | 
| 105 |  |  | 
| 106 | 14 | public String getMatchesNodeName() { | 
| 107 | 14 | return pattern.getMatchesNodeName(); | 
| 108 |  | } | 
| 109 |  |  | 
| 110 | 0 | public void setVariableContext(VariableContext variableContext) { | 
| 111 | 0 | context.getContextSupport().setVariableContext(variableContext); | 
| 112 |  | } | 
| 113 |  |  | 
| 114 | 19 | public String toString() { | 
| 115 | 19 | return "[XPathPattern: text: " + text + " Pattern: " + pattern + "]"; | 
| 116 |  | } | 
| 117 |  |  | 
| 118 | 37 | protected ContextSupport getContextSupport() { | 
| 119 | 37 | return new ContextSupport(new SimpleNamespaceContext(), | 
| 120 |  | XPathFunctionContext.getInstance(), | 
| 121 |  | new SimpleVariableContext(), DocumentNavigator.getInstance()); | 
| 122 |  | } | 
| 123 |  |  | 
| 124 | 0 | protected void handleJaxenException(JaxenException exception) | 
| 125 |  | throws XPathException { | 
| 126 | 0 | throw new XPathException(text, exception); | 
| 127 |  | } | 
| 128 |  | } | 
| 129 |  |  | 
| 130 |  |  | 
| 131 |  |  | 
| 132 |  |  | 
| 133 |  |  | 
| 134 |  |  | 
| 135 |  |  | 
| 136 |  |  | 
| 137 |  |  | 
| 138 |  |  | 
| 139 |  |  | 
| 140 |  |  | 
| 141 |  |  | 
| 142 |  |  | 
| 143 |  |  | 
| 144 |  |  | 
| 145 |  |  | 
| 146 |  |  | 
| 147 |  |  | 
| 148 |  |  | 
| 149 |  |  | 
| 150 |  |  | 
| 151 |  |  | 
| 152 |  |  | 
| 153 |  |  | 
| 154 |  |  | 
| 155 |  |  | 
| 156 |  |  | 
| 157 |  |  | 
| 158 |  |  | 
| 159 |  |  | 
| 160 |  |  | 
| 161 |  |  | 
| 162 |  |  | 
| 163 |  |  | 
| 164 |  |  | 
| 165 |  |  |