KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > dom > DOMInfo


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: DOMInfo.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.dom;
25
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 import org.enhydra.xml.lazydom.LazyDOMInfoPrinter;
30 import org.enhydra.xml.lazydom.LazyDocument;
31 import org.w3c.dom.Node JavaDoc;
32
33 /*
34  * NB: A lot of this code is duplicated in LazyDOMInfo using template nodes,
35  * keep them in-sync.
36  */

37
38 // FIXME: need to implement no-recursion; the organization of this class is
39
// to fixed. It would be useful to have methods to just format a node
40
// (without recursing) into a string, then the traversal to tie it together.
41

42 /**
43  * Methods to print information out about a DOM. Used for debugging, etc.
44  * Has special handling for Lazy DOMs to prevent expansion.
45  */

46 public class DOMInfo {
47     /**
48      * Print attributes as the Nodes that represent them.
49      */

50     public static final int PRINT_ATTR_DETAILS = 0x01;
51
52     /**
53      * Print all attributes, even unspecified ones.
54      */

55     public static final int ALL_ATTRS = 0x02;
56
57     /**
58      * Print newlines and carriage returns as \n and \r, avoiding
59      * linebreaks in text
60      */

61     public static final int SYM_TEXT_LINEBREAKS = 0x04;
62
63     /**
64      * Don't recurse, just print the specified node.
65      */

66     private static final int NO_RECURSION = 0x08; //FIXME: private because not implemented
67

68     /**
69      * Truncate text, comments etc, to 16 chars. Implys SYM_TEXT_LINEBREAKS.
70      */

71     public static final int TRUNCATE_TEXT = 0x10;
72
73     /**
74      * All Print options, except NO_RECURSION.
75      */

76     public static final int PRINT_ALL = 0xffff & ~NO_RECURSION;
77
78     /**
79      * Default print options.
80      */

81     public static final int PRINT_DEFAULT = 0;
82
83     /**
84      * Options to use.
85      */

86     protected int fOptions;
87
88     /**
89      * Write for output.
90      */

91     protected PrintWriter JavaDoc fOut;
92
93     /**
94      * DOM traversal object and handler.
95      */

96     protected DOMTraversal fTraverser;
97     protected DOMInfoPrinter fInfoPrinter;
98
99     /**
100      * Create a DOMInfo object to print data about the DOM. Sets up the
101      * traverser and traversal handlers based on the document type.
102      */

103     private static DOMInfo createDOMInfo(int options,
104                                          Node JavaDoc node,
105                                          PrintWriter JavaDoc out) {
106
107         // This will create a traverser and handler based on document type.
108
// Much check real document, as it could be an XMLC object..
109
DOMTraversal traverser
110             = DOMTraversal.getTraverser(null,
111                                         getTraverserOptions(options),
112                                         node);
113
114         DOMInfoPrinter handler;
115         if (DOMOps.getDocument(node) instanceof LazyDocument) {
116             handler = new LazyDOMInfoPrinter(options, traverser, out);
117         } else {
118             handler = new DOMInfoPrinter(options, traverser, out);
119         }
120         traverser.setHandler(handler);
121         return new DOMInfo(options, traverser, handler, out);
122     }
123
124     /**
125      * Constructor for derived objects.
126      */

127     protected DOMInfo(int options,
128                       DOMTraversal traverser,
129                       DOMInfoPrinter infoPrinter,
130                       PrintWriter JavaDoc out) {
131         fOptions = options;
132         fTraverser = traverser;
133         fInfoPrinter = infoPrinter;
134         fOut = out;
135     }
136
137     /**
138      * Traverse the DOM.
139      */

140     private void traverse(Node JavaDoc node) {
141         fTraverser.traverse(node);
142     }
143
144     /**
145      * Get traverser options from DOMInfo options.
146      */

147     public static int getTraverserOptions(int options) {
148         int travOptions = DOMTraversal.SORT_ATTRIBUTES;
149         if ((options & ALL_ATTRS) != 0) {
150             travOptions |= DOMTraversal.ALL_ATTRIBUTES;
151         }
152         return travOptions;
153     }
154
155
156     /**
157      * Convert a node type to a name.
158      */

159     public static String JavaDoc nodeTypeToName(short type) {
160         switch (type) {
161         case Node.ELEMENT_NODE:
162             return "Element";
163         case Node.ATTRIBUTE_NODE:
164             return "Attr";
165         case Node.TEXT_NODE:
166             return "Text";
167         case Node.CDATA_SECTION_NODE:
168             return "CDATASection";
169         case Node.ENTITY_REFERENCE_NODE:
170             return "EntityReference";
171         case Node.ENTITY_NODE:
172             return "Entity";
173         case Node.PROCESSING_INSTRUCTION_NODE:
174             return "ProcessingInstruction";
175         case Node.COMMENT_NODE:
176             return "Comment";
177         case Node.DOCUMENT_NODE:
178             return "Document";
179         case Node.DOCUMENT_TYPE_NODE:
180             return "DocumentType";
181         case Node.DOCUMENT_FRAGMENT_NODE:
182             return "DocumentFragment";
183         case Node.NOTATION_NODE:
184             return "Notation";
185         default:
186             throw new IllegalArgumentException JavaDoc("Unknown node type: " + type);
187         }
188     }
189
190     /**
191      * Print a DOM node and its children.
192      *
193      * @param msg A message to print at the start. If null or empty,
194      * don't print a message.
195      * @param root Top of the tree.
196      * @param options Set of print options.
197      * @param out Output writer. If null, stderr will be used.
198      */

199     public static void printTree(String JavaDoc msg,
200                                  Node JavaDoc root,
201                                  int options,
202                                  PrintWriter JavaDoc out) {
203         // Default options
204
if((options & TRUNCATE_TEXT) != 0) {
205             options |= SYM_TEXT_LINEBREAKS;
206         }
207
208         if (out == null) {
209             out = new PrintWriter JavaDoc(System.err, true);
210         }
211         if ((msg != null) && (msg.length() > 0)) {
212             out.println(msg + ":");
213         }
214         if (root == null) {
215             out.println(" null node");
216         } else {
217             DOMInfo domInfo = createDOMInfo(options, root, out);
218             domInfo.traverse(root);
219         }
220         out.flush();
221     }
222
223     /**
224      * Print a DOM node an its children.
225      *
226      * @param msg A message to print at the start. If null or empty,
227      * don't print a message.
228      * @param node Top of the tree.
229      * @param out Output writer. If null, stderr will be used.
230      */

231     public static void printTree(String JavaDoc msg,
232                                  Node JavaDoc root,
233                                  PrintWriter JavaDoc out) {
234         printTree(msg, root, PRINT_DEFAULT, out);
235     }
236
237     /**
238      * Print a DOM node an its children.
239      *
240      * @param msg A message to print at the start. If null or empty,
241      * don't print a message.
242      * @param root Top of the tree.
243      * @param out Output stream. If null, stderr will be used.
244      */

245     public static void printTree(String JavaDoc msg,
246                                  Node JavaDoc root,
247                                  OutputStream JavaDoc out) {
248         PrintWriter JavaDoc writer = new PrintWriter JavaDoc((out != null) ? out : System.err, true);
249         printTree(msg, root, PRINT_DEFAULT, writer);
250     }
251
252     /**
253      * Print a DOM node an its children to stderr..
254      *
255      * @param msg A message to print at the start. If null or empty,
256      * don't print a message.
257      * @param root Top of the tree.
258      */

259     public static void printTree(String JavaDoc msg,
260                                  Node JavaDoc root) {
261         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(System.err, true);
262         printTree(msg, root, PRINT_DEFAULT, writer);
263     }
264 }
265
Popular Tags