|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
BodyBuilder.java | 100% | 100% | 100% | 100% |
|
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;
|
|
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 _buffer = new StringBuffer(DEFAULT_LENGTH); |
|
35 |
|
|
36 |
private static final String 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 | 4152 |
public void clear() |
46 |
{ |
|
47 | 4152 |
_nestingDepth = 0; |
48 | 4152 |
_atNewLine = true;
|
49 | 4152 |
_buffer.setLength(0); |
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Adds text to the current line, without terminating the line.
|
|
54 |
*/
|
|
55 | 26779 |
public void add(String text) |
56 |
{ |
|
57 | 26779 |
indent(); |
58 |
|
|
59 | 26779 |
_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 | 964 |
public void add(String pattern, Object[] arguments) |
73 |
{ |
|
74 | 964 |
add(MessageFormat.format(pattern, arguments)); |
75 |
} |
|
76 |
|
|
77 |
/**
|
|
78 |
* Convience for {@link #add(String, Object[])}
|
|
79 |
*/
|
|
80 |
|
|
81 | 1 |
public void add(String pattern, Object arg0) |
82 |
{ |
|
83 | 1 |
add(pattern, new Object[]
|
84 |
{ arg0 }); |
|
85 |
} |
|
86 |
|
|
87 |
/**
|
|
88 |
* Convience for {@link #add(String, Object[])}
|
|
89 |
*/
|
|
90 |
|
|
91 | 962 |
public void add(String pattern, Object arg0, Object arg1) |
92 |
{ |
|
93 | 962 |
add(pattern, new Object[]
|
94 |
{ arg0, arg1 }); |
|
95 |
} |
|
96 |
|
|
97 |
/**
|
|
98 |
* Convience for {@link #add(String, Object[])}
|
|
99 |
*/
|
|
100 |
|
|
101 | 1 |
public void add(String pattern, Object arg0, Object arg1, Object arg2) |
102 |
{ |
|
103 | 1 |
add(pattern, new Object[]
|
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 | 3 |
public void addln(String pattern, Object[] arguments) |
118 |
{ |
|
119 | 3 |
addln(MessageFormat.format(pattern, arguments)); |
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* Convience for {@link #addln(String, Object[])}
|
|
124 |
*/
|
|
125 |
|
|
126 | 1 |
public void addln(String pattern, Object arg0) |
127 |
{ |
|
128 | 1 |
addln(pattern, new Object[]
|
129 |
{ arg0 }); |
|
130 |
} |
|
131 |
|
|
132 |
/**
|
|
133 |
* Convience for {@link #addln(String, Object[])}.
|
|
134 |
*/
|
|
135 |
|
|
136 | 1 |
public void addln(String pattern, Object arg0, Object arg1) |
137 |
{ |
|
138 | 1 |
addln(pattern, new Object[]
|
139 |
{ arg0, arg1 }); |
|
140 |
} |
|
141 |
|
|
142 |
/**
|
|
143 |
* Convience for {@link #addln(String, Object[])}.
|
|
144 |
*/
|
|
145 |
|
|
146 | 1 |
public void addln(String pattern, Object arg0, Object arg1, Object arg2) |
147 |
{ |
|
148 | 1 |
addln(pattern, new Object[]
|
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 | 55 |
public void addQuoted(String text) |
158 |
{ |
|
159 | 55 |
indent(); |
160 | 55 |
_buffer.append(QUOTE); |
161 | 55 |
_buffer.append(text); |
162 | 55 |
_buffer.append(QUOTE); |
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Adds the text to the current line, and terminates the line.
|
|
167 |
*/
|
|
168 |
|
|
169 | 10893 |
public void addln(String text) |
170 |
{ |
|
171 | 10893 |
add(text); |
172 |
|
|
173 | 10893 |
newline(); |
174 |
} |
|
175 |
|
|
176 | 30625 |
private void newline() |
177 |
{ |
|
178 | 30625 |
_buffer.append("\n");
|
179 | 30625 |
_atNewLine = true;
|
180 |
} |
|
181 |
|
|
182 |
/**
|
|
183 |
* Begins a new block. Emits a "{", properly indented, on a new line.
|
|
184 |
*/
|
|
185 | 7968 |
public void begin() |
186 |
{ |
|
187 | 7968 |
if (!_atNewLine)
|
188 | 937 |
newline(); |
189 |
|
|
190 | 7968 |
indent(); |
191 | 7968 |
_buffer.append("{");
|
192 | 7968 |
newline(); |
193 |
|
|
194 | 7968 |
_nestingDepth++; |
195 |
} |
|
196 |
|
|
197 |
/**
|
|
198 |
* Ends the current block. Emits a "}", propertly indented, on a new line.
|
|
199 |
*/
|
|
200 | 7968 |
public void end() |
201 |
{ |
|
202 | 7968 |
if (!_atNewLine)
|
203 | 2859 |
newline(); |
204 |
|
|
205 | 7968 |
_nestingDepth--; |
206 |
|
|
207 | 7968 |
indent(); |
208 | 7968 |
_buffer.append("}");
|
209 |
|
|
210 | 7968 |
newline(); |
211 |
} |
|
212 |
|
|
213 | 42770 |
private void indent() |
214 |
{ |
|
215 | 42770 |
if (_atNewLine)
|
216 |
{ |
|
217 | 31592 |
for (int i = 0; i < _nestingDepth; i++) |
218 | 22177 |
_buffer.append(INDENT); |
219 |
|
|
220 | 31592 |
_atNewLine = false;
|
221 |
} |
|
222 |
} |
|
223 |
|
|
224 |
/**
|
|
225 |
* Returns the current contents of the buffer.
|
|
226 |
*/
|
|
227 | 7065 |
public String toString()
|
228 |
{ |
|
229 | 7065 |
return _buffer.toString();
|
230 |
} |
|
231 |
} |
|