1 4 package gnu.kawa.xml; 5 import gnu.lists.*; 6 import gnu.xml.*; 7 8 10 import gnu.mapping.*; 11 12 public abstract class KNode extends SeqPosition 13 implements 14 15 17 Consumable 18 { 19 20 public KNode (NodeTree seq, int ipos) 21 { 22 super(seq, ipos); 23 } 24 25 public static Object atomicValue (Object value) 26 { 27 if (value instanceof KNode) 28 { 29 KNode node = (KNode) value; 30 return ((NodeTree) node.sequence).typedValue(node.ipos); 31 } 32 return value; 33 } 34 35 36 public static KNode coerce (Object value) 37 { 38 if (value instanceof KNode) 39 return (KNode) value; 40 if (value instanceof NodeTree) 41 { 42 NodeTree ntree = (NodeTree) value; 43 return make(ntree, ntree.startPos()); 44 } 45 if (value instanceof SeqPosition 46 && ! (value instanceof TreePosition)) 47 { 48 SeqPosition seqp = (SeqPosition) value; 49 if (seqp.sequence instanceof NodeTree) 50 return make((NodeTree) seqp.sequence, seqp.ipos); 51 } 52 return null; 53 } 54 55 public static KNode make(NodeTree seq, int ipos) 56 { 57 int index = seq.posToDataIndex(ipos); 58 while (index < seq.data.length 59 && seq.data[index] == TreeList.BEGIN_ENTITY) 60 { 61 index += TreeList.BEGIN_ENTITY_SIZE; 62 if (index == seq.gapStart) 63 index = seq.gapEnd; 64 ipos = index << 1; 65 } 66 int kind = seq.getNextKindI(seq.posToDataIndex(ipos)); 67 switch (kind) 68 { 69 case Sequence.GROUP_VALUE: 70 return new KElement(seq, ipos); 71 case Sequence.ATTRIBUTE_VALUE: 72 return new KAttr(seq, ipos); 73 case Sequence.DOCUMENT_VALUE: 74 return new KDocument(seq, ipos); 75 case Sequence.CDATA_VALUE: 76 return new KCDATASection(seq, ipos); 77 case Sequence.COMMENT_VALUE: 78 return new KComment(seq, ipos); 79 case Sequence.PROCESSING_INSTRUCTION_VALUE: 80 return new KProcessingInstruction(seq, ipos); 81 case Sequence.EOF_VALUE: 82 if (! seq.isEmpty()) 83 return null; 84 default: 86 return new KText(seq, ipos); 87 } 88 } 89 90 91 96 public SeqPosition copy () 97 { 98 return make((NodeTree) sequence, sequence.copyPos(getPos())); 99 } 100 101 102 public static KNode make(NodeTree seq) 103 { 104 return make(seq, 0); 105 } 106 107 public boolean isSupported(String feature, String version) 108 { 109 return false; 110 } 111 112 113 115 116 public String getNodeName () 117 { 118 return sequence.getNextTypeName(ipos); 119 } 120 121 public String getNamespaceURI () 122 { 123 return ((NodeTree) sequence).posNamespaceURI(ipos); 124 } 125 126 public String getPrefix () 127 { 128 return ((NodeTree) sequence).posPrefix(ipos); 129 } 130 131 public String getLocalName () 132 { 133 return ((NodeTree) sequence).posLocalName(ipos); 134 } 135 136 public static String getNodeValue (NodeTree seq, int ipos) 137 { 138 StringBuffer sbuf = new StringBuffer (); 139 getNodeValue(seq, ipos, sbuf); 140 return sbuf.toString(); 141 } 142 143 public static void getNodeValue (NodeTree seq, int ipos, StringBuffer sbuf) 144 { 145 seq.stringValue(seq.posToDataIndex(ipos), sbuf); 146 } 147 148 public String getNodeValue() 149 { 150 StringBuffer sbuf = new StringBuffer (); 151 getNodeValue(sbuf); 152 return sbuf.toString(); 153 } 154 155 public void getNodeValue (StringBuffer sbuf) 156 { 157 getNodeValue((NodeTree) sequence, ipos, sbuf); 158 } 159 160 public boolean hasChildNodes() 161 { 162 return ((NodeTree) sequence).posFirstChild(ipos) >= 0; 163 } 164 165 public String getTextContent () 166 { 167 StringBuffer sbuf = new StringBuffer (); 168 getTextContent(sbuf); 169 return sbuf.toString(); 170 } 171 172 protected void getTextContent (StringBuffer sbuf) 173 { 174 getNodeValue(sbuf); 176 } 177 178 179 187 208 209 210 217 223 235 247 264 265 266 273 280 288 296 304 312 320 330 338 343 344 public void normalize () 345 { 346 } 347 348 public boolean hasAttributes () 349 { 350 return false; 351 } 352 353 public boolean isDefaultNamespace (String namespaceURI) 354 { 355 return ((NodeTree) sequence).posIsDefaultNamespace(ipos, namespaceURI); 356 } 357 358 public String lookupNamespaceURI (String prefix) 359 { 360 return ((NodeTree) sequence).posLookupNamespaceURI(ipos, prefix); 361 } 362 363 public String lookupPrefix (String namespaceURI) 364 { 365 return ((NodeTree) sequence).posLookupPrefix(ipos, namespaceURI); 366 } 367 368 public String getBaseURI () 369 { 370 Object uri = ((NodeTree) sequence).baseUriOfPos(ipos, true); 371 return uri == null ? null : uri.toString(); 372 } 373 374 public Object baseURI () 375 { 376 return ((NodeTree) sequence).baseUriOfPos(ipos, true); 377 } 378 379 380 402 407 408 public String toString () 409 { 410 CharArrayOutPort wr = new CharArrayOutPort(); 411 XMLPrinter xp = new XMLPrinter(wr); 412 ((NodeTree) sequence).consumeNext(ipos, xp); 413 xp.close(); 414 wr.close(); 415 return wr.toString(); 416 } 417 418 public Object getFeature (String feature, String version) 419 { 420 return null; 421 } 422 423 public void consume(Consumer out) 424 { 425 if (out instanceof PositionConsumer) 426 ((PositionConsumer) out).consume(this); 427 else 428 ((NodeTree) sequence).consumeNext(ipos, out); 429 } 430 431 432 437 442 } 443 | Popular Tags |