1 19 20 package com.hp.hpl.jena.util.xml; 23 24 25 import java.util.*; 28 29 import org.w3c.dom.*; 30 31 32 40 public class SimpleXMLPathIterator 41 implements Iterator 42 { 43 46 49 52 53 protected List m_stack; 54 55 56 protected SimpleXMLPath m_path; 57 58 59 protected boolean m_prepared = false; 60 61 62 protected Object m_result; 63 64 65 protected int m_len; 66 67 70 public SimpleXMLPathIterator( SimpleXMLPath path, Node node ) { 71 m_path = path; 72 m_len = path.getPathComponents().size(); 73 m_stack = new ArrayList( m_len ); 74 75 m_stack.add( path.getPathComponent( 0 ).getAll( node ) ); 77 prepare(); 78 } 79 80 81 84 85 88 public void remove() { 89 throw new UnsupportedOperationException ( "Cannot remove from SimpleXMLPathIterator" ); 90 } 91 92 95 public boolean hasNext() { 96 prepare(); 97 return m_result != null; 98 } 99 100 103 public Object next() { 104 prepare(); 105 106 if (m_result == null) { 107 throw new NoSuchElementException( "No more values on this SimpleXMLPath" ); 108 } 109 110 m_prepared = false; 111 return m_result; 112 } 113 114 115 118 119 protected void prepare() { 120 if (!m_prepared) { 121 evaluate(); 122 m_prepared = true; 123 } 124 } 125 126 132 protected void evaluate() { 133 int i = 0; 135 m_result = null; 136 137 for (; i < min(m_len, m_stack.size()) && (m_stack.get(i) != null); i++); 139 i--; 140 141 while (i >= 0 && i < min(m_len, m_stack.size())) { 142 Iterator j = (Iterator) m_stack.get( i ); 143 144 if (j == null) { 145 i--; 147 } 148 else if (!j.hasNext()) { 149 m_stack.add( i, null ); 151 m_result = null; 152 i--; 153 } 154 else { 155 m_result = j.next(); 157 i++; 158 159 if (i < m_len) { 160 m_stack.add( i, m_path.getPathComponent( i ).getAll( (Node) m_result ) ); 162 } 163 } 164 } 165 } 166 167 168 169 private int min( int x, int y ) { 170 return (x < y) ? x : y; 171 } 172 173 177 } 178 179 180 206 | Popular Tags |