KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > generator > io > FormattedWriter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * FormattedWriter.java
26  *
27  * Created on November 14, 2001, 5:50 PM
28  */

29
30 package com.sun.jdo.spi.persistence.utility.generator.io;
31
32 import java.util.*;
33
34
35 /**
36  *
37  * @author raccah
38  */

39 class FormattedWriter
40 {
41     static private final String JavaDoc lineSeparator =
42         System.getProperty("line.separator");
43     static private final String JavaDoc indent = " "; // NOI18N
44

45     private StringBuffer JavaDoc _buffer;
46     private int _initialIndents = 0;
47
48     /** Creates new FormattedWriter */
49     FormattedWriter ()
50     {
51     }
52
53     private StringBuffer JavaDoc getBuffer ()
54     {
55         if (_buffer == null)
56             _buffer = new StringBuffer JavaDoc();
57
58         return _buffer;
59     }
60
61     /** Returns a string representation of the FormattedWriter.
62      * @return The string representation of the internal StringBuffer
63      * used by this object.
64      */

65     public String JavaDoc toString () { return getBuffer().toString(); }
66
67     void writeComments (final String JavaDoc[] comments)
68     {
69         final int n = (comments != null ? comments.length : 0);
70
71         for (int i = 0; i < n; i++)
72         {
73             final String JavaDoc s = comments[i];
74
75             writeln("// " + (s != null ? s : "")); // NOI18N
76
}
77     }
78
79     private void _write (final int indents, final String JavaDoc s)
80     {
81         final StringBuffer JavaDoc buffer = getBuffer();
82
83         if (!s.equals(lineSeparator))
84         {
85             for (int i = 0; i < indents; i++)
86                 buffer.append(indent);
87         }
88
89         buffer.append(s);
90     }
91
92     void write (final int indents, final String JavaDoc s)
93     {
94         _write(indents + _initialIndents, s);
95     }
96
97     void write (final String JavaDoc s)
98     {
99         _write(0, s);
100     }
101
102     void writeln (final int indents, final String JavaDoc s)
103     {
104         if (_initialIndents > 0)
105             _write(_initialIndents, ""); // NOI18N
106

107         _write(indents, s + lineSeparator);
108     }
109
110     void writeln (final String JavaDoc s)
111     {
112         writeln(0, s);
113     }
114
115     void writeln ()
116     {
117         writeln(0, ""); // NOI18N
118
}
119
120     void writeList (final int indents, final List list,
121         final boolean addSeparator)
122     {
123         if ((list != null) && (list.size() > 0))
124         {
125             Iterator iterator = list.iterator();
126     
127             while (iterator.hasNext())
128             {
129                 indent(indents, iterator.next().toString());
130
131                 if (addSeparator)
132                     writeln();
133             }
134
135             if (!addSeparator)
136                 writeln();
137         }
138     }
139
140     void writeList (final int indents, final List list)
141     {
142         writeList(indents, list, false);
143     }
144
145     void writeList (final List list)
146     {
147         writeList(0, list);
148     }
149
150     private void indent (final int indents, final String JavaDoc s)
151     {
152         if (s.indexOf(lineSeparator) != -1)
153         {
154             StringTokenizer tokenizer =
155                 new StringTokenizer(s, lineSeparator, true);
156
157             while (tokenizer.hasMoreTokens())
158                 write(indents, tokenizer.nextToken());
159         }
160         else
161             write(indents, s);
162     }
163 }
164
Popular Tags