KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > util > XMLDebugOutputer


1 /*
2  * $Header:
3  * $Revision:
4  * $Date:
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.util;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import java.io.*;
36 import org.xml.sax.InputSource JavaDoc;
37
38
39
40 import java.util.StringTokenizer JavaDoc;
41
42 /**
43  * XMLDebugOutputer helper class.
44  */

45 public class XMLDebugOutputer {
46     
47     // -------------------------------------------------------------- Constants
48

49     // blanks for indent (80 char)
50
private final static String JavaDoc INDENT = " ";
51     private int tabWidth = 3;
52
53
54     // ----------------------------------------------------- Instance Variables
55

56     // indicator of debug mode
57
private boolean debug = false;
58     
59     
60     // local parser
61
private DocumentBuilderFactory JavaDoc dbf=null;
62     private DocumentBuilder JavaDoc db=null;
63             
64
65     // ----------------------------------------------------------- Constructors
66

67     /**
68      * Constructor
69      */

70     public XMLDebugOutputer() {
71
72         try {
73              dbf=DocumentBuilderFactory.newInstance();
74              db =dbf.newDocumentBuilder();
75             
76         } catch (ParserConfigurationException JavaDoc e) {
77         }
78
79     // Document doc=db.parse(file);
80

81     };
82
83     // --------------------------------------------------------- Public Methods
84

85     /**
86      * Print the given document to debug output. If debug is set to true;
87      * @param doc
88      */

89     public void print(Document JavaDoc doc) {
90         
91         if (debug) {
92             Node JavaDoc root = doc.getDocumentElement();
93     
94             dispatchNode(root, 0);
95         }
96     }
97     
98     /**
99      * Print the given XML string to debug output. If debug is set to true;
100      * @param xmlString
101      */

102     public void print(String JavaDoc xmlString) {
103  
104         if (debug) {
105             try {
106                 Document JavaDoc doc = db.parse(new InputSource JavaDoc(new StringReader(xmlString)));
107                 print(doc);
108                
109             } catch (SAXException JavaDoc e) {
110             } catch (IOException e) {
111             }
112         }
113         
114         
115     }
116     
117     /**
118      * Set debug information.
119      * @param debug
120      */

121     public void setDebug(boolean debug) {
122         this.debug = debug;
123     }
124     
125     
126     // --------------------------------------------------------- Private Methods
127

128  
129     
130     private void dispatchNode(Node JavaDoc node, int level) {
131         switch (node.getNodeType()) {
132             case Node.CDATA_SECTION_NODE :
133                 printCDATANode(node, level);
134                 break;
135     
136             case Node.COMMENT_NODE :
137                 printCommentNode(node, level);
138                 break;
139     
140             case Node.TEXT_NODE :
141                 printTextNode(node, level);
142                 break;
143     
144             case Node.ELEMENT_NODE :
145                 printElementNode(node, level);
146                 break;
147     
148             default :
149                 break;
150         }
151     }
152     
153     private void printCDATANode(Node JavaDoc node, int level) {
154         System.out.print(INDENT.substring(0, level * tabWidth));
155         System.out.println("<![CDATA[");
156     
157         indentBlock(node.getNodeValue(), level + 1);
158     
159         System.out.print(INDENT.substring(0, level * tabWidth));
160         System.out.println("]]>");
161     }
162     
163     private void printTextNode(Node JavaDoc node, int level) {
164         indentBlock(node.getNodeValue(), level + 1);
165     }
166     
167     private void printCommentNode(Node JavaDoc node, int level) {
168         System.out.print(INDENT.substring(0, level * tabWidth));
169         System.out.println("<!-- ");
170         indentBlock(node.getNodeValue(), level + 1);
171         System.out.print(INDENT.substring(0, level * tabWidth));
172         System.out.println(" -->");
173     }
174     
175     private void printElementNode(Node JavaDoc node, int level) {
176         String JavaDoc name = node.getNodeName();
177         System.out.print(INDENT.substring(0, level * tabWidth));
178         System.out.print("<");
179         System.out.print(name);
180     
181         NamedNodeMap JavaDoc attributes = node.getAttributes();
182         for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
183             if (attributes.getLength() > 1) {
184                 System.out.println();
185                 System.out.print(INDENT.substring(0, (2 + level) * tabWidth));
186             } else {
187                 System.out.print(" ");
188             }
189     
190             Node JavaDoc attribute = attributes.item(i);
191             System.out.print(attribute.getNodeName());
192             System.out.print("=\"");
193             System.out.print(attribute.getNodeValue());
194             System.out.print("\"");
195         }
196         System.out.println(">");
197     
198         // in case of more than one attribute add a blank line as seperator
199
if (attributes.getLength() > 1) {
200             System.out.println();
201         }
202     
203         if (node.hasChildNodes()) {
204             NodeList JavaDoc children = node.getChildNodes();
205             for (int i = 0; i < children.getLength(); i++) {
206                 // recursive use of dispatchNode()
207
dispatchNode(children.item(i), level + 1);
208             }
209         }
210     
211         System.out.print(INDENT.substring(0, level * tabWidth));
212         System.out.print("</");
213         System.out.print(name);
214         System.out.println(">");
215     }
216
217     private void indentBlock(String JavaDoc block, int level) {
218         StringTokenizer JavaDoc linetok = new StringTokenizer JavaDoc(block.replace('\n', ' '));
219         int pos = level * tabWidth;
220     
221         if (linetok.countTokens() > 0) {
222             System.out.print(INDENT.substring(0, level * tabWidth));
223         }
224     
225         while (linetok.hasMoreTokens()) {
226             String JavaDoc token = linetok.nextToken();
227             pos += (token.length() + 1);
228             if (pos < 80 && token.length() < (80 - (level * tabWidth))) {
229                 if (linetok.countTokens() > 0) {
230                     System.out.print(token);
231                     System.out.print(" ");
232                 } else {
233                     System.out.println(token);
234                 }
235             } else {
236                 System.out.println(token);
237                 if (linetok.countTokens() > 0) {
238                     System.out.print(INDENT.substring(0, level * tabWidth));
239                 }
240                 pos = level * tabWidth;
241             }
242         }
243     }
244
245
246 }
247
Popular Tags