01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.contract.lang.parser;
28:
29: import java.io.*;
30: import java.util.*;
31:
32: import org.cougaar.lib.contract.lang.*;
33:
34: /**
35: * A simple <code>String</code> indent factory.
36: */
37: public class IndentFactory {
38:
39: private static String[] presetIndents;
40: static {
41: growPresetIndents();
42: }
43:
44: private IndentFactory() {
45: }
46:
47: /**
48: * Create a <code>String</code> representing a newline and an indent of
49: * <tt>(2*indent)</tt> spaces (' ').
50: * <p>
51: * For example, <tt>createIndent(3)</tt> is equal to <tt>"\n "</tt>.
52: */
53: public static final String createIndent(final int indent) {
54: while (true) {
55: try {
56: return presetIndents[indent];
57: } catch (ArrayIndexOutOfBoundsException e) {
58: // rare!
59: if (indent < 0)
60: throw new RuntimeException("NEGATIVE INDENT???");
61: growPresetIndents();
62: }
63: }
64: }
65:
66: /**
67: * Grow the <tt>presetIndents</tt> array, due to initialization or
68: * the request of a <tt>createIndent</tt> beyond the length of
69: * <tt>presetIndents</tt>.
70: */
71: private static void growPresetIndents() {
72: // synchronize?
73: int newLen = ((presetIndents != null) ? (2 * presetIndents.length)
74: : 10);
75: presetIndents = new String[newLen];
76: StringBuffer parensb = new StringBuffer(2 * newLen);
77: parensb.append("\n");
78: for (int i = 0; i < newLen; i++) {
79: presetIndents[i] = parensb.toString();
80: parensb.append(" ");
81: }
82: }
83: }
|