KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) 2003 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.mapping.Values;
6 import gnu.lists.*;
7 import gnu.xml.*;
8 /* #ifdef use:org.w3c.dom.Node */
9 // import org.w3c.dom.*;
10
/* #endif */
11
12 /** Manages a sequence of node references. */
13
14 public class Nodes extends Values
15   /* #ifdef use:org.w3c.dom.Node */
16   // implements org.w3c.dom.NodeList
17
/* #endif */
18 {
19   /** Number of data elements for a POSITION_PAIR_FOLLOWS node reference. */
20   static final int POS_SIZE = 5;
21
22   int count;
23
24   int nesting = 0;
25   boolean inAttribute;
26   NodeTree curNode;
27   XMLFilter curFragment;
28
29   public void writePosition (AbstractSequence seq, int ipos)
30   {
31     count++;
32     super.writePosition(seq, ipos);
33   }
34
35   public int find (Object JavaDoc seq)
36   {
37     // See if can re-use the object index of the position before the gap.
38
if (gapStart > 0)
39       {
40     int oindex = getIntN(gapStart - POS_SIZE + 1);
41     if (objects[oindex] == seq)
42       return oindex;
43       }
44     // See if can re-use the object index of the position after the gap.
45
if (gapEnd < data.length)
46       {
47     int oindex = getIntN(gapEnd + 1);
48     if (objects[oindex] == seq)
49       return oindex;
50       }
51     return super.find(seq);
52   }
53
54   public void writeObject(Object JavaDoc v)
55   {
56     if (curFragment != null)
57       {
58     if (nesting == 0
59         && (v instanceof SeqPosition || v instanceof TreeList))
60       finishFragment();
61     else
62       {
63         curFragment.writeObject(v);
64         return;
65       }
66       }
67     if (v instanceof SeqPosition)
68       {
69     SeqPosition seq = (SeqPosition) v;
70     writePosition(seq.sequence, seq.ipos);
71     return;
72       }
73     if (v instanceof TreeList)
74       {
75     TreeList tlist = (TreeList) v;
76     writePosition(tlist, 0);
77     return;
78       }
79     handleNonNode();
80     curFragment.writeObject(v);
81     return;
82   }
83
84   void maybeStartTextNode ()
85   {
86     if (curFragment == null)
87       {
88         throw new IllegalArgumentException JavaDoc("non-node where node required");
89       }
90   }
91
92   void handleNonNode ()
93   {
94     if (curFragment == null)
95       {
96         throw new ClassCastException JavaDoc("atomic value where node is required");
97       }
98   }
99
100   public void writeFloat (float v)
101   {
102     handleNonNode();
103     curFragment.writeFloat(v);
104   }
105
106   public void writeDouble (double v)
107   {
108     handleNonNode();
109     curFragment.writeDouble(v);
110   }
111
112   public void writeLong(long v)
113   {
114     handleNonNode();
115     curFragment.writeLong(v);
116   }
117
118   public void writeInt(int v)
119   {
120     handleNonNode();
121     curFragment.writeInt(v);
122   }
123
124   public void writeBoolean (boolean v)
125   {
126     handleNonNode();
127     curFragment.writeBoolean(v);
128   }
129
130   public void write (int v)
131   {
132     maybeStartTextNode();
133     curFragment.write(v);
134   }
135
136   /* #ifdef use:java.lang.CharSequence */
137   public Consumer append (CharSequence JavaDoc csq, int start, int end)
138   {
139     maybeStartTextNode();
140     curFragment.write(csq, start, end);
141     return this;
142   }
143   /* #else */
144   // public Consumer append (String str)
145
// {
146
// maybeStartTextNode();
147
// curFragment.append(str);
148
// return this;
149
// }
150
/* #endif */
151
152   public void write(char[] buf, int off, int len)
153   {
154     maybeStartTextNode();
155     curFragment.write(buf, off, len);
156   }
157
158   /* #ifdef use:java.lang.CharSequence */
159   public void write(CharSequence JavaDoc str, int start, int length)
160   /* #else */
161   // public void write(String str, int start, int length)
162
/* #endif */
163   {
164     maybeStartTextNode();
165     curFragment.write(str, start, length);
166   }
167
168   public void write (String JavaDoc str)
169   {
170     maybeStartTextNode();
171     curFragment.write(str);
172   }
173
174   private void maybeStartNonTextNode ()
175   {
176     if (curFragment != null && nesting == 0)
177       finishFragment();
178     if (curFragment == null)
179       startFragment();
180     nesting++;
181   }
182
183   private void maybeEndNonTextNode ()
184   {
185     if (--nesting == 0)
186       finishFragment();
187   }
188
189   public void beginGroup (Object JavaDoc type)
190   {
191     maybeStartNonTextNode();
192     curFragment.beginGroup(type);
193   }
194
195   public void endGroup ()
196   {
197     curFragment.endGroup();
198     maybeEndNonTextNode();
199   }
200
201   public void beginAttribute(Object JavaDoc attrType)
202   {
203     maybeStartNonTextNode();
204     curFragment.beginAttribute(attrType);
205     inAttribute = true;
206   }
207
208   public void endAttribute()
209   {
210     if (! inAttribute)
211       return;
212     inAttribute = false;
213     curFragment.endAttribute();
214     maybeEndNonTextNode();
215   }
216
217   public void writeComment(char[] chars, int offset, int length)
218   {
219     maybeStartNonTextNode();
220     curFragment.writeComment(chars, offset, length);
221     maybeEndNonTextNode();
222   }
223
224   public void writeCDATA(char[] chars, int offset, int length)
225   {
226     maybeStartNonTextNode();
227     curFragment.writeCDATA(chars, offset, length);
228   }
229
230   public void writeProcessingInstruction(String JavaDoc target, char[] content,
231                      int offset, int length)
232   {
233     maybeStartNonTextNode();
234     curFragment.writeProcessingInstruction(target, content, offset, length);
235     maybeEndNonTextNode();
236   }
237
238   public void beginDocument()
239   {
240     maybeStartNonTextNode();
241     curFragment.beginDocument();
242   }
243
244   public void endDocument()
245   {
246     curFragment.endDocument();
247     maybeEndNonTextNode();
248   }
249
250   public void beginEntity(Object JavaDoc base)
251   {
252     maybeStartNonTextNode();
253     curFragment.beginEntity(base);
254   }
255
256   public void endEntity()
257   {
258     curFragment.endEntity();
259     maybeEndNonTextNode();
260   }
261
262   void startFragment ()
263   {
264     curNode = new NodeTree();
265     curFragment = new XMLFilter(curNode);
266     writePosition(curNode, 0);
267   }
268
269   void finishFragment ()
270   {
271     curNode = null;
272     curFragment = null;
273   }
274
275   public int size()
276   {
277     return count;
278   }
279
280   public int getLength()
281   {
282     return count;
283   }
284
285   public Object JavaDoc get (int index)
286   {
287     int i = POS_SIZE * index;
288     if (i >= gapStart)
289       i += gapEnd - gapStart;
290     if (i < 0 || i >= data.length)
291       throw new IndexOutOfBoundsException JavaDoc();
292     // Inline of: return getPosNext(i << 1)
293
if (data[i] != POSITION_PAIR_FOLLOWS)
294       throw new RuntimeException JavaDoc("internal error - unexpected data");
295     return KNode.make((NodeTree) objects[getIntN(i+1)], getIntN(i+3));
296   }
297
298   /* #ifdef use:org.w3c.dom.Node */
299   // public Node item(int index)
300
// {
301
// if (index >= count)
302
// return null;
303
// else
304
// return (Node) get(index);
305
// }
306
/* #endif */
307
308   public Object JavaDoc getPosNext(int ipos)
309   {
310     int index = posToDataIndex(ipos);
311     if (index == data.length)
312       return Sequence.eofValue;
313     if (data[index] != POSITION_PAIR_FOLLOWS)
314       throw new RuntimeException JavaDoc("internal error - unexpected data");
315     return KNode.make((NodeTree) objects[getIntN(index+1)], getIntN(index+3));
316   }
317
318   /** Optimization of ((SeqPosition) get(index)).sequence.
319    * However returns null instead of throwing IndexOutOfBoundsException
320    * if index >= count. */

321   public AbstractSequence getSeq (int index)
322   {
323     int i = POS_SIZE * index;
324     if (i >= gapStart)
325       i += gapEnd - gapStart;
326     if (i < 0 || i >= data.length)
327       return null;
328     // Inline of: return getPosNext(i << 1)
329
if (data[i] != POSITION_PAIR_FOLLOWS)
330       throw new RuntimeException JavaDoc("internal error - unexpected data");
331     return (AbstractSequence) objects[getIntN(i+1)];
332   }
333
334   /** Optimization of ((SeqPosition) get(index)). ipos. */
335   public int getPos (int index)
336   {
337     int i = POS_SIZE * index;
338     if (i >= gapStart)
339       i += gapEnd - gapStart;
340     // Inline of: return getPosNext(i << 1)
341
if (data[i] != POSITION_PAIR_FOLLOWS)
342       throw new RuntimeException JavaDoc("internal error - unexpected data");
343     return getIntN(i+3);
344   }
345
346   public static KNode root (NodeTree seq, int ipos)
347   {
348     int root;
349     if (seq.gapStart > TreeList.BEGIN_ENTITY_SIZE
350         && seq.data[0] == TreeList.BEGIN_ENTITY)
351       root = TreeList.BEGIN_ENTITY_SIZE << 1;
352     else
353       root = 0;
354     return KNode.make(seq, root);
355     /*
356     int end = seq.endPos();
357     for (;;)
358       {
359     int parent = seq.parentPos(ipos);
360     if (parent == end)
361       return KNode.make(seq, ipos);
362     ipos = parent;
363       }
364     */

365   }
366 }
367
Popular Tags