KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > gen > IndentingWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beansdev.gen;
21
22 import java.util.*;
23 import java.io.*;
24
25 public class IndentingWriter extends GenBuffer {
26     protected boolean crDone[];
27     protected int indentLevel[];
28     protected String JavaDoc indentString = "\t";
29
30     public IndentingWriter(int bufferCount) {
31         super(bufferCount);
32         crDone = new boolean[bufferCount];
33         indentLevel = new int[bufferCount];
34         privateInit();
35     }
36
37     public IndentingWriter(IndentingWriter source) {
38         super(source);
39         indentString = source.indentString;
40         crDone = new boolean[bufferCount];
41         indentLevel = new int[bufferCount];
42         for (int i = 0; i < bufferCount; i++) {
43             crDone[i] = source.crDone[i];
44             indentLevel[i] = source.indentLevel[i];
45         }
46     }
47
48     public void reset() {
49         super.reset();
50         privateInit();
51     }
52
53     private void privateInit() {
54         for (int i = 0; i < bufferCount; i++) {
55             crDone[i] = true;
56             indentLevel[i] = 0;
57         }
58     }
59
60     public void writeTo(GenBuffer o) {
61         super.writeTo(o);
62         if (o instanceof IndentingWriter) {
63             IndentingWriter out = (IndentingWriter) o;
64             int minInCommonBufferCount = bufferCount;
65             if (out.bufferCount < bufferCount)
66                 minInCommonBufferCount = out.bufferCount;
67             for (int i = 0; i < minInCommonBufferCount; i++) {
68                 out.crDone[i] = crDone[i];
69                 out.indentLevel[i] = indentLevel[i];
70             }
71         }
72     }
73     
74     /**
75      * Insert some additional buffers.
76      * Previous buffers are not adjusted automatically.
77      * select() should be called afterwards to reestablish current buffer.
78      */

79     public void insertAdditionalBuffers(int offset, int count) {
80         boolean[] newCrDone = new boolean[bufferCount + count];
81         // copy before and including offset
82
System.arraycopy(crDone, 0, newCrDone, 0, offset+1);
83         // copy after offset
84
System.arraycopy(crDone, offset+1, newCrDone,
85                          offset + 1 + count, bufferCount - offset - 1);
86         // init the new elements
87
for (int i = 0; i < count; ++i) {
88             newCrDone[offset + 1 + i] = true;
89         }
90         crDone = newCrDone;
91
92         int[] newIndentLevel = new int[bufferCount + count];
93         // copy before and including offset
94
System.arraycopy(indentLevel, 0, newIndentLevel, 0, offset+1);
95         // copy after offset
96
System.arraycopy(indentLevel, offset+1, newIndentLevel,
97                          offset + 1 + count, bufferCount - offset - 1);
98         // init the new elements
99
for (int i = 0; i < count; ++i) {
100             newIndentLevel[offset + 1 + i] = 0;
101         }
102         indentLevel = newIndentLevel;
103
104         super.insertAdditionalBuffers(offset, count);
105     }
106
107     public void setIndent(String JavaDoc indent) {
108         this.indentString = indent;
109     }
110
111     public String JavaDoc getIndent() {
112         return indentString;
113     }
114
115     public void cr() throws IOException {
116         listOut[curOut].append("\n");
117         crDone[curOut] = true;
118     }
119
120     public void write(String JavaDoc str) throws IOException {
121         int len = str.length();
122         if (len == 0)
123             return;
124         char lastChar = str.charAt(len-1);
125         if (lastChar == '\n') {
126             char firstChar = str.charAt(0);
127             char secondLastChar = (len <= 1) ? ' ' : str.charAt(len-2);
128             if (firstChar == '}' || secondLastChar == '}') {
129                 indentLeft();
130             }
131             super.write(str.substring(0, len-1));
132             cr();
133             if (secondLastChar == '{') {
134                 indentRight();
135             }
136         } else {
137             super.write(str);
138         }
139     }
140
141     public void writecr(String JavaDoc str) throws IOException {
142         super.write(str);
143         cr();
144     }
145
146     public void writecr(String JavaDoc s1, String JavaDoc s2) throws IOException {
147         super.write(s1, s2);
148         cr();
149     }
150
151     public void writecr(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3) throws IOException {
152         super.write(s1, s2, s3);
153         cr();
154     }
155
156     public void writecr(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3, String JavaDoc s4) throws IOException {
157         super.write(s1, s2, s3, s4);
158         cr();
159     }
160
161     public void indentRight() {
162         ++indentLevel[curOut];
163     }
164
165     public void indentLeft() {
166         --indentLevel[curOut];
167     }
168
169     protected void beforeWriteHook() {
170         if (crDone[curOut]) {
171             indent();
172             crDone[curOut] = false;
173         }
174     }
175
176     /**
177      * Adds the indentString to the current buffer.
178      */

179     public void indentOneLevel() {
180         listOut[curOut].append(indentString);
181     }
182
183     /**
184      * Adds indentLevel[curOut] number of indentString's to the current
185      * buffer. Put another way, this will indent from the left margin to
186      * the current indention level.
187      */

188     public void indent() {
189         // This must not call a write as beforeWriteHook calls us
190
for (int i = 0; i < indentLevel[curOut]; ++i)
191             indentOneLevel();
192     }
193 }
194
Popular Tags