KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > parsers > ParseTracer


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: ParseTracer.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.parsers;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import org.enhydra.xml.xmlc.codegen.IndentWriter;
31
32 /**
33  * Object used to trace parser calls. Handles indentation for logging the
34  * parsing of nested elements. This also is a PrintWriter, so it can be
35  * be passed to methods not expecting a ParseTracer.
36  * @see PrintWriter
37  */

38 public class ParseTracer extends IndentWriter {
39     /**
40      * Is tracing enabled?
41      */

42     private boolean fEnabled;
43      
44     /**
45      * Writer that discards all output, used to pass to super class if
46      * disabled. Normally not called, as explict enable checks are done
47      * in in commonly used methods.
48      */

49     private static class NullWriter extends Writer JavaDoc {
50         /** Constructor */
51         public NullWriter() {
52         }
53
54         /** @see Writer#write(int) */
55         public void write(int c) throws IOException JavaDoc {
56         }
57
58         /** @see Writer#write(char[],int,int) */
59         public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
60         }
61
62         /** @see Writer#flush */
63         public void flush() throws IOException JavaDoc {
64         }
65
66         /** @see Writer#close */
67         public void close() throws IOException JavaDoc {
68         }
69     }
70
71     /**
72      * Create tracer. If traceOut is null, it is created
73      * disabled.
74      */

75     public ParseTracer(PrintWriter JavaDoc traceOut) {
76         super(((traceOut != null) ? traceOut
77                : new PrintWriter JavaDoc(new NullWriter())), true);
78         fEnabled = (traceOut != null);
79         setZeroCheck(false);
80     }
81
82     /**
83      * Determine if tracing is enabled.
84      */

85     public final boolean enabled() {
86         return fEnabled;
87     }
88
89     /**
90      * Format a right-justified number.
91      * FIXME: should be a utility somewhere.
92      */

93     private String JavaDoc formatNumber(int number,
94                                 int width) {
95         String JavaDoc num = Integer.toString(number);
96         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(num.length()+width);
97         for (int cnt = width-num.length(); cnt >= 0; cnt--) {
98             buf.append(' ');
99         }
100         buf.append(num);
101         return buf.toString();
102     }
103
104     /**
105      * Generate trace output with indentation.
106      */

107     public void trace(String JavaDoc str) {
108         if (fEnabled) {
109             printPrefix(formatNumber(getIndentLevel(), 2) + ">");
110             super.println(str);
111         }
112     }
113
114     /**
115      * Print a string, indenting at the beginning of lines.
116      */

117     public void print(String JavaDoc str) {
118         if (fEnabled) {
119             super.print(str);
120         }
121     }
122
123     /**
124      * Print a newline.
125      */

126     public void println() {
127         if (fEnabled) {
128             super.println();
129         }
130     }
131
132     /**
133      * Print a string and newline, indenting at the beginning of lines.
134      */

135     public void println(String JavaDoc str) {
136         if (fEnabled) {
137             super.println(str);
138         }
139     }
140 }
141
Popular Tags