Coverage Report - com.ccg.macros.OutputStringBuffer
 
Classes in this File Line Coverage Branch Coverage Complexity
OutputStringBuffer
37% 
40% 
1.25
 
 1  
 /*----------------------------------------------------------------
 2  
  * $Id: OutputStringBuffer.java,v 1.1.1.1 2004/01/26 20:46:18 pkb Exp $
 3  
  * 
 4  
  * (c)2000 - Paul Blankenbaker
 5  
  *
 6  
  * Revision Log
 7  
  * 
 8  
  *  2000-11-16 11:19:26        Paul Blankenbaker 
 9  
  * 
 10  
  *         Created initial version
 11  
  */
 12  
 //----------------------------------------------------------------
 13  
 
 14  
 package com.ccg.macros;
 15  
 
 16  
 import java.io.*;                // needed for I/O operations
 17  
 
 18  
 //----------------------------------------------------------------
 19  
 /** A {@link Output Output} implementation which uses a string buffer.
 20  
  *
 21  
  * <p>This implementation is fairly efficient and useful if you want
 22  
  * to be able to get a String after interpreting macros.</p>
 23  
  * 
 24  
  * @version $Revision: 1.1.1.1 $
 25  
  * 
 26  
  * @since 1.0
 27  
  * 
 28  
  * @author $Author: pkb $
 29  
  * 
 30  
  * @see Output */
 31  
 //----------------------------------------------------------------
 32  
 
 33  
 public class OutputStringBuffer implements Output {
 34  
 
 35  
   //----------------------------------------------------------------
 36  
   /** Construct a output string buffer with the default initial capacity.
 37  
    * 
 38  
    * @since        1.0 */
 39  
   //----------------------------------------------------------------
 40  
 
 41  0
   public OutputStringBuffer() {
 42  0
     setBuffer(128);
 43  0
   }
 44  
 
 45  
 
 46  
 
 47  
   //----------------------------------------------------------------
 48  
   /** Construct a output string buffer with a initial capacity.
 49  
    * 
 50  
    * @param        cap
 51  
    * 
 52  
    *         Initial capacity to allocate.
 53  
    * 
 54  
    * @since        1.0 */
 55  
   //----------------------------------------------------------------
 56  
 
 57  726
   public OutputStringBuffer(int size) {
 58  726
     setBuffer(size);
 59  726
   }
 60  
 
 61  
 
 62  
   //----------------------------------------------------------------
 63  
   /** Construct a output buffer with a existing buffer.
 64  
    * 
 65  
    * This method allows one to contstruct a output buffer with an
 66  
    * existing {@link StringBuffer} object. This will initialize the
 67  
    * resulting output with the contents of the object passed. All data
 68  
    * which is {@link #write written} to the output buffer will be
 69  
    * appended.
 70  
    *
 71  
    * <p>It should be noted, that in this case a new {@link
 72  
    * StringBuffer} object is not allocated - the object will directly
 73  
    * append output to the {@link StringBuffer object you supply}.
 74  
    * 
 75  
    * @param        init
 76  
    * 
 77  
    *         Initial contents to load the output buffer with (any new
 78  
    *         output will be appeneded). You can pass null (buffer won't
 79  
    *         have any initial contents)
 80  
    * 
 81  
    * @since        1.0
 82  
    * 
 83  
    * @see #setBuffer(StringBuffer) */
 84  
   //----------------------------------------------------------------
 85  
 
 86  0
   public OutputStringBuffer(StringBuffer sb) {
 87  0
     setBuffer(sb);
 88  0
   }
 89  
 
 90  
 
 91  
   //----------------------------------------------------------------
 92  
   /** Construct a output buffer with some initial content.
 93  
    * 
 94  
    * This method allows one to contstruct a output buffer with an
 95  
    * existing {@link String} object. This will initialize the
 96  
    * resulting output with the contents of the object passed. All data
 97  
    * which is {@link #write written} to the output buffer will be
 98  
    * appended.
 99  
    * 
 100  
    * @param        init
 101  
    * 
 102  
    *         Initial contents to load the output buffer with (any new
 103  
    *         output will be appended). You can pass null (buffer won't
 104  
    *         have any initial contents)
 105  
    * 
 106  
    * @since        1.0
 107  
    * 
 108  
    * @see #setBuffer(String) */
 109  
   //----------------------------------------------------------------
 110  
 
 111  0
   public OutputStringBuffer(String init) {
 112  0
     setBuffer(init);
 113  0
   }
 114  
 
 115  
 
 116  
   //----------------------------------------------------------------
 117  
   /** One line summary...
 118  
    * 
 119  
    * Describe...
 120  
    * 
 121  
    * @param        arg1
 122  
    * 
 123  
    *         arg1-describe
 124  
    * 
 125  
    * @param        arg2
 126  
    * 
 127  
    *         arg2-describe
 128  
    * 
 129  
    * @throws        exc
 130  
    * 
 131  
    *         Why thrown
 132  
    * 
 133  
    * @return
 134  
    * 
 135  
    *         ret-description
 136  
    * 
 137  
    * @since        1.0 */
 138  
   //----------------------------------------------------------------
 139  
 
 140  
   public void setBuffer(StringBuffer sb) {
 141  0
     if (sb != null) _buf = sb;
 142  0
     else setBuffer(128);
 143  0
   }
 144  
 
 145  
 
 146  
   //----------------------------------------------------------------
 147  
   /** One line summary...
 148  
    * 
 149  
    * Describe...
 150  
    * 
 151  
    * @param        arg1
 152  
    * 
 153  
    *         arg1-describe
 154  
    * 
 155  
    * @param        arg2
 156  
    * 
 157  
    *         arg2-describe
 158  
    * 
 159  
    * @throws        exc
 160  
    * 
 161  
    *         Why thrown
 162  
    * 
 163  
    * @return
 164  
    * 
 165  
    *         ret-description
 166  
    * 
 167  
    * @since        1.0 */
 168  
   //----------------------------------------------------------------
 169  
 
 170  
   public void setBuffer(int size) {
 171  726
     _buf = ((size <= 0) ? new StringBuffer() : new StringBuffer(size));
 172  726
   }
 173  
 
 174  
 
 175  
   //----------------------------------------------------------------
 176  
   /** One line summary...
 177  
    * 
 178  
    * Describe...
 179  
    * 
 180  
    * @param        arg1
 181  
    * 
 182  
    *         arg1-describe
 183  
    * 
 184  
    * @param        arg2
 185  
    * 
 186  
    *         arg2-describe
 187  
    * 
 188  
    * @throws        exc
 189  
    * 
 190  
    *         Why thrown
 191  
    * 
 192  
    * @return
 193  
    * 
 194  
    *         ret-description
 195  
    * 
 196  
    * @since        1.0 */
 197  
   //----------------------------------------------------------------
 198  
 
 199  
   public void setBuffer(String with) {
 200  0
     _buf = (with == null) ? new StringBuffer(with) : new StringBuffer(128);
 201  0
   }
 202  
 
 203  
 
 204  
 
 205  
   //----------------------------------------------------------------
 206  
   /** One line summary...
 207  
    * 
 208  
    * Describe...
 209  
    * 
 210  
    * @param        arg1
 211  
    * 
 212  
    *         arg1-describe
 213  
    * 
 214  
    * @param        arg2
 215  
    * 
 216  
    *         arg2-describe
 217  
    * 
 218  
    * @throws        exc
 219  
    * 
 220  
    *         Why thrown
 221  
    * 
 222  
    * @return
 223  
    * 
 224  
    *         ret-description
 225  
    * 
 226  
    * @since        1.0 */
 227  
   //----------------------------------------------------------------
 228  
 
 229  
   public StringBuffer getBuffer() {
 230  0
     return _buf;
 231  
   }
 232  
 
 233  
 
 234  
   //----------------------------------------------------------------
 235  
   /** One line summary...
 236  
    * 
 237  
    * Describe...
 238  
    * 
 239  
    * @param        arg1
 240  
    * 
 241  
    *         arg1-describe
 242  
    * 
 243  
    * @param        arg2
 244  
    * 
 245  
    *         arg2-describe
 246  
    * 
 247  
    * @throws        exc
 248  
    * 
 249  
    *         Why thrown
 250  
    * 
 251  
    * @return
 252  
    * 
 253  
    *         ret-description
 254  
    * 
 255  
    * @since        1.0 */
 256  
   //----------------------------------------------------------------
 257  
 
 258  
   public void clear() {
 259  0
     if (_buf != null) _buf.setLength(0);
 260  0
   }
 261  
 
 262  
 
 263  
   //----------------------------------------------------------------
 264  
   /** One line summary...
 265  
    * 
 266  
    * Describe...
 267  
    * 
 268  
    * @param        arg1
 269  
    * 
 270  
    *         arg1-describe
 271  
    * 
 272  
    * @param        arg2
 273  
    * 
 274  
    *         arg2-describe
 275  
    * 
 276  
    * @throws        exc
 277  
    * 
 278  
    *         Why thrown
 279  
    * 
 280  
    * @return
 281  
    * 
 282  
    *         ret-description
 283  
    * 
 284  
    * @since        1.0 */
 285  
   //----------------------------------------------------------------
 286  
 
 287  
   public String toString() {
 288  726
     return _buf.toString();
 289  
   }
 290  
 
 291  
 
 292  
   //----------------------------------------------------------------
 293  
   /** Writes a set of characters from a buffer to the output.
 294  
    * 
 295  
    * This method is used by macro processors (such as the {@link
 296  
    * AtMacros AtMacros} class) to send a set of characters to the
 297  
    * output device (string, file, etc).
 298  
    * 
 299  
    * @param        buf
 300  
    * 
 301  
    *         A character array to take the data from (must not be null)
 302  
    * 
 303  
    * @param        ofs
 304  
    * 
 305  
    *         Offset into the buffer to the first character to start the
 306  
    *         copy from. Should be in the range of [0,buf.length-1].
 307  
    * 
 308  
    * @param        len
 309  
    * 
 310  
    *         How many characters to copy. When added to the offset, the
 311  
    *         resulting value should not exceed the length of the array.
 312  
    * 
 313  
    * @throws        IOException
 314  
    * 
 315  
    *         If a I/O error is encountered (which this implementation of a
 316  
    *         {@link Output Output} device will probably never throw).
 317  
    * 
 318  
    * @since        1.0 */
 319  
   //----------------------------------------------------------------
 320  
 
 321  
   public void write(char[] buf, int ofs, int len) throws IOException {
 322  432
     _buf.append(buf,ofs,len);
 323  432
   }
 324  
 
 325  
 
 326  
   //----------------------------------------------------------------
 327  
   /** Appends the {@link java.lang.String string} representation of a {@link java.lang.Object Object} to the output.
 328  
    * 
 329  
    * This method is used by macro processors (such as the {@link
 330  
    * AtMacros AtMacros} class) to cause the {@link java.lang.String
 331  
    * string} representation of a {@link java.lang.Object Object} to be
 332  
    * added to the output produced.
 333  
    * 
 334  
    * @param        object
 335  
    * 
 336  
    *         The object to write out (the {@link java.lang.Object#toString
 337  
    *         toString()} method is used to write out the string
 338  
    *         representation of the object). You may pass null (in which
 339  
    *         case nothing will be done).
 340  
    * 
 341  
    * @throws        IOException
 342  
    * 
 343  
    *         If a I/O error is encountered (which this implementation of a
 344  
    *         {@link Output Output} device will probably never throw).
 345  
    * 
 346  
    * @since        1.0 */
 347  
   //----------------------------------------------------------------
 348  
 
 349  
   public void write(Object o) throws IOException {
 350  670
     if (o != null) _buf.append(o);
 351  670
   }
 352  
 
 353  
   //----------------------------------------------------------------  
 354  
   // private data
 355  
   //----------------------------------------------------------------
 356  
 
 357  
                                 // buffer to append info to on each write
 358  
   private transient StringBuffer _buf;
 359  
 
 360  
 }