KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > BodyBuilder


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service;
16
17 import java.text.MessageFormat JavaDoc;
18
19 /**
20  * Utility class for assembling the <em>body</em> used with Javassist as a method or catch block.
21  *
22  * @author Howard Lewis Ship
23  */

24
25 public class BodyBuilder
26 {
27     /**
28      * Feels right for the size of a typical body.
29      */

30     private static final int DEFAULT_LENGTH = 200;
31
32     private static final char QUOTE = '"';
33
34     private StringBuffer JavaDoc _buffer = new StringBuffer JavaDoc(DEFAULT_LENGTH);
35
36     private static final String JavaDoc INDENT = " ";
37
38     private int _nestingDepth = 0;
39
40     private boolean _atNewLine = true;
41
42     /**
43      * Clears the builder, returning it to its initial, empty state.
44      */

45     public void clear()
46     {
47         _nestingDepth = 0;
48         _atNewLine = true;
49         _buffer.setLength(0);
50     }
51
52     /**
53      * Adds text to the current line, without terminating the line.
54      */

55     public void add(String JavaDoc text)
56     {
57         indent();
58
59         _buffer.append(text);
60     }
61
62     /**
63      * Adds text to the current line, without terminating the line.
64      *
65      * @param pattern
66      * a string pattern, used with
67      * {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
68      * @param arguments
69      * arguments used witht the format string
70      */

71
72     public void add(String JavaDoc pattern, Object JavaDoc[] arguments)
73     {
74         add(MessageFormat.format(pattern, arguments));
75     }
76
77     /**
78      * Convience for {@link #add(String, Object[])}
79      */

80
81     public void add(String JavaDoc pattern, Object JavaDoc arg0)
82     {
83         add(pattern, new Object JavaDoc[]
84         { arg0 });
85     }
86
87     /**
88      * Convience for {@link #add(String, Object[])}
89      */

90
91     public void add(String JavaDoc pattern, Object JavaDoc arg0, Object JavaDoc arg1)
92     {
93         add(pattern, new Object JavaDoc[]
94         { arg0, arg1 });
95     }
96
97     /**
98      * Convience for {@link #add(String, Object[])}
99      */

100
101     public void add(String JavaDoc pattern, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
102     {
103         add(pattern, new Object JavaDoc[]
104         { arg0, arg1, arg2 });
105     }
106
107     /**
108      * Adds text to the current line then terminates the line.
109      *
110      * @param pattern
111      * a string pattern, used with
112      * {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
113      * @param arguments
114      * arguments used witht the format string
115      */

116
117     public void addln(String JavaDoc pattern, Object JavaDoc[] arguments)
118     {
119         addln(MessageFormat.format(pattern, arguments));
120     }
121
122     /**
123      * Convience for {@link #addln(String, Object[])}
124      */

125
126     public void addln(String JavaDoc pattern, Object JavaDoc arg0)
127     {
128         addln(pattern, new Object JavaDoc[]
129         { arg0 });
130     }
131
132     /**
133      * Convience for {@link #addln(String, Object[])}.
134      */

135
136     public void addln(String JavaDoc pattern, Object JavaDoc arg0, Object JavaDoc arg1)
137     {
138         addln(pattern, new Object JavaDoc[]
139         { arg0, arg1 });
140     }
141
142     /**
143      * Convience for {@link #addln(String, Object[])}.
144      */

145
146     public void addln(String JavaDoc pattern, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
147     {
148         addln(pattern, new Object JavaDoc[]
149         { arg0, arg1, arg2 });
150     }
151
152     /**
153      * Adds the text to the current line, surrounded by double quotes.
154      * <em>Does not escape quotes in the text</em>.
155      */

156
157     public void addQuoted(String JavaDoc text)
158     {
159         indent();
160         _buffer.append(QUOTE);
161         _buffer.append(text);
162         _buffer.append(QUOTE);
163     }
164
165     /**
166      * Adds the text to the current line, and terminates the line.
167      */

168
169     public void addln(String JavaDoc text)
170     {
171         add(text);
172
173         newline();
174     }
175
176     private void newline()
177     {
178         _buffer.append("\n");
179         _atNewLine = true;
180     }
181
182     /**
183      * Begins a new block. Emits a "{", properly indented, on a new line.
184      */

185     public void begin()
186     {
187         if (!_atNewLine)
188             newline();
189
190         indent();
191         _buffer.append("{");
192         newline();
193
194         _nestingDepth++;
195     }
196
197     /**
198      * Ends the current block. Emits a "}", propertly indented, on a new line.
199      */

200     public void end()
201     {
202         if (!_atNewLine)
203             newline();
204
205         _nestingDepth--;
206
207         indent();
208         _buffer.append("}");
209
210         newline();
211     }
212
213     private void indent()
214     {
215         if (_atNewLine)
216         {
217             for (int i = 0; i < _nestingDepth; i++)
218                 _buffer.append(INDENT);
219
220             _atNewLine = false;
221         }
222     }
223
224     /**
225      * Returns the current contents of the buffer.
226      */

227     public String JavaDoc toString()
228     {
229         return _buffer.toString();
230     }
231 }
Popular Tags