KickJava   Java API By Example, From Geeks To Geeks.

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


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: DOMInfoPrinter.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.PrintWriter JavaDoc;
27
28 import org.enhydra.xml.xmlc.codegen.JavaLang;
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.CDATASection JavaDoc;
31 import org.w3c.dom.CharacterData JavaDoc;
32 import org.w3c.dom.Comment JavaDoc;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.DocumentFragment JavaDoc;
35 import org.w3c.dom.DocumentType JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Entity JavaDoc;
38 import org.w3c.dom.EntityReference JavaDoc;
39 import org.w3c.dom.NamedNodeMap JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.Notation JavaDoc;
42 import org.w3c.dom.ProcessingInstruction JavaDoc;
43 import org.w3c.dom.Text JavaDoc;
44
45 /**
46  * DOMTraversal handler for DOMInfo, using only generic DOM interfaces.
47  */

48 public class DOMInfoPrinter implements DOMTraversal.Handler {
49     /** Size to truncate text to */
50     private static final int TEXT_TRUNCATE_SIZE = 12;
51
52     /** Writer for output. */
53     protected PrintWriter JavaDoc fOut;
54
55     /** Print lines breaks as \n and \r */
56     private boolean fSymbolicLineBreaks;
57
58     /** Should text strings be truncated? */
59     private boolean fTruncateText;
60
61     /** Control attribute formatting. */
62     protected boolean fVerboseAttributes;
63     
64     /** Should all attributes, or only specified ones be printed? */
65     protected boolean fAllAttributes;
66     
67     /**
68      * Flag to indicate that nothing should be printed, just nodes counted.
69      * Use to implement terse attribute printing, nonly need be checked on
70      * thinks that can be children of attributes.
71      */

72     protected boolean fAttrSkipPrint;
73
74     /**
75      * DOM traversal object.
76      */

77     private DOMTraversal fTraverser;
78
79     /**
80      * Indent level.
81      */

82     protected int level;
83
84     /**
85      * Constructor.
86      */

87     protected DOMInfoPrinter(int options,
88                              DOMTraversal traverser,
89                              PrintWriter JavaDoc out) {
90         fOut = out;
91         fSymbolicLineBreaks = ((options & DOMInfo.SYM_TEXT_LINEBREAKS) != 0);
92         fTruncateText = ((options & DOMInfo.TRUNCATE_TEXT) != 0);
93         fVerboseAttributes = ((options & DOMInfo.PRINT_ATTR_DETAILS) != 0);
94         fAllAttributes = ((options & DOMInfo.ALL_ATTRS) != 0);
95         fTraverser = traverser;
96         traverser.setHandler(this);
97     }
98
99     /**
100      * Print indentation.
101      */

102     protected void printIndent() {
103         for (int i = 0; i < level + 1; i++) {
104             fOut.print(" ");
105         }
106     }
107
108     /**
109      * Determine if a character should be printed as is.
110      */

111     private boolean isPrintable(char ch) {
112         return ((ch <= 0x7f)
113                 && (Character.isWhitespace(ch) || !Character.isISOControl(ch)));
114     }
115
116     /**
117      * Print a block of text, encoding non-ASCII or ISO control
118      * charcters using the convention.
119      */

120     private void printString(String JavaDoc text) {
121         int len = text.length();
122         for (int idx = 0; idx < len; idx++) {
123             char ch = text.charAt(idx);
124             if (fSymbolicLineBreaks && ((ch == '\n') || (ch == '\r'))) {
125                 fOut.write((ch == '\n') ? "\\n" : "\\r");
126             } else if (isPrintable(ch)) {
127                 fOut.write(ch);
128             } else {
129                 String JavaDoc hex = Integer.toHexString(ch);
130                 fOut.write("\\u");
131                 for (int cnt = hex.length(); cnt < 4; cnt++) {
132                     fOut.write('0');
133                 }
134                 fOut.write(hex);
135             }
136         }
137     }
138
139     /*
140      * Print a label and value if the value is not null or empty.
141      */

142     private void printLabelValue(String JavaDoc label,
143                                  String JavaDoc value) {
144         if ((value != null) && (value.length() > 0)) {
145             fOut.print(" ");
146             fOut.print(label);
147             fOut.print("=");
148             printString(value);
149         }
150     }
151
152     /**
153      * Print a name, with a space in front of it, if not null.
154      */

155     private void printName(String JavaDoc name) {
156         if (name != null) {
157             fOut.print(" ");
158             fOut.print(name);
159         }
160     }
161
162     /**
163      * Print node name (override to add additional information).
164      */

165     protected void printNodeName(Node JavaDoc node) {
166         fOut.print(JavaLang.simpleClassName(node.getClass().getName()));
167     }
168
169     /**
170      * Print the basic information displayed about every node.
171      */

172     private void printNodeInfo(Node JavaDoc node) {
173         // Print node class name without package.
174
printIndent();
175         printNodeName(node);
176         fOut.print(':');
177         String JavaDoc namespaceURI = node.getNamespaceURI();
178         if ((namespaceURI != null) && (namespaceURI.length() > 0)) {
179             printLabelValue("NS", namespaceURI);
180             fOut.print(":");
181         }
182     }
183
184     /**
185      * Process children.
186      */

187     private void processChildren(Node JavaDoc node) {
188         level++;
189         fTraverser.processChildren(node);
190         level--;
191     }
192
193     /**
194      * Determine if an element has attributes that are to be printed.
195      */

196     protected boolean hasPrintAttributes(Element JavaDoc element) {
197         NamedNodeMap JavaDoc attrs = element.getAttributes();
198         if (attrs == null) {
199             return false;
200         }
201         if (fAllAttributes) {
202             return attrs.getLength() > 0;
203         }
204         // Check for specified attributes.
205
for (int i = 0; i < attrs.getLength(); i++) {
206             if (((Attr JavaDoc)attrs.item(i)).getSpecified()) {
207                 return true;
208             }
209         }
210         return false;
211     }
212
213     /**
214      * Handler called for Document nodes.
215      */

216     public void handleDocument(Document JavaDoc document) {
217         printNodeInfo(document);
218         fOut.println();
219
220         level++;
221         fTraverser.processDocumentType(document);
222         level--;
223         processChildren(document);
224     }
225
226     /**
227      * Handler called for DocumentType nodes.
228      */

229     public void handleDocumentType(DocumentType JavaDoc documentType) {
230         printNodeInfo(documentType);
231         printLabelValue("name", documentType.getNodeName());
232         fOut.println();
233
234         String JavaDoc internalSubset = documentType.getInternalSubset();
235         if ((internalSubset != null) && (internalSubset.length() > 0)) {
236             printIndent();
237             printLabelValue("internalSubset", internalSubset);
238             fOut.println();
239         }
240
241         level++;
242         fTraverser.processDocumentTypeContents(documentType);
243         // It is non-standard to have children of a document type,
244
// however Xerces uses this for grammar.
245
processChildren(documentType);
246         level--;
247     }
248
249     /**
250      * Handler called for DocumentFragment nodes.
251      */

252     public void handleDocumentFragment(DocumentFragment JavaDoc documentFragment) {
253         printNodeInfo(documentFragment);
254         printLabelValue("name", documentFragment.getNodeName());
255         fOut.println();
256
257         processChildren(documentFragment);
258     }
259
260     /**
261      * Print an attribute in a terse manner.
262      */

263     private void printTerseAttrInfo(Node JavaDoc attr) {
264         printName(attr.getNodeName());
265         fOut.print("=\"");
266         printString(attr.getNodeValue());
267         fOut.print("\"");
268     }
269
270     /**
271      * Print an attribute in a verbose manner.
272      */

273     private void printVerboseAttrInfo(Node JavaDoc attr) {
274         printNodeInfo(attr);
275         printName(attr.getNodeName());
276         if (!((Attr JavaDoc)attr).getSpecified()) {
277             fOut.print(" (unspecified)");
278         }
279         fOut.println();
280     }
281
282     /**
283      * Handler called for Attr nodes.
284      */

285     public void handleAttr(Attr JavaDoc attr) {
286         if (fVerboseAttributes) {
287             printVerboseAttrInfo(attr);
288         } else {
289             printTerseAttrInfo(attr);
290         }
291
292         fAttrSkipPrint = !fVerboseAttributes;
293         processChildren(attr);
294         fAttrSkipPrint = false;
295     }
296
297     /**
298      * Handler called for Entity nodes.
299      */

300     public void handleEntity(Entity JavaDoc entity) {
301         printNodeInfo(entity);
302         printLabelValue("name", entity.getNodeName());
303         printLabelValue("notationName", entity.getNotationName());
304         printLabelValue("systemId", entity.getPublicId());
305         printLabelValue("publicId", entity.getPublicId());
306         fOut.println();
307
308         processChildren(entity);
309     }
310
311     /**
312      * Handler called for EntityReference nodes.
313      */

314     public void handleEntityReference(EntityReference JavaDoc entityRef) {
315         if (!fAttrSkipPrint) {
316             printNodeInfo(entityRef);
317             printLabelValue("name", entityRef.getNodeName());
318             fOut.println();
319         }
320         processChildren(entityRef);
321     }
322
323     /**
324      * Process an element's attributes.
325      */

326     private void processAttributes(Element JavaDoc element) {
327         if (fVerboseAttributes) {
328             fOut.println();
329             if (hasPrintAttributes(element)) {
330                 level++;
331                 printIndent();
332                 fOut.println("Attributes:");
333                 level++;
334                 fTraverser.processAttributes(element);
335                 level -= 2;
336             }
337         } else {
338             if (hasPrintAttributes(element)) {
339                 fOut.print(":");
340                 fTraverser.processAttributes(element);
341             }
342             fOut.println();
343         }
344     }
345
346     /**
347      * Handler called for Element nodes.
348      */

349     public void handleElement(Element JavaDoc element) {
350         printNodeInfo(element);
351         printName(element.getNodeName());
352
353         processAttributes(element);
354         processChildren(element);
355     }
356
357     /**
358      * Handler called for Notation nodes.
359      */

360     public void handleNotation(Notation JavaDoc notation) {
361         if (!fAttrSkipPrint) {
362             printNodeInfo(notation);
363             printLabelValue("systemId", notation.getSystemId());
364             printLabelValue("publicId", notation.getPublicId());
365         }
366     }
367
368     /**
369      * Handler called for ProcessingInstruction nodes.
370      */

371     public void handleProcessingInstruction(ProcessingInstruction JavaDoc pi) {
372         if (!fAttrSkipPrint) {
373             printNodeInfo(pi);
374             printLabelValue("target", pi.getTarget());
375             printLabelValue("data", pi.getData());
376             fOut.println();
377         }
378     }
379
380     /**
381      * Print information about any of the CharacterData nodes.
382      */

383     private void printCharacterDataInfo(CharacterData JavaDoc charData) {
384         if (!fAttrSkipPrint) {
385             printNodeInfo(charData);
386             fOut.print(" ");
387             String JavaDoc data = charData.getData();
388             if (fTruncateText && (data.length() > TEXT_TRUNCATE_SIZE)) {
389                 data = data.substring(0, TEXT_TRUNCATE_SIZE);
390             }
391             printString(data);
392             fOut.println();
393         }
394     }
395
396     /**
397      * Handler called for CDATASection nodes.
398      */

399     public void handleCDATASection(CDATASection JavaDoc cdata) {
400         printCharacterDataInfo(cdata);
401     }
402
403     /**
404      * Handler called for Comment nodes.
405      */

406     public void handleComment(Comment JavaDoc comment) {
407         printCharacterDataInfo(comment);
408     }
409
410     /**
411      * Handler called for Text nodes.
412      */

413     public void handleText(Text JavaDoc text) {
414         printCharacterDataInfo(text);
415     }
416 }
417
Popular Tags