Coverage Report - org.simpleframework.xml.core.Template
 
Classes in this File Line Coverage Branch Coverage Complexity
Template
66%
24/36
100%
2/2
1.083
 
 1  
 /*
 2  
  * Template.java February 2001
 3  
  *
 4  
  * Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
  
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 /** 
 22  
  * This is primarily used to replace the <code>StringBuffer</code> 
 23  
  * class, as a way for the <code>TemplateEngine</code> to store the 
 24  
  * data for a specific region within the parse data that constitutes 
 25  
  * a desired value. The methods are not synchronized so it enables 
 26  
  * the characters to be taken quicker than the string buffer class.
 27  
  *
 28  
  * @author Niall Gallagher
 29  
  */
 30  
 class Template {      
 31  
 
 32  
    /** 
 33  
     * This is used to quicken <code>toString</code>.
 34  
     */
 35  
    protected String cache;
 36  
 
 37  
    /** 
 38  
     * The characters that this buffer has accumulated.
 39  
     */
 40  
    protected char[] buf;
 41  
 
 42  
    /** 
 43  
     * This is the number of characters this has stored.
 44  
     */
 45  
    protected int count;
 46  
    
 47  
    /** 
 48  
     * Constructor for <code>Template</code>. The default 
 49  
     * <code>Template</code> stores 16 characters before a
 50  
     * <code>resize</code> is needed to append extra characters. 
 51  
     */
 52  
    public Template(){
 53  295419
       this(16);
 54  295419
    }
 55  
    
 56  
    /** 
 57  
     * This creates a <code>Template</code> with a specific 
 58  
     * default size. The buffer will be created the with the 
 59  
     * length specified. The <code>Template</code> can grow 
 60  
     * to accomodate a collection of characters larger the the 
 61  
     * size spacified.
 62  
     *
 63  
     * @param size initial size of this <code>Template</code>
 64  
     */
 65  295419
    public Template(int size){
 66  295419
       this.buf = new char[size];
 67  295419
    }
 68  
    
 69  
    /** 
 70  
     * This will add a <code>char</code> to the end of the buffer.
 71  
     * The buffer will not overflow with repeated uses of the 
 72  
     * <code>append</code>, it uses an <code>ensureCapacity</code>
 73  
     * method which will allow the buffer to dynamically grow in 
 74  
     * size to accomodate more characters.
 75  
     *
 76  
     * @param c the <code>char</code> to be appended
 77  
     */
 78  
    public void append(char c){
 79  7845759
       ensureCapacity(count+ 1);
 80  7845759
       buf[count++] = c;
 81  7845759
    }
 82  
 
 83  
    /** 
 84  
     * This will add a <code>String</code> to the end of the buffer.
 85  
     * The buffer will not overflow with repeated uses of the 
 86  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 87  
     * method which will allow the buffer to dynamically grow in 
 88  
     * size to accomodate large <code>String</code> objects.
 89  
     *
 90  
     * @param str the <code>String</code> to be appended to this
 91  
     */  
 92  
    public void append(String str){
 93  560214
       ensureCapacity(count+ str.length());
 94  560214
       str.getChars(0,str.length(),buf,count);
 95  560214
       count += str.length();
 96  560214
    }
 97  
 
 98  
    /** 
 99  
     * This will add a <code>Template</code> to the end of this.
 100  
     * The buffer will not overflow with repeated uses of the 
 101  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 102  
     * method which will allow the buffer to dynamically grow in 
 103  
     * size to accomodate large <code>Template</code> objects.
 104  
     *
 105  
     * @param text the <code>Template</code> to be appended 
 106  
     */  
 107  
    public void append(Template text){
 108  0
       append(text.buf, 0, text.count);           
 109  0
    }
 110  
    
 111  
    /** 
 112  
     * This will add a <code>char</code> to the end of the buffer.
 113  
     * The buffer will not overflow with repeated uses of the 
 114  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 115  
     * method which will allow the buffer to dynamically grow in 
 116  
     * size to accomodate large <code>char</code> arrays.
 117  
     *
 118  
     * @param c the <code>char</code> array to be appended to this
 119  
     * @param off the read offset for the array    
 120  
     * @param len the number of characters to append to this
 121  
     */   
 122  
    public void append(char[] c, int off, int len){
 123  0
       ensureCapacity(count+ len);
 124  0
       System.arraycopy(c,off,buf,count,len);
 125  0
       count+=len;
 126  0
    }
 127  
    
 128  
    /** 
 129  
     * This will add a <code>String</code> to the end of the buffer.
 130  
     * The buffer will not overflow with repeated uses of the 
 131  
     * <code>append</code>, it uses an <code>ensureCapacity</code>
 132  
     * method which will allow the buffer to dynamically grow in 
 133  
     * size to accomodate large <code>String</code> objects.
 134  
     *
 135  
     * @param str the <code>String</code> to be appended to this
 136  
     * @param off the read offset for the <code>String</code>
 137  
     * @param len the number of characters to append to this
 138  
     */   
 139  
    public void append(String str, int off, int len){
 140  0
       ensureCapacity(count+ len);
 141  0
       str.getChars(off,len,buf,count);  
 142  0
       count += len;
 143  0
    }
 144  
 
 145  
 
 146  
    /** 
 147  
     * This will add a <code>Template</code> to the end of this.
 148  
     * The buffer will not overflow with repeated uses of the 
 149  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 150  
     * method which will allow the buffer to dynamically grow in 
 151  
     * size to accomodate large <code>Template</code> objects.
 152  
     *
 153  
     * @param text the <code>Template</code> to be appended 
 154  
     * @param off the read offset for the <code>Template</code>
 155  
     * @param len the number of characters to append to this
 156  
     */  
 157  
    public void append(Template text, int off, int len){
 158  0
       append(text.buf, off, len);           
 159  0
    }   
 160  
    
 161  
    /** 
 162  
     * This ensure that there is enough space in the buffer to allow
 163  
     * for more characters to be added. If the buffer is already 
 164  
     * larger than min then the buffer will not be expanded at all.
 165  
     *
 166  
     * @param min the minimum size needed for this buffer
 167  
     */     
 168  
    protected void ensureCapacity(int min) {
 169  8405973
       if(buf.length < min) {
 170  200111
          int size = buf.length * 2;
 171  200111
          int max = Math.max(min, size);
 172  200111
          char[] temp = new char[max];         
 173  200111
          System.arraycopy(buf, 0, temp, 0, count); 
 174  200111
          buf = temp;
 175  
       }
 176  8405973
    }  
 177  
    
 178  
    /** 
 179  
     * This will empty the <code>Template</code> so that the
 180  
     * <code>toString</code> paramater will return <code>null</code>. 
 181  
     * This is used so that the same <code>Template</code> can be 
 182  
     * recycled for different tokens.
 183  
     */
 184  
    public void clear(){
 185  680330
       cache = null;
 186  680330
       count = 0;
 187  680330
    }
 188  
   
 189  
    /** 
 190  
     * This will return the number of bytes that have been appended 
 191  
     * to the <code>Template</code>. This will return zero after 
 192  
     * the clear method has been invoked.
 193  
     *
 194  
     * @return the number of characters within this buffer object
 195  
     */
 196  
    public int length(){
 197  400162
       return count;
 198  
    }
 199  
 
 200  
    /** 
 201  
     * This will return the characters that have been appended to the 
 202  
     * <code>Template</code> as a <code>String</code> object.
 203  
     * If the <code>String</code> object has been created before then
 204  
     * a cached <code>String</code> object will be returned. This
 205  
     * method will return <code>null</code> after clear is invoked.
 206  
     *
 207  
     * @return the characters appended as a <code>String</code>
 208  
     */
 209  
    public String toString(){
 210  360164
       return new String(buf,0,count);
 211  
    }
 212  
 }