KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xml > KNode


1 // Copyright (c) 2004, 2005 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.kawa.xml;
5 import gnu.lists.*;
6 import gnu.xml.*;
7 /* #ifdef use:org.w3c.dom.Node */
8 // import org.w3c.dom.*;
9
/* #endif */
10 import gnu.mapping.*;
11
12 public abstract class KNode extends SeqPosition
13   implements
14   /* #ifdef use:org.w3c.dom.Node */
15   // org.w3c.dom.Node,
16
/* #endif */
17   Consumable
18 {
19
20   public KNode (NodeTree seq, int ipos)
21   {
22     super(seq, ipos);
23   }
24
25   public static Object JavaDoc atomicValue (Object JavaDoc 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   /** Convert value to a KNode, returning null if it isn't a node. */
36   public static KNode coerce (Object JavaDoc 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     // .. else fall through to create an empty text node.
85
default:
86     return new KText(seq, ipos);
87       }
88   }
89
90   /* #ifdef JAVA5 */
91   // public KNode copy ()
92
// {
93
// return make((NodeTree) sequence, sequence.copyPos(getPos()));
94
// }
95
/* #else */
96   public SeqPosition copy ()
97   {
98     return make((NodeTree) sequence, sequence.copyPos(getPos()));
99   }
100   /* #endif */
101  
102   public static KNode make(NodeTree seq)
103   {
104     return make(seq, 0);
105   }
106
107   public boolean isSupported(String JavaDoc feature, String JavaDoc version)
108   {
109     return false;
110   }
111
112   /* #ifdef use:org.w3c.dom.Node */
113   // public abstract short getNodeType ();
114
/* #endif */
115
116   public String JavaDoc getNodeName ()
117   {
118     return sequence.getNextTypeName(ipos);
119   }
120
121   public String JavaDoc getNamespaceURI ()
122   {
123     return ((NodeTree) sequence).posNamespaceURI(ipos);
124   }
125
126   public String JavaDoc getPrefix ()
127   {
128     return ((NodeTree) sequence).posPrefix(ipos);
129   }
130
131   public String JavaDoc getLocalName ()
132   {
133     return ((NodeTree) sequence).posLocalName(ipos);
134   }
135
136   public static String JavaDoc getNodeValue (NodeTree seq, int ipos)
137   {
138     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
139     getNodeValue(seq, ipos, sbuf);
140     return sbuf.toString();
141   }
142
143   public static void getNodeValue (NodeTree seq, int ipos, StringBuffer JavaDoc sbuf)
144   {
145     seq.stringValue(seq.posToDataIndex(ipos), sbuf);
146   }
147
148   public String JavaDoc getNodeValue()
149   {
150     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
151     getNodeValue(sbuf);
152     return sbuf.toString();
153   }
154
155   public void getNodeValue (StringBuffer JavaDoc 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 JavaDoc getTextContent ()
166   {
167     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
168     getTextContent(sbuf);
169     return sbuf.toString();
170   }
171
172   protected void getTextContent (StringBuffer JavaDoc sbuf)
173   {
174     // What is the difference between getTextContent and getNodeValue? FIXME.
175
getNodeValue(sbuf);
176   }
177
178   /* #ifdef use:org.w3c.dom.Node */
179   // public Node getParentNode()
180
// {
181
// int parent = sequence.parentPos(ipos);
182
// if (parent == -1)
183
// return null;
184
// return make((NodeTree) sequence, parent);
185
// }
186

187   // public Node getPreviousSibling ()
188
// {
189
// int parent = sequence.parentPos(ipos);
190
// if (parent == -1)
191
// parent = 0;
192
// int index = ((NodeTree) sequence).posToDataIndex(ipos);
193
// int child = sequence.firstChildPos(parent);
194
// int previous = 0;
195
// for (;;)
196
// {
197
// previous = child;
198
// child = sequence.nextPos(child);
199
// if (child == 0)
200
// break;
201
// if (((NodeTree) sequence).posToDataIndex(child) == index)
202
// break;
203
// }
204
// return previous == 0 ? null
205
// : make((NodeTree) sequence, previous);
206
// }
207
/* #endif */
208
209   /* #ifdef use:org.w3c.dom.Node */
210   // public Node getNextSibling ()
211
// {
212
// int next = ((NodeTree) sequence).nextPos(ipos);
213
// return next == 0 ? null
214
// : make((NodeTree) sequence, next);
215
// }
216

217   // public Node getFirstChild()
218
// {
219
// int child = ((NodeTree) sequence).posFirstChild(ipos);
220
// return make((NodeTree) sequence, child);
221
// }
222

223   // public Node getLastChild()
224
// {
225
// int last = 0;
226
// int child = sequence.firstChildPos(ipos);
227
// while (child != 0)
228
// {
229
// last = child;
230
// child = sequence.nextPos(child);
231
// }
232
// return last == 0 ? null : make((NodeTree) sequence, last);
233
// }
234

235   // public NodeList getChildNodes ()
236
// {
237
// Nodes nodes = new SortedNodes();
238
// int child = sequence.firstChildPos(ipos);
239
// while (child != 0)
240
// {
241
// nodes.writePosition(sequence, child);
242
// child = sequence.nextPos(child);
243
// }
244
// return nodes;
245
// }
246

247   // /** Not implemented yet. */
248
// public NodeList getElementsByTagName(String tagname)
249
// {
250
// throw new UnsupportedOperationException("getElementsByTagName not implemented yet");
251
// /*
252
// Nodes nodes = new SortedNodes();
253
// int child = sequence.firstChildPos(ipos);
254
// while (child != 0)
255
// {
256
// if (matches)
257
// nodes.writePosition(sequence, child);
258
// child = sequence.nextPos(child);
259
// }
260
// return nodes;
261
// */
262
// }
263
/* #endif */
264
265   /* #ifdef use:org.w3c.dom.Node */
266   // /** Not implemented. */
267
// public void setNodeValue (String nodeValue) throws DOMException
268
// {
269
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
270
// "setNodeValue not supported");
271
// }
272

273   // /** Not implemented. */
274
// public void setPrefix (String prefix) throws DOMException
275
// {
276
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
277
// "setPrefix not supported");
278
// }
279

280   // /** Not implemented. */
281
// public Node insertBefore(Node newChild, Node refChild)
282
// throws DOMException
283
// {
284
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
285
// "insertBefore not supported");
286
// }
287

288   // /** Not implemented. */
289
// public Node replaceChild(Node newChild, Node oldChild)
290
// throws DOMException
291
// {
292
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
293
// "replaceChild not supported");
294
// }
295

296   // /** Not implemented. */
297
// public Node removeChild(Node oldChild)
298
// throws DOMException
299
// {
300
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
301
// "removeChild not supported");
302
// }
303

304   // /** Not implemented. */
305
// public Node appendChild(Node newChild)
306
// throws DOMException
307
// {
308
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
309
// "appendChild not supported");
310
// }
311

312   // /** Not implemented. */
313
// public void setTextContent (String textContent)
314
// throws DOMException
315
// {
316
// throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
317
// "setTextContent not supported");
318
// }
319

320   // /** Only implemented if deep is true. */
321
// public Node cloneNode(boolean deep)
322
// {
323
// if (! deep)
324
// throw new UnsupportedOperationException("shallow cloneNode not implemented");
325
// NodeTree tree = new NodeTree();
326
// ((NodeTree) sequence).consumeNext(ipos, tree);
327
// return make(tree);
328
// }
329

330   // public org.w3c.dom.Document getOwnerDocument ()
331
// {
332
// int kind = sequence.getNextKind(ipos);
333
// if (kind == Sequence.DOCUMENT_VALUE)
334
// return new KDocument((NodeTree) sequence, 0);
335
// return null;
336
// }
337

338   // public NamedNodeMap getAttributes ()
339
// {
340
// throw new UnsupportedOperationException("getAttributes not implemented yet");
341
// }
342
/* #endif */
343
344   public void normalize ()
345   {
346   }
347
348   public boolean hasAttributes ()
349   {
350     return false;
351   }
352
353   public boolean isDefaultNamespace (String JavaDoc namespaceURI)
354   {
355     return ((NodeTree) sequence).posIsDefaultNamespace(ipos, namespaceURI);
356   }
357
358   public String JavaDoc lookupNamespaceURI (String JavaDoc prefix)
359   {
360     return ((NodeTree) sequence).posLookupNamespaceURI(ipos, prefix);
361   }
362
363   public String JavaDoc lookupPrefix (String JavaDoc namespaceURI)
364   {
365     return ((NodeTree) sequence).posLookupPrefix(ipos, namespaceURI);
366   }
367
368   public String JavaDoc getBaseURI ()
369   {
370     Object JavaDoc uri = ((NodeTree) sequence).baseUriOfPos(ipos, true);
371     return uri == null ? null : uri.toString();
372   }
373
374   public Object JavaDoc baseURI ()
375   {
376     return ((NodeTree) sequence).baseUriOfPos(ipos, true);
377   }
378
379   /* #ifdef use:org.w3c.dom.Node */
380   // public short compareDocumentPosition (Node other)
381
// throws DOMException
382
// {
383
// if (! (other instanceof KNode))
384
// throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
385
// "other Node is a "+other.getClass().getName());
386
// KNode n = (KNode) other;
387
// AbstractSequence nseq = n.sequence;
388
// return (short) (sequence == nseq ? nseq.compare(ipos, n.ipos)
389
// : (int) sequence.stableCompare(nseq));
390
// }
391
//
392
// public boolean isSameNode (Node node)
393
// {
394
// if (! (node instanceof KNode))
395
// return false;
396
// KNode n = (KNode) node;
397
// if (sequence != n.sequence)
398
// return false;
399
// return sequence.equals(ipos, n.ipos);
400
// }
401

402   // public boolean isEqualNode (Node node)
403
// {
404
// throw new UnsupportedOperationException("getAttributesisEqualNode not implemented yet");
405
// }
406
/* #endif */
407
408   public String JavaDoc 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 JavaDoc getFeature (String JavaDoc feature, String JavaDoc 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   /* #ifdef JAXP-1.3 */
432   // public Object setUserData (String key, Object data, UserDataHandler handler)
433
// {
434
// throw new UnsupportedOperationException("setUserData not implemented yet");
435
// }
436

437   // public Object getUserData (String key)
438
// {
439
// return null;
440
// }
441
/* #endif JAXP-1.3 */
442 }
443
Popular Tags