1 package net.sf.saxon.pull; 2 3 import net.sf.saxon.event.PipelineConfiguration; 4 import net.sf.saxon.om.*; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.AtomicType; 7 import net.sf.saxon.type.Type; 8 import net.sf.saxon.value.AtomicValue; 9 10 import javax.xml.transform.SourceLocator ; 11 12 20 21 public class PullFromIterator implements PullProvider { 22 23 private SequenceIterator base; 24 private PullProvider treeWalker = null; 25 private PipelineConfiguration pipe; 26 private int currentEvent = START_OF_INPUT; 27 28 public PullFromIterator(SequenceIterator base) { 29 this.base = base; 30 } 31 32 36 37 public void setPipelineConfiguration(PipelineConfiguration pipe) { 38 this.pipe = pipe; 39 } 40 41 44 45 public PipelineConfiguration getPipelineConfiguration() { 46 return pipe; 47 } 48 49 55 56 public int next() throws XPathException { 57 if (treeWalker == null) { 58 Item item = base.next(); 59 if (item == null) { 60 currentEvent = END_OF_INPUT; 61 return currentEvent; 62 } else if (item instanceof UnconstructedParent) { 63 treeWalker = ((UnconstructedParent)item).getPuller(); 65 treeWalker.setPipelineConfiguration(pipe); 66 currentEvent = treeWalker.next(); 67 return currentEvent; 68 } else if (item instanceof AtomicValue) { 69 currentEvent = ATOMIC_VALUE; 70 return currentEvent; 71 } else { 72 switch (((NodeInfo)item).getNodeKind()) { 73 case Type.TEXT: 74 currentEvent = TEXT; 75 return currentEvent; 76 77 case Type.COMMENT: 78 currentEvent = COMMENT; 79 return currentEvent; 80 81 case Type.PROCESSING_INSTRUCTION: 82 currentEvent = PROCESSING_INSTRUCTION; 83 return currentEvent; 84 85 case Type.ATTRIBUTE: 86 currentEvent = ATTRIBUTE; 87 return currentEvent; 88 89 case Type.NAMESPACE: 90 currentEvent = NAMESPACE; 91 return currentEvent; 92 93 case Type.ELEMENT: 94 case Type.DOCUMENT: 95 treeWalker = TreeWalker.makeTreeWalker((NodeInfo)item); 96 treeWalker.setPipelineConfiguration(pipe); 97 currentEvent = treeWalker.next(); 98 return currentEvent; 99 100 default: 101 throw new IllegalStateException (); 102 103 } 104 } 105 106 } else { 107 int event = treeWalker.next(); 109 if (event == END_OF_INPUT) { 110 treeWalker = null; 111 currentEvent = next(); 112 } else { 113 currentEvent = event; 114 } 115 return currentEvent; 116 117 } 118 } 119 120 127 128 public int current() { 129 return currentEvent; 130 } 131 132 146 147 public AttributeCollection getAttributes() throws XPathException { 148 if (treeWalker != null) { 149 return treeWalker.getAttributes(); 150 } else { 151 throw new IllegalStateException (); 152 } 153 } 154 155 174 175 public NamespaceDeclarations getNamespaceDeclarations() throws XPathException { 176 if (treeWalker != null) { 177 return treeWalker.getNamespaceDeclarations(); 178 } else { 179 throw new IllegalStateException (); 180 } 181 } 182 183 189 190 public int skipToMatchingEnd() throws XPathException { 191 if (treeWalker != null) { 192 return treeWalker.skipToMatchingEnd(); 193 } else { 194 throw new IllegalStateException (); 195 } 196 } 197 198 205 206 public void close() { 207 if (treeWalker != null) { 208 treeWalker.close(); 209 } 210 } 211 212 224 225 public int getNameCode() { 226 if (treeWalker != null) { 227 return treeWalker.getNameCode(); 228 } else { 229 Item item = base.current(); 230 if (item instanceof NodeInfo) { 231 return ((NodeInfo)item).getNameCode(); 232 } else { 233 throw new IllegalStateException (); 234 } 235 } 236 } 237 238 251 252 public int getFingerprint() { 253 int nc = getNameCode(); 254 if (nc == -1) { 255 return -1; 256 } else { 257 return nc & NamePool.FP_MASK; 258 } 259 } 260 261 275 276 public CharSequence getStringValue() throws XPathException { 277 if (treeWalker != null) { 278 return treeWalker.getStringValue(); 279 } else { 280 Item item = base.current(); 281 return item.getStringValueCS(); 282 } 283 } 284 285 290 291 public AtomicValue getAtomicValue() { 292 if (currentEvent == ATOMIC_VALUE) { 293 return (AtomicValue)base.current(); 294 } else { 295 throw new IllegalStateException (); 296 } 297 } 298 299 307 308 public int getTypeAnnotation() { 309 if (treeWalker != null) { 310 return treeWalker.getTypeAnnotation(); 311 } else { 312 Item item = base.current(); 313 if (item instanceof NodeInfo) { 314 return ((NodeInfo)item).getTypeAnnotation(); 315 } else { 316 return ((AtomicType)((AtomicValue)item).getItemType()).getFingerprint(); 317 } 318 } 319 } 320 321 328 329 public SourceLocator getSourceLocator() { 330 if (treeWalker != null) { 331 return treeWalker.getSourceLocator(); 332 } else { 333 return null; 334 } 335 } 336 } 337 338 339 | Popular Tags |