KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > IndentingWriter


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

19
20 import java.io.IOException JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 /**
24  * The IndentingWriter class is a simple implementation of an indenting code writer
25  */

26 public class IndentingWriter extends Writer JavaDoc
27 {
28     /** current depth:
29      * <PRE>
30      * // depth = 0;
31      * {
32      * // depth now is 2
33      * {
34      * // depth now is 4
35      * }
36      * // depth now is 2
37      * }
38      * // depth now is 0
39      * </PRE>
40      */

41     protected int depth = 0;
42
43     public IndentingWriter(Writer JavaDoc delegate)
44     {
45         this(delegate,
46              Integer.getInteger("org.apache.beehive.controls.runtime.generator.indentLevel", 4).intValue());
47     }
48
49     public IndentingWriter(Writer JavaDoc delegate, int indentLevel)
50     {
51         _out = delegate;
52         this._indentLevel = indentLevel;
53     }
54
55     public void write(char cbuf[], int off, int len) throws IOException JavaDoc
56     {
57         if (off < 0 || off + len > cbuf.length)
58             throw new ArrayIndexOutOfBoundsException JavaDoc();
59
60         for (int i = off; i < off + len; i++)
61         {
62             char c = cbuf[i];
63             if (c == '}')
64             {
65                 decrDepth();
66             }
67             else if ((c == ' ' || c == '\t') && _needIndent)
68             {
69                 continue;
70             }
71
72             if (_needIndent)
73                 indent();
74             _out.write(c);
75
76             if (c == '\n')
77             {
78                 _needIndent = true;
79             }
80             else if (c == '{')
81             {
82                 incrDepth();
83             }
84         }
85     }
86
87     public void flush() throws IOException JavaDoc
88     {
89         _out.flush();
90     }
91
92     public void close() throws IOException JavaDoc
93     {
94         _out.close();
95     }
96
97     private void indent() throws IOException JavaDoc
98     {
99         for (int i = 0; i < depth; i++)
100         {
101             _out.write(' ');
102         }
103         _needIndent = false;
104     }
105
106     private void incrDepth()
107     {
108         depth += _indentLevel;
109     }
110
111     private void decrDepth()
112     {
113         depth -= _indentLevel;
114         if (depth < 0)
115             depth = 0;
116     }
117
118     protected Writer JavaDoc _out;
119     protected int _indentLevel;
120     private boolean _needIndent = false;
121 }
122
Popular Tags