001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.service;
016    
017    import java.text.MessageFormat;
018    
019    /**
020     * Utility class for assembling the <em>body</em> used with Javassist as a method or catch block.
021     * 
022     * @author Howard Lewis Ship
023     */
024    
025    public class BodyBuilder
026    {
027        /**
028         * Feels right for the size of a typical body.
029         */
030        private static final int DEFAULT_LENGTH = 200;
031    
032        private static final char QUOTE = '"';
033    
034        private StringBuffer _buffer = new StringBuffer(DEFAULT_LENGTH);
035    
036        private static final String INDENT = "  ";
037    
038        private int _nestingDepth = 0;
039    
040        private boolean _atNewLine = true;
041    
042        /**
043         * Clears the builder, returning it to its initial, empty state.
044         */
045        public void clear()
046        {
047            _nestingDepth = 0;
048            _atNewLine = true;
049            _buffer.setLength(0);
050        }
051    
052        /**
053         * Adds text to the current line, without terminating the line.
054         */
055        public void add(String text)
056        {
057            indent();
058    
059            _buffer.append(text);
060        }
061    
062        /**
063         * Adds text to the current line, without terminating the line.
064         * 
065         * @param pattern
066         *            a string pattern, used with
067         *            {@link java.text.MessageFormat#format(java.lang.String, java.lang.Object[])}
068         * @param arguments
069         *            arguments used witht the format string
070         */
071    
072        public void add(String pattern, Object[] arguments)
073        {
074            add(MessageFormat.format(pattern, arguments));
075        }
076    
077        /**
078         * Convience for {@link #add(String, Object[])}
079         */
080    
081        public void add(String pattern, Object arg0)
082        {
083            add(pattern, new Object[]
084            { arg0 });
085        }
086    
087        /**
088         * Convience for {@link #add(String, Object[])}
089         */
090    
091        public void add(String pattern, Object arg0, Object arg1)
092        {
093            add(pattern, new Object[]
094            { arg0, arg1 });
095        }
096    
097        /**
098         * Convience for {@link #add(String, Object[])}
099         */
100    
101        public void add(String pattern, Object arg0, Object arg1, Object arg2)
102        {
103            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        public void addln(String pattern, Object[] 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 pattern, Object arg0)
127        {
128            addln(pattern, new Object[]
129            { arg0 });
130        }
131    
132        /**
133         * Convience for {@link #addln(String, Object[])}.
134         */
135    
136        public void addln(String pattern, Object arg0, Object arg1)
137        {
138            addln(pattern, new Object[]
139            { arg0, arg1 });
140        }
141    
142        /**
143         * Convience for {@link #addln(String, Object[])}.
144         */
145    
146        public void addln(String pattern, Object arg0, Object arg1, Object arg2)
147        {
148            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        public void addQuoted(String 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 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 toString()
228        {
229            return _buffer.toString();
230        }
231    }