001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vasily Zakharov
021: * @version $Revision: 1.1.2.1 $
022: */package org.apache.harmony.rmi.compiler;
023:
024: import org.apache.harmony.rmi.internal.nls.Messages;
025:
026: /**
027: * Provides dynamic 4-space indents usable for source code generation.
028: * All indents are measured in 4-space units.
029: *
030: * This class' methods are designed to be called right from the output stream
031: * composition, i. e. <code>String output =
032: * ("first line" + indenter.increase() + "second line");</code>
033: *
034: * @author Vasily Zakharov
035: * @version $Revision: 1.1.2.1 $
036: */
037: final class Indenter {
038:
039: /**
040: * String used for indentation.
041: */
042: private final String stepString = " "; //$NON-NLS-1$
043:
044: /**
045: * Length of {@linkplain #stepString indentation string}.
046: */
047: private final int STEP_LENGTH = stepString.length();
048:
049: /**
050: * Indent string.
051: */
052: private String currentIndent;
053:
054: /**
055: * Create instance with zero starting indent.
056: */
057: Indenter() {
058: currentIndent = new String();
059: }
060:
061: /**
062: * Create instance with specified starting indent.
063: *
064: * @param indent
065: * Starting indent.
066: */
067: Indenter(int indent) {
068: this ();
069:
070: if (indent > 0) {
071: increase(indent);
072: }
073: }
074:
075: /**
076: * Returns current indent string.
077: *
078: * @return Current indent string.
079: */
080: String indent() {
081: return currentIndent;
082: }
083:
084: /**
085: * Increase current indent one step right.
086: *
087: * @return Current (increased) indent string.
088: */
089: String increase() {
090: return increase(1);
091: }
092:
093: /**
094: * Increase current indent the specified number of steps right.
095: *
096: * @param steps
097: * Number of steps to increase current indent.
098: *
099: * @return Current (increased) indent string.
100: */
101: String increase(int steps) {
102: return currentIndent = tIncrease(steps);
103: }
104:
105: /**
106: * Decrease current indent one step left.
107: *
108: * @return Current (decreased) indent string.
109: *
110: * @throws IndexOutOfBoundsException
111: * If current indent is empty and thus cannot be decreased.
112: */
113: String decrease() throws IndexOutOfBoundsException {
114: return decrease(1);
115: }
116:
117: /**
118: * Decrease current indent the specified number of steps left.
119: *
120: * @param steps
121: * Number of steps to decrease current indent.
122: *
123: * @return Current (decreased) indent string.
124: *
125: * @throws IndexOutOfBoundsException
126: * If current indent is empty and thus cannot be decreased.
127: */
128: String decrease(int steps) throws IndexOutOfBoundsException {
129: return currentIndent = tDecrease(steps);
130: }
131:
132: /**
133: * Increases current indent one step right, but returns empty string.
134: *
135: * @return Empty string.
136: */
137: String hIncrease() {
138: return hIncrease(1);
139: }
140:
141: /**
142: * Increases current indent the specified number of steps right,
143: * but returns empty string.
144: *
145: * @param steps
146: * Number of steps to increase current indent.
147: *
148: * @return Empty string.
149: */
150: String hIncrease(int steps) {
151: increase(steps);
152: return ""; //$NON-NLS-1$
153: }
154:
155: /**
156: * Decreases current indent one step left, but returns empty string.
157: *
158: * @return Empty string.
159: *
160: * @throws IndexOutOfBoundsException
161: * If current indent is empty and thus cannot be decreased.
162: */
163: String hDecrease() throws IndexOutOfBoundsException {
164: return hDecrease(1);
165: }
166:
167: /**
168: * Decreases current indent the specified number of steps left,
169: * but returns empty string.
170: *
171: * @param steps
172: * Number of steps to decrease indent.
173: *
174: * @return Empty string.
175: *
176: * @throws IndexOutOfBoundsException
177: * If current indent is empty and thus cannot be decreased.
178: */
179: String hDecrease(int steps) throws IndexOutOfBoundsException {
180: decrease(steps);
181: return ""; //$NON-NLS-1$
182: }
183:
184: /**
185: * Returns current indent temporary increased one step right.
186: * The stored indent is not changed.
187: *
188: * @return Increased indent string.
189: */
190: String tIncrease() {
191: return tIncrease(1);
192: }
193:
194: /**
195: * Returns current indent temporary increased the specified number of steps
196: * right. The stored indent is not changed.
197: *
198: * @param steps
199: * Number of steps to increase current indent.
200: *
201: * @return Increased indent string.
202: */
203: String tIncrease(int steps) {
204: StringBuffer buffer = new StringBuffer(currentIndent);
205:
206: for (int i = 0; i < steps; i++) {
207: buffer.append(stepString);
208: }
209:
210: return buffer.toString();
211: }
212:
213: /**
214: * Returns current indent temporary decreased one step left.
215: * The stored indent is not changed.
216: *
217: * @return Decreased indent string.
218: *
219: * @throws IndexOutOfBoundsException
220: * If current indent is empty and thus cannot be decreased.
221: */
222: String tDecrease() throws IndexOutOfBoundsException {
223: return tDecrease(1);
224: }
225:
226: /**
227: * Returns current indent temporary decreased the specified number of steps
228: * left. The stored indent is not changed.
229: *
230: * @param steps
231: * Number of steps to decrease indent.
232: *
233: * @return Decreased indent string.
234: *
235: * @throws IndexOutOfBoundsException
236: * If current indent is empty and thus cannot be decreased.
237: */
238: String tDecrease(int steps) throws IndexOutOfBoundsException {
239: return currentIndent.substring(0,
240: (currentIndent.length() - (steps * STEP_LENGTH)));
241: }
242:
243: /**
244: * Return empty string if current indent is empty.
245: * Throws {@link IllegalStateException} otherwise.
246: *
247: * @return Empty string.
248: *
249: * @throws IllegalStateException
250: * If current indent is not empty.
251: */
252: String assertEmpty() throws IllegalStateException {
253: if (currentIndent.length() != 0) {
254: // rmi.56=Indenter assertion failed: current indent is not empty
255: throw new IllegalStateException(Messages
256: .getString("rmi.56")); //$NON-NLS-1$
257: }
258: return ""; //$NON-NLS-1$
259: }
260: }
|