KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > DOMUtils > Tuple


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mediator.DOMUtils;
24
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 import org.w3c.dom.Node JavaDoc;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.ext.LexicalHandler JavaDoc;
32 import org.xquark.mediator.plan.Operator;
33 import org.xquark.mediator.plan.ResultSet;
34 import org.xquark.schema.SimpleType;
35 import org.xquark.util.SAXConstants;
36 import org.xquark.xpath.datamodel.TypedNode;
37 import org.xquark.xquery.parser.XQueryException;
38 import org.xquark.xquery.typing.QAtomicSerializer;
39
40 /**
41  * This is the implementation of a "semi-structured" tuple.
42  * As a semi-structured data is tree-based, we can't just store as an
43  * arraylist of Strings.
44  * A tuple is a tree (DOM representation for now) indexed with paths.
45  *
46  */

47 public class Tuple {
48     // **********************************************************************
49
// * VERSIONING
50
// **********************************************************************
51
private static final String JavaDoc RCSRevision = "$Revision: 1.11 $";
52     private static final String JavaDoc RCSName = "$Name: $";
53     // **********************************************************************
54
// * CLASS VARIABLES
55
// **********************************************************************
56
//protected static DOM2SAX dom2sax = null ;
57
//static { dom2sax = new DOM2SAX() ; }
58

59     //private ArrayList roots = null ;
60
private int size = 0;
61     private int idsize = 0;
62
63     /**
64      * TypedNode are referenced by paths, and must be classified in the *same order*
65      * that is given in paths of BufferTuple.
66      * As there can be multivaluated node, this is an arraylist of arraylist
67      * of references on node.
68      */

69     private ArrayList JavaDoc indexnodes = null;
70     private ArrayList JavaDoc identifiers = null;
71     private BufferTuple parent = null;
72
73     // ***********************************************************************
74
// * INITIALIZATION
75
// ***********************************************************************
76
/**
77      * Allocate a new Tuple.
78      */

79
80     public Tuple(Operator algebra) {
81         this.size = algebra.getSize();
82         this.idsize = algebra.getIdSize();
83         if (size > 0) {
84             indexnodes = new ArrayList JavaDoc(size);
85             for (int i = 0; i < size; i++)
86                 indexnodes.add(null);
87         }
88         if (idsize > 0) {
89             identifiers = new ArrayList JavaDoc(idsize);
90             for (int i = 0; i < idsize; i++)
91                 identifiers.add(null);
92         }
93         //indexnodes = new ArrayList[algebra.getSize()];
94
//identifiers = new Object[algebra.getIdSize()];
95
//roots = new ArrayList() ;
96
}
97
98     public Tuple(int size) {
99         this.size = size;
100         if (size > 0) {
101             indexnodes = new ArrayList JavaDoc(size);
102             for (int i = 0; i < size; i++)
103                 indexnodes.add(null);
104         }
105         //indexnodes = new ArrayList[size];
106
//roots = new ArrayList() ;
107
}
108
109     public Tuple(int size, int idsize) {
110         this.size = size;
111         this.idsize = idsize;
112         if (size > 0) {
113             indexnodes = new ArrayList JavaDoc(size);
114             for (int i = 0; i < size; i++)
115                 indexnodes.add(null);
116         }
117         if (idsize > 0) {
118             identifiers = new ArrayList JavaDoc(idsize);
119             for (int i = 0; i < idsize; i++)
120                 identifiers.add(null);
121         }
122         //indexnodes = new ArrayList[size];
123
//identifiers = new Object[idsize];
124
//roots = new ArrayList() ;
125
}
126
127     public Tuple(ResultSet rs) {
128         this.size = rs.getOperator().getSize();
129         this.idsize = rs.getOperator().getIdSize();
130         if (size > 0) {
131             indexnodes = new ArrayList JavaDoc(size);
132             for (int i = 0; i < size; i++)
133                 indexnodes.add(null);
134         }
135         if (idsize > 0) {
136             identifiers = new ArrayList JavaDoc(idsize);
137             for (int i = 0; i < idsize; i++)
138                 identifiers.add(null);
139         }
140         //indexnodes = new ArrayList[size];
141
//identifiers = new Object[rs.getAlgebra().getIdSize()];
142
//roots = new ArrayList() ;
143
}
144
145     public Tuple(Tuple tuple) {
146         this.size = tuple.size();
147         this.idsize = tuple.idsize();
148         if (size > 0) {
149             indexnodes = new ArrayList JavaDoc(size);
150             for (int i = 0; i < size; i++)
151                 indexnodes.add(null);
152         }
153         if (idsize > 0) {
154             identifiers = new ArrayList JavaDoc(idsize);
155             for (int i = 0; i < idsize; i++)
156                 identifiers.add(null);
157         }
158         this.fillIdentifiers(0, tuple);
159         this.appendTupleAtIndex(0, tuple);
160         //indexnodes = new ArrayList[size];
161
//identifiers = new Object[rs.getAlgebra().getIdSize()];
162
//roots = new ArrayList() ;
163
}
164
165     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
166         return super.clone();
167     }
168     // ***********************************************************************
169
// * GET/SET METHODS
170
// ***********************************************************************
171
public void setParent(BufferTuple parent) {
172         this.parent = parent;
173     }
174     public BufferTuple getParent() {
175         return parent;
176     }
177
178     // ***********************************************************************
179
// * TOOLS METHODS
180
// ***********************************************************************
181
/**
182      *
183      */

184     public void addNodeAtIndex(int idx, TypedNode element) /*throws XQueryException*/ {
185         //if ((idx < 0) || (idx >= indexnodes.length)) throw new XQueryException("Can't index " + element + " at " + idx) ;
186
if (indexnodes.get(idx) == null)
187             indexnodes.set(idx, new ArrayList JavaDoc());
188         ((ArrayList JavaDoc) indexnodes.get(idx)).add(element);
189         /*
190         if (indexnodes[idx] == null)
191             indexnodes[idx] = new ArrayList();
192         indexnodes[idx].add(element);
193         */

194     }
195
196     /**
197      *
198      */

199     public void addNodesAtIndex(int idx, ArrayList JavaDoc elements) {
200         if (elements == null)
201             return;
202         if (indexnodes.get(idx) == null)
203             indexnodes.set(idx, new ArrayList JavaDoc());
204         ((ArrayList JavaDoc) indexnodes.get(idx)).addAll(elements);
205         /*
206         if (indexnodes[idx] == null)
207             indexnodes[idx] = new ArrayList();
208         indexnodes[idx].addAll(elements);
209         */

210     }
211
212     /**
213      *
214      */

215     public ArrayList JavaDoc getPath(String JavaDoc path) {
216         int pos = parent.getPathIndex(path);
217         //HashMap hm = parent.getIndexPath() ;
218
if (pos != -1)
219             return getNodesAtIndex(pos);
220         return null;
221     }
222
223     public ArrayList JavaDoc getNodesAtIndex(int idx) {
224         if (idx >= 0 && idx < indexnodes.size())
225             return (ArrayList JavaDoc) indexnodes.get(idx);
226         else
227             return null;
228         /*
229         if (idx < indexnodes.length)
230             return indexnodes[idx];
231         else
232             return null;
233         */

234     }
235
236     public void appendTuple(Tuple tuple) {
237         appendTupleAtIndex(0, tuple);
238     }
239
240     public void appendTupleAtIndex(int idx, Tuple tuple) {
241         for (int i = 0; i < tuple.size(); i++)
242             addNodesAtIndex(idx + i, tuple.getNodesAtIndex(i));
243     }
244
245     /**
246      *
247      */

248     // private void add(int idx, ArrayList nodes) throws XMLDBCException { addNodesAtIndex(idx, nodes) ; }
249

250     /**
251      *
252      */

253     public int size() {
254         return size;
255     }
256
257     /**
258      *
259      */

260     public int idsize() {
261         return idsize;
262         /*
263         if (identifiers == null)
264             return 0;
265         else
266             return identifiers.length;
267         */

268     }
269
270     /**
271      *
272      */

273     public void eraseIdSize() {
274         identifiers = null;
275         idsize = 0;
276     }
277
278     // ***********************************************************************
279
// * TOOLS METHODS
280
// ***********************************************************************
281

282     public Tuple getPart(int index, int part, int idsiz) {
283         if (index >= size)
284             return null;
285         ArrayList JavaDoc tmplist = this.getNodesAtIndex(index);
286         if (tmplist == null || part >= tmplist.size())
287             return null;
288         ArrayList JavaDoc reslist = new ArrayList JavaDoc(1);
289         reslist.add(tmplist.get(part));
290         Tuple newtuple = new Tuple(1, idsiz);
291         newtuple.addNodesAtIndex(0, reslist);
292         newtuple.fillIdentifiers();
293         return newtuple;
294     }
295
296     public Tuple getPart(int index, int idsiz) {
297         if (index < 0 || index >= size)
298             return null;
299         ArrayList JavaDoc tmplist = this.getNodesAtIndex(index);
300         Tuple newtuple = new Tuple(1, idsiz);
301         newtuple.fillIdentifiers(0,newtuple);
302         newtuple.addNodesAtIndex(0, tmplist);
303         return newtuple;
304     }
305
306     public Tuple getPart(int index) {
307         return getPart(index,0);
308     }
309
310     public Tuple getPart(String JavaDoc str, int idsiz) {
311         if (parent == null)
312             return null;
313         return getPart(parent.getPathIndex(str),idsiz);
314     }
315     
316     public Tuple getPart(String JavaDoc str) {
317         return getPart(str,0);
318     }
319
320     /**
321      *
322      */

323     public Tuple project(ArrayList JavaDoc steps, ArrayList JavaDoc idxsteps) {
324         return project(steps, idxsteps, false);
325     }
326     public Tuple project(ArrayList JavaDoc steps, ArrayList JavaDoc idxsteps, boolean erase) {
327         // if it is the first time this method is called, idxsteps is empty
328
// and we must calculate which column must be kept
329
if (idxsteps.isEmpty()) {
330             for (int i = 0; i < steps.size(); i++) {
331                 /*
332                 ArrayList stepi = (ArrayList) steps.get(i) ;
333                 StringBuffer sb = new StringBuffer() ;
334                 for (int j = 0 ; j < stepi.size() ; j ++) {
335                     if (j!=0) sb.append("/") ;
336                     sb.append((String) stepi.get(j)) ;
337                 }
338                  */

339                 String JavaDoc stepsi = (String JavaDoc) steps.get(i);
340                 // CHANGE (add condition on index)
341
int tmpInt = parent.getPathIndex(stepsi);
342                 if (tmpInt >= 0)
343                     idxsteps.add(new Integer JavaDoc(tmpInt));
344             }
345         }
346
347         Tuple newtuple = null;
348         if (erase) { // not done yet
349
newtuple = this;
350         } else {
351             newtuple = new Tuple(idxsteps.size(), idsize);
352             newtuple.fillIdentifiers(0, this);
353             int cpt = 0;
354             for (int i = 0; i < idxsteps.size(); i++) {
355                 int idx = ((Integer JavaDoc) idxsteps.get(i)).intValue();
356                 newtuple.addNodesAtIndex(cpt++, getNodesAtIndex(idx));
357             }
358         }
359         return newtuple;
360     }
361
362     /**
363      *
364      */

365     public Tuple merge(Tuple addtuple, Operator algebra) {
366         Tuple newtuple = new Tuple(algebra);
367         newtuple.appendTuple(this);
368         newtuple.fillIdentifiers(0, this);
369         int cpt = size();
370         for (int k = 0; k < addtuple.size(); k++) {
371 // int equivi = -1;
372
// if ((equivi = getEquiv(algebra.getEquivalences(), k)) != -1) {
373
// ArrayList trees1 = (ArrayList) getNodesAtIndex(equivi);
374
// ArrayList trees2 = (ArrayList) addtuple.getNodesAtIndex(equivi);
375
// if (DOMUtils.compareTree(trees1, trees2)) {
376
// // it's OK, do nothing
377
// } else
378
// return null;
379
// } else {
380
newtuple.addNodesAtIndex(cpt++, addtuple.getNodesAtIndex(k));
381 // }
382
}
383         cpt = idsize;
384
385         for (int k = 0; k < addtuple.idsize(); k++)
386 // if (getEquiv(algebra.getIdEquivalences(), k) == -1)
387
newtuple.fillIdentifiers(cpt++, addtuple.getIdentifiers(k));
388
389         return newtuple;
390     }
391
392     /**
393      *
394      */

395     public void mergeInPlace(ArrayList JavaDoc nodes, Operator algebra) {
396         int oldsize = size;
397         if (size < algebra.getSize()) {
398             for (int i = size; i < algebra.getSize(); i++)
399                 indexnodes.add(null);
400             size = algebra.getSize();
401         }
402         if (idsize < algebra.getIdSize()) {
403             if (identifiers == null)
404                 identifiers = new ArrayList JavaDoc();
405             for (int i = idsize; i < algebra.getIdSize(); i++)
406                 identifiers.add(this);
407             idsize = algebra.getIdSize();
408         }
409         if (nodes != null)
410             addNodesAtIndex(oldsize, nodes);
411     }
412
413     /**
414      *
415      */

416     public void mergeInPlace(TypedNode node, Operator algebra) {
417         int oldsize = size;
418         if (size < algebra.getSize()) {
419             for (int i = size; i < algebra.getSize(); i++)
420                 indexnodes.add(null);
421             size = algebra.getSize();
422         }
423         if (idsize < algebra.getIdSize()) {
424             if (identifiers == null)
425                 identifiers = new ArrayList JavaDoc();
426             for (int i = idsize; i < algebra.getIdSize(); i++)
427                 identifiers.add(this);
428             idsize = algebra.getIdSize();
429         }
430         if (node != null)
431             addNodeAtIndex(oldsize, node);
432     }
433
434     /**
435      *
436      */

437 // private int getEquiv(ArrayList equivalences, int j) {
438
// if (equivalences != null)
439
// for (int k = 0; k < equivalences.size(); k++) {
440
// int[] couple = (int[]) equivalences.get(k);
441
// if (couple[1] == j)
442
// return couple[0];
443
// }
444
// return -1;
445
// }
446

447     /**
448      *
449      */

450     public Tuple completeTupleForLeft(int size) {
451         Tuple tuple = new Tuple(size);
452         tuple.appendTuple(this);
453         //for (int i = size() ; i < size ; i ++) tuple.appendTuple(null) ;
454
return tuple;
455     }
456
457     /**
458      *
459      */

460     public Tuple completeTupleForRight(int size, ArrayList JavaDoc equivalences) {
461         Tuple tuple = new Tuple(size);
462         int cpt = 0;
463
464         // initialize all to null
465
//for (int i = 0 ; i < size ; i ++) tuple.appendTuple(null) ;
466

467         int limit = size - size();
468
469         // complete the common
470
for (int i = 0; i < size(); i++) {
471 // int idx1 = getEquiv(equivalences, i);
472
// if (idx1 != -1)
473
// tuple.addNodesAtIndex(idx1, getNodesAtIndex(i));
474
// else {
475
tuple.addNodesAtIndex(limit + cpt, getNodesAtIndex(i));
476                 cpt++;
477 // }
478
}
479         return tuple;
480     }
481
482     // pour fetch
483
/**
484      *
485      */

486     public String JavaDoc makeStringPath(int index, QAtomicSerializer ser) {
487         ArrayList JavaDoc nodes = getNodesAtIndex(index);
488         if (nodes == null)
489             return null;
490         if (nodes.size() != 1)
491             return null;
492
493         TypedNode node = (TypedNode) nodes.get(0);
494         if (node == null)
495             return null;
496         Object JavaDoc typedValue = node.getTypedValue();
497         if (typedValue == null && node.getChildNodes().getLength() == 1) {
498             node = (TypedNode)node.getChildNodes().item(0) ;
499             typedValue = node.getTypedValue();
500         }
501         if (typedValue == null) return null;
502         else try {
503             return ser.serialize((SimpleType)node.getType(), typedValue);
504         } catch (XQueryException e) {
505             return null;
506         }
507     }
508     // pour generate
509
/**
510      *
511      */

512     public void makeEventPath(TypedDOMReader domreader, ContentHandler JavaDoc handler, int index) throws XQueryException {
513         if (domreader == null)
514             return;
515         ArrayList JavaDoc nodes = getNodesAtIndex(index);
516         if (nodes == null || nodes.isEmpty())
517             return;
518         try {
519             for (int i = 0; i < nodes.size(); i++) {
520                 TypedNode nodei = (TypedNode) nodes.get(i);
521                 if (nodei == null) {
522                     continue;
523                 }
524                 domreader.setContentHandler(handler);
525                 if (handler instanceof LexicalHandler JavaDoc)
526                     domreader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
527                 domreader.setRoot(nodei);
528                 domreader.setFeature(SAXConstants.FRAGMENT_FEATURE, true);
529                 domreader.parse("");
530             }
531             return;
532         } catch (IOException JavaDoc ioe) {
533             throw new XQueryException("Tuple.makeEventPath: " + ioe.getMessage());
534         } catch (SAXException JavaDoc saxe) {
535             throw new XQueryException("Tuple.makeEventPath: " + saxe.getMessage());
536         }
537     }
538
539     /**
540      *
541      */

542     //public void fillIdentifiers(long identifier) {
543
public void fillIdentifiers() {
544         if (identifiers == null)
545             return;
546         //Long id = new Long(identifier);
547
for (int i = 0; i < idsize; i++)
548             identifiers.set(i, this);
549         /*
550         if (identifiers == null)
551             return;
552         Long id = new Long(identifier);
553         for (int i = 0; i < idsize; i++)
554             identifiers[i] = id;
555         */

556     }
557
558     /**
559      *
560      */

561     public void fillIdentifiers(int startindex, Tuple tuple) {
562         for (int i = startindex, j = 0; i < startindex + tuple.idsize(); i++, j++)
563             identifiers.set(i, tuple.getIdentifiers(j));
564         /*
565         for (int i = startindex, j = 0; i < startindex + tuple.idsize(); i++, j++)
566             identifiers[i] = tuple.getIdentifiers(j);
567         */

568     }
569
570     /**
571      *
572      */

573     public void fillIdentifiers(int startindex, Object JavaDoc obj) {
574         identifiers.set(startindex, obj);
575         //identifiers[startindex] = obj;
576
}
577
578     /**
579      *
580      */

581     public ArrayList JavaDoc retIdentifiers() {
582         return identifiers;
583     }
584     // public Object[] retIdentifiers() {
585
// return identifiers;
586
// }
587

588     /**
589      *
590      */

591     public Object JavaDoc getIdentifiers(int index) {
592         if (identifiers == null || idsize <= index)
593             return null;
594         return identifiers.get(index);
595     }
596     // public Object getIdentifiers(int index) {
597
// if (identifiers == null || idsize <= index)
598
// return null;
599
// return identifiers[index];
600
// }
601

602     /**
603      *
604      */

605     public void getIdentifiers(Object JavaDoc[] identifiers) {
606         if (this.identifiers == null) {
607             for (int i = 0; i < identifiers.length; i++)
608                 identifiers[i] = null;
609             return;
610         }
611         for (int i = 0; i < identifiers.length; i++)
612             if (idsize > i)
613                 identifiers[i] = this.identifiers.get(i);
614             else
615                 identifiers[i] = null;
616     }
617     // public void getIdentifiers(Object[] identifiers) {
618
// if (this.identifiers == null) {
619
// for (int i = 0; i < identifiers.length; i++)
620
// identifiers[i] = null;
621
// return;
622
// }
623
// for (int i = 0; i < this.identifiers.length; i++)
624
// identifiers[i] = this.identifiers[i];
625
// }
626

627     public boolean equals(Tuple tuple) {
628         if (tuple == null)
629             return false;
630         if (tuple.size() != this.size)
631             return false;
632         if (tuple.idsize() != this.idsize)
633             return false;
634         for (int i = 0; i < size; i++) {
635             ArrayList JavaDoc listi = tuple.getNodesAtIndex(i);
636             if (((ArrayList JavaDoc) indexnodes.get(i)).size() != listi.size())
637                 return false;
638             for (int j = 0; j < listi.size(); j++) {
639                 TypedNode nodeij1 = (TypedNode) listi.get(j);
640                 TypedNode nodeij2 = (TypedNode) ((ArrayList JavaDoc) indexnodes.get(i)).get(j);
641                 if (!DOMUtils.compareTree(nodeij1, nodeij2))
642                     return false;
643             }
644         }
645         return true;
646     }
647     // public boolean equals(Tuple tuple) {
648
// if (tuple == null)
649
// return false;
650
// if (tuple.size() != this.size)
651
// return false;
652
// if (tuple.idsize() != this.idsize)
653
// return false;
654
// for (int i = 0; i < size; i++) {
655
// ArrayList listi = tuple.getNodesAtIndex(i);
656
// if (indexnodes[i].size() != listi.size())
657
// return false;
658
// for (int j = 0; j < listi.size(); j++) {
659
// TypedNode nodeij1 = (TypedNode) listi.get(j);
660
// TypedNode nodeij2 = (TypedNode) indexnodes[i].get(j);
661
// if (!DOMUtils.compareTree(nodeij1, nodeij2))
662
// return false;
663
// }
664
// }
665
// return true;
666
// }
667

668     // ***********************************************************************
669
// * DEBUG
670
// ***********************************************************************
671
/*
672     public String toString() {
673         return toCompleteString();
674     }
675     */

676     public String JavaDoc toCompleteString() {
677         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
678         sb.append("\n----------- <TUPLE (" + size() + ")> -----------------\n");
679         sb.append("INDEX \n");
680         for (int i = 0; i < indexnodes.size(); i++)
681             sb.append(i + " - " + indexnodes.get(i) + "\n");
682         
683         if (identifiers == null)
684             sb.append("\nidentifiers is empty\n");
685         else {
686             sb.append("\nidentifiers \n");
687             for (int i = 0; i < identifiers.size(); i++)
688                 sb.append(i + " - " + identifiers.get(i) + "\n");
689         }
690         
691         for (int i = 0; i < size; i++) {
692             sb.append("\n-------------------------------\n");
693             ArrayList JavaDoc nodes = getNodesAtIndex(i);
694             if (nodes == null) {
695                 sb.append("null nodes " + i + "\n");
696                 continue;
697             }
698             for (int j = 0; j < nodes.size(); j++) {
699                 //TypedNode nodej = (TypedNode) nodes.get(j);
700
if (nodes.get(j) == null)
701                     sb.append("null node " + i + "-" + j + "\n");
702                 else {
703                     Object JavaDoc obj = nodes.get(j);
704                     if (obj instanceof TypedNode)
705                         sb.append(stringRepresentationOf((TypedNode)obj));
706                     else if (obj instanceof Node JavaDoc)
707                         sb.append(stringRepresentationOf((Node JavaDoc)obj));
708                     else
709                         sb.append("unknown node type " + obj.getClass().getName());
710                 }
711             }
712         }
713         sb.append("\n----------------------------------------\n");
714         return sb.toString();
715     }
716
717     /**
718      * Make a programmer-friendly representation of a TypedNode (useful for
719      * debugging).
720      *
721      * @param root the node to display recursively.
722      * @return a String represention of the node.
723      */

724     public String JavaDoc stringRepresentationOf(TypedNode root) {
725         return stringRepresentationOf("", root);
726     }
727     public String JavaDoc stringRepresentationOf(Node JavaDoc root) {
728         return stringRepresentationOf("", root);
729     }
730
731     /**
732      * Make a programmer-friendly representation of a TypedNode (useful for
733      * debugging).
734      *
735      * @param root the node to display recursively.
736      * @return a String representation of the node.
737      */

738     private String JavaDoc stringRepresentationOf(String JavaDoc before, TypedNode root) {
739         if (root == null)
740             return before + "<null>";
741
742         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
743         String JavaDoc newline = System.getProperty("line.separator");
744
745         // display Attributes first
746
org.w3c.dom.NamedNodeMap JavaDoc nnm = root.getAttributes();
747         if (nnm != null) {
748             for (int i = 0; i < nnm.getLength(); i++)
749                 str.append(newline + before + "*---- [A] " + nnm.item(i).getNodeName() + " : " + nnm.item(i).getNodeValue());
750         }
751
752         // display TypedNode first
753
org.w3c.dom.NodeList JavaDoc nl = root.getChildNodes();
754         if ((nl == null) || (nl.getLength() <= 0))
755             str.append(newline + before + "+----" + root.getNodeName() + " : " + root.getNodeValue() + " [" + root.getNodeType() + "]");
756         else {
757             str.append(newline + before + "+----" + root.getNodeName() + " [" + root.getNodeType() + "]");
758             for (int i = 0; i < nl.getLength(); i++)
759                 str.append(stringRepresentationOf(before + " ", nl.item(i)));
760         }
761         return str.toString();
762     }
763
764     private String JavaDoc stringRepresentationOf(String JavaDoc before, Node JavaDoc root) {
765         if (root == null)
766             return before + "<null>";
767
768         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
769         String JavaDoc newline = System.getProperty("line.separator");
770
771         // display Attributes first
772
org.w3c.dom.NamedNodeMap JavaDoc nnm = root.getAttributes();
773         if (nnm != null) {
774             for (int i = 0; i < nnm.getLength(); i++)
775                 str.append(newline + before + "*---- [A] " + nnm.item(i).getNodeName() + " : " + nnm.item(i).getNodeValue());
776         }
777
778         // display TypedNode first
779
org.w3c.dom.NodeList JavaDoc nl = root.getChildNodes();
780         if ((nl == null) || (nl.getLength() <= 0))
781             str.append(newline + before + "+----" + root.getNodeName() + " : " + root.getNodeValue() + " [" + root.getNodeType() + "]");
782         else {
783             str.append(newline + before + "+----" + root.getNodeName() + " [" + root.getNodeType() + "]");
784             for (int i = 0; i < nl.getLength(); i++)
785                 str.append(stringRepresentationOf(before + " ", nl.item(i)));
786         }
787         return str.toString();
788     }
789     
790 }
791
Popular Tags