|
|||||||||||||||||||
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 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
|
|
21 |
* Javassist as a method or catch block.
|
|
22 |
*
|
|
23 |
* @author Howard Lewis Ship
|
|
24 |
*/
|
|
25 |
|
|
26 |
public class BodyBuilder |
|
27 |
{ |
|
28 |
/**
|
|
29 |
* Feels right for the size of a typical body.
|
|
30 |
*/
|
|
31 |
private static final int DEFAULT_LENGTH = 200; |
|
32 |
|
|
33 |
private static final char QUOTE = '"'; |
|
34 |
|
|
35 |
private StringBuffer _buffer = new StringBuffer(DEFAULT_LENGTH); |
|
36 |
|
|
37 |
private static final String INDENT = " "; |
|
38 |
|
|
39 |
private int _nestingDepth = 0; |
|
40 |
private boolean _atNewLine = true; |
|
41 |
|
|
42 |
/**
|
|
43 |
* Clears the builder, returning it to its initial, empty state.
|
|
44 |
*/
|
|
45 | 2904 |
public void clear() |
46 |
{ |
|
47 | 2904 |
_nestingDepth = 0; |
48 | 2904 |
_atNewLine = true;
|
49 | 2904 |
_buffer.setLength(0); |
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Adds text to the current line, without terminating the line.
|
|
54 |
*/
|
|
55 | 18092 |
public void add(String text) |
56 |
{ |
|
57 | 18092 |
indent(); |
58 |
|
|
59 | 18092 |
_buffer.append(text); |
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* Adds text to the current line, without terminating the line.
|
|
64 |
*
|
|
65 |
* @param pattern a string pattern, used with {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
|
|
66 |
* @param arguments arguments used witht the format string
|
|
67 |
*/
|
|
68 |
|
|
69 | 3 |
public void add(String pattern, Object[] arguments) |
70 |
{ |
|
71 | 3 |
add(MessageFormat.format(pattern, arguments)); |
72 |
} |
|
73 |
|
|
74 |
/**
|
|
75 |
* Convience for {@link #add(String, Object[])
|
|
76 |
*/
|
|
77 |
|
|
78 | 1 |
public void add(String pattern, Object arg0) |
79 |
{ |
|
80 | 1 |
add(pattern, new Object[] { arg0 });
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* Convience for {@link #add(String, Object[])
|
|
85 |
*/
|
|
86 |
|
|
87 | 1 |
public void add(String pattern, Object arg0, Object arg1) |
88 |
{ |
|
89 | 1 |
add(pattern, new Object[] { arg0, arg1 });
|
90 |
} |
|
91 |
|
|
92 |
/**
|
|
93 |
* Convience for {@link #add(String, Object[])
|
|
94 |
*/
|
|
95 |
|
|
96 | 1 |
public void add(String pattern, Object arg0, Object arg1, Object arg2) |
97 |
{ |
|
98 | 1 |
add(pattern, new Object[] { arg0, arg1, arg2 });
|
99 |
} |
|
100 |
|
|
101 |
/**
|
|
102 |
* Adds text to the current line then terminates the line.
|
|
103 |
*
|
|
104 |
* @param pattern a string pattern, used with {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
|
|
105 |
* @param arguments arguments used witht the format string
|
|
106 |
*/
|
|
107 |
|
|
108 | 3 |
public void addln(String pattern, Object[] arguments) |
109 |
{ |
|
110 | 3 |
addln(MessageFormat.format(pattern, arguments)); |
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* Convience for {@link #addln(String, Object[])
|
|
115 |
*/
|
|
116 |
|
|
117 | 1 |
public void addln(String pattern, Object arg0) |
118 |
{ |
|
119 | 1 |
addln(pattern, new Object[] { arg0 });
|
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* Convience for {@link #addln(String, Object[])
|
|
124 |
*/
|
|
125 |
|
|
126 | 1 |
public void addln(String pattern, Object arg0, Object arg1) |
127 |
{ |
|
128 | 1 |
addln(pattern, new Object[] { arg0, arg1 });
|
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Convience for {@link #addln(String, Object[])
|
|
133 |
*/
|
|
134 |
|
|
135 | 1 |
public void addln(String pattern, Object arg0, Object arg1, Object arg2) |
136 |
{ |
|
137 | 1 |
addln(pattern, new Object[] { arg0, arg1, arg2 });
|
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* Adds the text to the current line, surrounded by double quotes.
|
|
142 |
* <em>Does not escape quotes in the text</em>.
|
|
143 |
*/
|
|
144 |
|
|
145 | 52 |
public void addQuoted(String text) |
146 |
{ |
|
147 | 52 |
indent(); |
148 | 52 |
_buffer.append(QUOTE); |
149 | 52 |
_buffer.append(text); |
150 | 52 |
_buffer.append(QUOTE); |
151 |
} |
|
152 |
|
|
153 |
/**
|
|
154 |
* Adds the text to the current line, and terminates the line.
|
|
155 |
*/
|
|
156 |
|
|
157 | 7713 |
public void addln(String text) |
158 |
{ |
|
159 | 7713 |
add(text); |
160 |
|
|
161 | 7713 |
newline(); |
162 |
} |
|
163 |
|
|
164 | 21578 |
private void newline() |
165 |
{ |
|
166 | 21578 |
_buffer.append("\n");
|
167 | 21578 |
_atNewLine = true;
|
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* Begins a new block. Emits a "{", properly indented, on a new line.
|
|
172 |
*/
|
|
173 | 5596 |
public void begin() |
174 |
{ |
|
175 | 5596 |
if (!_atNewLine)
|
176 | 666 |
newline(); |
177 |
|
|
178 | 5596 |
indent(); |
179 | 5596 |
_buffer.append("{");
|
180 | 5596 |
newline(); |
181 |
|
|
182 | 5596 |
_nestingDepth++; |
183 |
} |
|
184 |
|
|
185 |
/**
|
|
186 |
* Ends the current block. Emits a "}", propertly indented, on a new line.
|
|
187 |
*/
|
|
188 | 5596 |
public void end() |
189 |
{ |
|
190 | 5596 |
if (!_atNewLine)
|
191 | 2007 |
newline(); |
192 |
|
|
193 | 5596 |
_nestingDepth--; |
194 |
|
|
195 | 5596 |
indent(); |
196 | 5596 |
_buffer.append("}");
|
197 |
|
|
198 | 5596 |
newline(); |
199 |
} |
|
200 |
|
|
201 | 29336 |
private void indent() |
202 |
{ |
|
203 | 29336 |
if (_atNewLine)
|
204 |
{ |
|
205 | 21584 |
for (int i = 0; i < _nestingDepth; i++) |
206 | 15706 |
_buffer.append(INDENT); |
207 |
|
|
208 | 21584 |
_atNewLine = false;
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
/**
|
|
213 |
* Returns the current contents of the buffer.
|
|
214 |
*/
|
|
215 | 4274 |
public String toString()
|
216 |
{ |
|
217 | 4274 |
return _buffer.toString();
|
218 |
} |
|
219 |
} |
|
220 |
|
|