1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.xpath.Env; 32 import com.caucho.xpath.ExprEnvironment; 33 import com.caucho.xpath.XPathException; 34 35 import org.w3c.dom.Node ; 36 37 41 public class UnionPattern extends AbstractPattern { 42 private AbstractPattern _left; 43 private AbstractPattern _right; 44 45 public UnionPattern(AbstractPattern left, AbstractPattern right) 46 { 47 super(null); 48 49 if (right.getParent() instanceof FromAttributes && 50 left.getParent() instanceof FromChildren) { 51 _left = right; 52 _right = left; 53 } 54 else { 55 _left = left; 56 _right = right; 57 } 58 } 59 60 68 public boolean match(Node node, ExprEnvironment env) 69 throws XPathException 70 { 71 return (_left.match(node, env) || _right.match(node, env)); 72 } 73 74 77 public boolean isStrictlyAscending() 78 { 79 if (_left.getParent() instanceof FromAttributes && 81 _right.getParent() instanceof FromChildren) 82 return true; 83 else 84 return false; 85 } 86 87 88 97 public NodeIterator createNodeIterator(Node node, ExprEnvironment env, 98 AbstractPattern match) 99 throws XPathException 100 { 101 NodeIterator leftIter = _left.createNodeIterator(node, env, 102 _left.copyPosition()); 103 NodeIterator rightIter = _right.createNodeIterator(node, env, 104 _right.copyPosition()); 105 106 return new UnionIterator(env, leftIter, rightIter); 107 } 108 109 public int position(Node node, Env env, AbstractPattern pattern) 110 throws XPathException 111 { 112 NodeIterator iter = select(node, env); 113 114 int i = 1; 115 while (iter.hasNext()) { 116 if (iter.next() == node) 117 return i; 118 i++; 119 } 120 121 return 0; 122 } 123 124 public int count(Node node, Env env, AbstractPattern pattern) 125 throws XPathException 126 { 127 NodeIterator iter = select(node, env); 128 int count = 0; 129 130 while (iter.hasNext()) { 131 iter.next(); 132 count++; 133 } 134 135 return count; 136 } 137 138 141 public AbstractPattern getLeft() 142 { 143 return _left; 144 } 145 146 149 public AbstractPattern getRight() 150 { 151 return _right; 152 } 153 154 public String toString() 155 { 156 return _left.toString() + "|" + _right.toString(); 158 } 159 } 160 | Popular Tags |