1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.xpath.ExprEnvironment; 32 import com.caucho.xpath.XPathException; 33 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.NamedNodeMap ; 36 import org.w3c.dom.Node ; 37 38 41 public class AttributeListIterator extends NodeIterator { 42 protected NodeIterator _parentIter; 43 44 protected NamedNodeMap _attributeMap; 45 protected Node _node; 46 protected int _index; 47 protected AbstractPattern _match; 48 49 protected AttributeListIterator(ExprEnvironment env) 50 { 51 super(env); 52 } 53 54 61 public AttributeListIterator(NodeIterator parentIter, 62 ExprEnvironment env, 63 AbstractPattern match) 64 throws XPathException 65 { 66 super(env); 67 68 _parentIter = parentIter; 69 _match = match; 70 71 _node = findFirstMatchingNode(); 72 } 73 74 77 public boolean hasNext() 78 { 79 return _node != null; 80 } 81 82 85 public Node nextNode() 86 throws XPathException 87 { 88 Node node = _node; 89 90 _node = findFirstMatchingNode(); 91 92 return node; 93 } 94 95 98 private Node findFirstMatchingNode() 99 throws XPathException 100 { 101 Node node = null; 102 103 while (true) { 104 Node parent; 105 106 if (node != null && (_match == null || _match.match(node, _env))) { 107 _position++; 108 return node; 109 } 110 111 if (_attributeMap != null && _index < _attributeMap.getLength()) 112 node = _attributeMap.item(_index++); 113 else if (_parentIter == null || 114 (parent = _parentIter.nextNode()) == null) 115 return null; 116 else if (parent instanceof Element ) { 117 _position = 0; 118 _size = 0; 119 _index = 0; 120 _attributeMap = ((Element ) parent).getAttributes(); 121 } 122 } 123 } 124 125 128 public int getContextSize() 129 { 130 if (_attributeMap == null) 131 return 0; 132 else 133 return _attributeMap.getLength(); 134 } 135 136 137 public Object clone() 138 { 139 AttributeListIterator iter = new AttributeListIterator(_env); 140 141 iter.copy(this); 142 143 if (_parentIter != null) 144 iter._parentIter = (NodeIterator) _parentIter.clone(); 145 iter._node = _node; 146 iter._index = _index; 147 iter._attributeMap = _attributeMap; 148 iter._match = _match; 149 150 return iter; 151 } 152 153 public String toString() 154 { 155 return "AttributeListIterator[" + _match + "]"; 156 } 157 } 158 | Popular Tags |