001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.schema2beansdev.gen;
043:
044: import java.util.*;
045: import java.io.*;
046:
047: public class IndentingWriter extends GenBuffer {
048: protected boolean crDone[];
049: protected int indentLevel[];
050: protected String indentString = "\t";
051:
052: public IndentingWriter(int bufferCount) {
053: super (bufferCount);
054: crDone = new boolean[bufferCount];
055: indentLevel = new int[bufferCount];
056: privateInit();
057: }
058:
059: public IndentingWriter(IndentingWriter source) {
060: super (source);
061: indentString = source.indentString;
062: crDone = new boolean[bufferCount];
063: indentLevel = new int[bufferCount];
064: for (int i = 0; i < bufferCount; i++) {
065: crDone[i] = source.crDone[i];
066: indentLevel[i] = source.indentLevel[i];
067: }
068: }
069:
070: public void reset() {
071: super .reset();
072: privateInit();
073: }
074:
075: private void privateInit() {
076: for (int i = 0; i < bufferCount; i++) {
077: crDone[i] = true;
078: indentLevel[i] = 0;
079: }
080: }
081:
082: public void writeTo(GenBuffer o) {
083: super .writeTo(o);
084: if (o instanceof IndentingWriter) {
085: IndentingWriter out = (IndentingWriter) o;
086: int minInCommonBufferCount = bufferCount;
087: if (out.bufferCount < bufferCount)
088: minInCommonBufferCount = out.bufferCount;
089: for (int i = 0; i < minInCommonBufferCount; i++) {
090: out.crDone[i] = crDone[i];
091: out.indentLevel[i] = indentLevel[i];
092: }
093: }
094: }
095:
096: /**
097: * Insert some additional buffers.
098: * Previous buffers are not adjusted automatically.
099: * select() should be called afterwards to reestablish current buffer.
100: */
101: public void insertAdditionalBuffers(int offset, int count) {
102: boolean[] newCrDone = new boolean[bufferCount + count];
103: // copy before and including offset
104: System.arraycopy(crDone, 0, newCrDone, 0, offset + 1);
105: // copy after offset
106: System.arraycopy(crDone, offset + 1, newCrDone, offset + 1
107: + count, bufferCount - offset - 1);
108: // init the new elements
109: for (int i = 0; i < count; ++i) {
110: newCrDone[offset + 1 + i] = true;
111: }
112: crDone = newCrDone;
113:
114: int[] newIndentLevel = new int[bufferCount + count];
115: // copy before and including offset
116: System.arraycopy(indentLevel, 0, newIndentLevel, 0, offset + 1);
117: // copy after offset
118: System.arraycopy(indentLevel, offset + 1, newIndentLevel,
119: offset + 1 + count, bufferCount - offset - 1);
120: // init the new elements
121: for (int i = 0; i < count; ++i) {
122: newIndentLevel[offset + 1 + i] = 0;
123: }
124: indentLevel = newIndentLevel;
125:
126: super .insertAdditionalBuffers(offset, count);
127: }
128:
129: public void setIndent(String indent) {
130: this .indentString = indent;
131: }
132:
133: public String getIndent() {
134: return indentString;
135: }
136:
137: public void cr() throws IOException {
138: listOut[curOut].append("\n");
139: crDone[curOut] = true;
140: }
141:
142: public void write(String str) throws IOException {
143: int len = str.length();
144: if (len == 0)
145: return;
146: char lastChar = str.charAt(len - 1);
147: if (lastChar == '\n') {
148: char firstChar = str.charAt(0);
149: char secondLastChar = (len <= 1) ? ' ' : str
150: .charAt(len - 2);
151: if (firstChar == '}' || secondLastChar == '}') {
152: indentLeft();
153: }
154: super .write(str.substring(0, len - 1));
155: cr();
156: if (secondLastChar == '{') {
157: indentRight();
158: }
159: } else {
160: super .write(str);
161: }
162: }
163:
164: public void writecr(String str) throws IOException {
165: super .write(str);
166: cr();
167: }
168:
169: public void writecr(String s1, String s2) throws IOException {
170: super .write(s1, s2);
171: cr();
172: }
173:
174: public void writecr(String s1, String s2, String s3)
175: throws IOException {
176: super .write(s1, s2, s3);
177: cr();
178: }
179:
180: public void writecr(String s1, String s2, String s3, String s4)
181: throws IOException {
182: super .write(s1, s2, s3, s4);
183: cr();
184: }
185:
186: public void indentRight() {
187: ++indentLevel[curOut];
188: }
189:
190: public void indentLeft() {
191: --indentLevel[curOut];
192: }
193:
194: protected void beforeWriteHook() {
195: if (crDone[curOut]) {
196: indent();
197: crDone[curOut] = false;
198: }
199: }
200:
201: /**
202: * Adds the indentString to the current buffer.
203: */
204: public void indentOneLevel() {
205: listOut[curOut].append(indentString);
206: }
207:
208: /**
209: * Adds indentLevel[curOut] number of indentString's to the current
210: * buffer. Put another way, this will indent from the left margin to
211: * the current indention level.
212: */
213: public void indent() {
214: // This must not call a write as beforeWriteHook calls us
215: for (int i = 0; i < indentLevel[curOut]; ++i)
216: indentOneLevel();
217: }
218: }
|