1 9 10 package org.ozoneDB.xml.dom4j.xpath; 11 12 import org.dom4j.InvalidXPathException; 13 import org.dom4j.Node; 14 import org.dom4j.XPathException; 15 import org.jaxen.*; 16 import org.jaxen.dom4j.DocumentNavigator; 17 import org.jaxen.pattern.Pattern; 18 import org.jaxen.pattern.PatternParser; 19 import org.ozoneDB.OzoneInterface; 20 import org.ozoneDB.OzoneObject; 21 22 import java.util.ArrayList ; 23 24 31 public class XPathPatternImpl extends OzoneObject implements XPathPattern { 32 33 final static long serialVersionUID = 1L; 34 35 private String text; 36 private Pattern pattern; 37 private Context context; 38 39 public static XPathPattern create(OzoneInterface db, Pattern pattern) { 40 final Class [] signature = new Class [] {Pattern.class}; 41 return (XPathPattern) db.createObject(XPathPatternImpl.class, 42 signature, 43 new Object []{pattern} 44 ); 45 46 } 47 48 public XPathPatternImpl(Pattern pattern) { 49 this.pattern = pattern; 50 this.text = pattern.getText(); 51 this.context = new Context(getContextSupport()); 52 } 53 54 public static XPathPattern create(OzoneInterface db, String text) { 55 final Class [] signature = new Class [] {String .class}; 56 return (XPathPattern) db.createObject(XPathPatternImpl.class, 57 signature, 58 new Object []{text} 59 ); 60 61 } 62 63 public XPathPatternImpl(String text) { 64 this.text = text; 65 this.context = new Context(getContextSupport()); 66 try { 67 this.pattern = PatternParser.parse(text); 68 } catch (Exception e) { 69 throw new InvalidXPathException(text, e.getMessage()); 70 } 71 } 72 73 public boolean matches(Node node) { 74 try { 75 ArrayList list = new ArrayList (1); 76 list.add(node); 77 context.setNodeSet(list); 78 return pattern.matches(node, context); 79 } catch (JaxenException e) { 80 handleJaxenException(e); 81 return false; 82 } 83 } 84 85 public String getText() { 86 return text; 87 } 88 89 90 public double getPriority() { 91 return pattern.getPriority(); 92 } 93 94 public org.dom4j.rule.Pattern[] getUnionPatterns() { 95 Pattern[] patterns = pattern.getUnionPatterns(); 96 if (patterns != null) { 97 int size = patterns.length; 98 XPathPattern[] answer = new XPathPattern[size]; 99 for (int i = 0; i < size; i++) { 100 answer[i] = create(database(), patterns[i]); 101 } 102 return answer; 103 } 104 return null; 105 } 106 107 public short getMatchType() { 108 return pattern.getMatchType(); 109 } 110 111 public String getMatchesNodeName() { 112 return pattern.getMatchesNodeName(); 113 } 114 115 public void setVariableContext(VariableContext variableContext) { 116 context.getContextSupport().setVariableContext(variableContext); 117 } 118 119 120 public String toString() { 121 return "[XPathPatternImpl: text: " + text + " Pattern: " + pattern + "]"; 122 } 123 124 protected ContextSupport getContextSupport() { 125 return new ContextSupport( 126 new SimpleNamespaceContext(), 127 XPathFunctionContext.getInstance(), 128 new SimpleVariableContext(), 129 DocumentNavigator.getInstance() 130 ); 131 } 132 133 protected void handleJaxenException(JaxenException e) throws XPathException { 134 throw new XPathException(text, e); 135 } 136 } 137 138 139 183 | Popular Tags |