001package com.hfg.xml.parser; 002 003 004//------------------------------------------------------------------------------ 005/** 006 * A custom StringBuffer to get better performance when parsing XML. 007 * @author J. Alex Taylor, hairyfatguy.com 008 */ 009//------------------------------------------------------------------------------ 010// com.hfg XML/HTML Coding Library 011// 012// This library is free software; you can redistribute it and/or 013// modify it under the terms of the GNU Lesser General Public 014// License as published by the Free Software Foundation; either 015// version 2.1 of the License, or (at your option) any later version. 016// 017// This library is distributed in the hope that it will be useful, 018// but WITHOUT ANY WARRANTY; without even the implied warranty of 019// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 020// Lesser General Public License for more details. 021// 022// You should have received a copy of the GNU Lesser General Public 023// License along with this library; if not, write to the Free Software 024// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 025// 026// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 027// jataylor@hairyfatguy.com 028//------------------------------------------------------------------------------ 029 030public class XMLStringBuffer 031{ 032 private char[] mArray; 033 private int mLength; 034 035 private static int sDefaultSize = 32; 036 037 //-------------------------------------------------------------------------- 038 public XMLStringBuffer() 039 { 040 mArray = new char[sDefaultSize]; 041 } 042 043 //-------------------------------------------------------------------------- 044 public XMLStringBuffer(int inSize) 045 { 046 mArray = new char[inSize]; 047 } 048 049 //-------------------------------------------------------------------------- 050 public XMLStringBuffer(char c) 051 { 052 this(); 053 append(c); 054 } 055 056 //-------------------------------------------------------------------------- 057 public XMLStringBuffer(String inString) 058 { 059 this(inString.length() + sDefaultSize); 060 append(inString); 061 } 062 063 064 //-------------------------------------------------------------------------- 065 public void append(char c) 066 { 067 if (mLength + 1 > mArray.length) 068 { 069// char[] newArray = new char[mLength + 1 + sDefaultSize]; 070 char[] newArray = new char[mLength * 2]; 071 System.arraycopy(mArray, 0, newArray, 0, mLength); 072 mArray = newArray; 073 } 074 075 mArray[mLength] = c; 076 mLength++; 077 } 078 079 //-------------------------------------------------------------------------- 080 public void append(char[] inBuffer, int inOffset, int inLength) 081 { 082 if (mLength + inLength > mArray.length) 083 { 084 char[] newArray = new char[mLength + inLength + sDefaultSize]; 085 System.arraycopy(mArray, 0, newArray, 0, mLength); 086 mArray = newArray; 087 } 088 089 System.arraycopy(inBuffer, inOffset, mArray, mLength, inLength); 090 mLength += inLength; 091 } 092 093 //-------------------------------------------------------------------------- 094 public void append(String inString) 095 { 096 if (mLength + inString.length() > mArray.length) 097 { 098 char[] newArray = new char[mLength + inString.length() + sDefaultSize]; 099 System.arraycopy(mArray, 0, newArray, 0, mLength); 100 mArray = newArray; 101 } 102 103 inString.getChars(0, inString.length(), mArray, mLength); 104 mLength += inString.length(); 105 } 106 107 //-------------------------------------------------------------------------- 108 public void prepend(String inString) 109 { 110 if (mLength + inString.length() > mArray.length) 111 { 112 char[] newArray = new char[mLength + inString.length() + sDefaultSize]; 113 System.arraycopy(mArray, 0, newArray, inString.length(), mLength); 114 mArray = newArray; 115 } 116 else 117 { 118 System.arraycopy(mArray, 0, mArray, inString.length(), mLength); 119 } 120 121 inString.getChars(0, inString.length(), mArray, 0); 122 mLength += inString.length(); 123 } 124 125 //-------------------------------------------------------------------------- 126 public void clear() 127 { 128 mLength = 0; 129 mArray = new char[sDefaultSize]; 130 } 131 132 //-------------------------------------------------------------------------- 133 public char[] getCharArray() 134 { 135 return mArray; 136 } 137 138 //-------------------------------------------------------------------------- 139 public void setLength(int inLength) 140 { 141 if (inLength > 0 && inLength < mLength) mLength = inLength; 142 } 143 144 //-------------------------------------------------------------------------- 145 public int length() 146 { 147 return mLength; 148 } 149 150 //-------------------------------------------------------------------------- 151 public String toString() 152 { 153 return new String(mArray, 0, mLength); 154 } 155 156 //-------------------------------------------------------------------------- 157 public boolean hasNonwhitespaceContent() 158 { 159 for (int i = 0; i < mLength; i++) 160 { 161 if (mArray[i] != ' ' 162 && mArray[i] != '\t' 163 && mArray[i] != '\n' 164 && mArray[i] != '\r') 165 { 166 return true; 167 } 168 } 169 170 return false; 171 } 172 173 //-------------------------------------------------------------------------- 174 public static void setDefaultSize(int inValue) 175 { 176 sDefaultSize = inValue; 177 } 178}