1 22 23 package org.xquark.xml.xqueryevaluator.eval; 24 25 26 public class XPathFilter implements Cloneable { 27 private static final String RCSRevision = "$Revision: 1.1 $"; 28 private static final String RCSName = "$Name: $"; 29 private int index = 0; 30 private short[] skipCounts; 31 XPathExpr xpath; 32 33 public XPathFilter(XPathExpr xpath) { 34 this.xpath = xpath; 35 skipCounts = new short[xpath.getStepCount()]; 36 } 37 38 public boolean isDone() { 39 return index == xpath.getStepCount(); 40 } 41 42 protected Object clone() { 43 XPathFilter result = null; 44 try { 45 result = (XPathFilter) super.clone(); 46 result.skipCounts = new short[skipCounts.length]; 47 for (int i = 0; i < skipCounts.length; i++) { 48 result.skipCounts[i] = skipCounts[i]; 49 } 50 } catch (CloneNotSupportedException ex) {} 51 return result; 52 } 53 54 public boolean pushElement(String namespaceURI, String localName) { 55 if (isDone()) return false; 56 if (xpath.isAnyLevel(index)) { 57 skipCounts[index]++; 58 return true; 59 } 60 if (xpath.isSelf(index)) return false; 61 if (xpath.matchElement(namespaceURI, localName, index)) { 62 index++; 63 skipSelfStep(namespaceURI, localName); 64 return true; 65 } 66 return false; 67 } 68 69 public boolean matchDescendantOrSelfStep(String namespaceURI, String localName) { 70 return xpath.isAnyLevel(index) && (xpath.isSelf(index) || skipCounts[index] > 0) && xpath.matchElement(namespaceURI, localName, index); 71 } 72 73 public XPathFilter fork(String namespaceURI, String localName) { 74 XPathFilter next = (XPathFilter) clone(); 75 if (next.skipCounts[index] > 0) next.skipCounts[index]--; 76 next.index++; 77 next.skipSelfStep(namespaceURI, localName); 78 return next; 79 } 80 81 public void skipSelfStep(String namespaceURI, String localName) { 82 while (!xpath.isAnyLevel(index) && xpath.isSelfElement(index) && xpath.matchElement(namespaceURI, localName, index)) { 83 index++; 84 } 85 } 86 87 public boolean matchSelf(String uri, String localName) { 88 if (isDone()) return false; 89 for (int i = index; i < xpath.getStepCount(); i++) { 90 if (!xpath.isSelf(i) || !xpath.matchElement(uri, localName, i)) 91 return false; 92 } 93 return true; 94 } 95 96 public boolean matchAttribute(String namespaceURI, String localName) { 97 if (isDone()) return false; 98 if (xpath.matchAttribute(namespaceURI, localName, index)) { 99 return true; 100 } 101 return false; 102 } 103 104 public boolean matchText() { 105 if (isDone()) return false; 106 if (!xpath.isAnyLevel(index) && xpath.isSelf(index)) return false; 107 if (xpath.matchText(index)) { 108 for (int i = index+1; i < xpath.getStepCount(); i++) { 109 if (!xpath.isSelf(i) || !xpath.matchText(i)) 110 return false; 111 } 112 return true; 113 } 114 return false; 115 } 116 117 public boolean matchSelfText() { 118 if (isDone()) return false; 119 for (int i = index; i < xpath.getStepCount(); i++) { 120 if (!xpath.isSelf(i) || !xpath.matchText(i)) 121 return false; 122 } 123 return true; 124 } 125 126 public boolean matchSelfNode() { 127 if (isDone()) return false; 128 for (int i = index; i < xpath.getStepCount(); i++) { 129 if (!xpath.isSelf(i) || !xpath.isSelfNode(i)) 130 return false; 131 } 132 return true; 133 } 134 135 public void popElement() { 136 if (!isDone() && skipCounts[index] > 0) skipCounts[index]--; 137 else if (index > 0) index--; 138 } 139 140 } 141 | Popular Tags |