All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.parser.XMLStringBuffer Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.parser;


//------------------------------------------------------------------------------
/**
 * A custom StringBuffer to get better performance when parsing XML.
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class XMLStringBuffer
{
   private char[] mArray;
   private int    mLength;

   private static int sDefaultSize = 32;

   //--------------------------------------------------------------------------
   public XMLStringBuffer()
   {
      mArray = new char[sDefaultSize];
   }

   //--------------------------------------------------------------------------
   public XMLStringBuffer(int inSize)
   {
      mArray = new char[inSize];
   }

   //--------------------------------------------------------------------------
   public XMLStringBuffer(char c)
   {
      this();
      append(c);
   }

   //--------------------------------------------------------------------------
   public XMLStringBuffer(String inString)
   {
      this(inString.length() + sDefaultSize);
      append(inString);
   }


   //--------------------------------------------------------------------------
   public void append(char c)
   {
      if (mLength + 1 > mArray.length)
      {
//         char[] newArray = new char[mLength + 1 + sDefaultSize];
         char[] newArray = new char[mLength * 2];
         System.arraycopy(mArray, 0, newArray, 0, mLength);
         mArray = newArray;
      }

      mArray[mLength] = c;
      mLength++;
   }

   //--------------------------------------------------------------------------
   public void append(char[] inBuffer, int inOffset, int inLength)
   {
      if (mLength + inLength > mArray.length)
      {
         char[] newArray = new char[mLength + inLength + sDefaultSize];
         System.arraycopy(mArray, 0, newArray, 0, mLength);
         mArray = newArray;
      }

      System.arraycopy(inBuffer, inOffset, mArray, mLength, inLength);
      mLength += inLength;
   }

   //--------------------------------------------------------------------------
   public void append(String inString)
   {
      if (mLength + inString.length() > mArray.length)
      {
         char[] newArray = new char[mLength + inString.length() + sDefaultSize];
         System.arraycopy(mArray, 0, newArray, 0, mLength);
         mArray = newArray;
      }

      inString.getChars(0, inString.length(), mArray, mLength);
      mLength += inString.length();
   }

   //--------------------------------------------------------------------------
   public void prepend(String inString)
   {
      if (mLength + inString.length() > mArray.length)
      {
         char[] newArray = new char[mLength + inString.length() + sDefaultSize];
         System.arraycopy(mArray, 0, newArray, inString.length(), mLength);
         mArray = newArray;
      }
      else
      {
         System.arraycopy(mArray, 0, mArray, inString.length(), mLength);
      }

      inString.getChars(0, inString.length(), mArray, 0);
      mLength += inString.length();
   }

   //--------------------------------------------------------------------------
   public void clear()
   {
      mLength = 0;
      mArray = new char[sDefaultSize];
   }

   //--------------------------------------------------------------------------
   public char[] getCharArray()
   {
      return mArray;
   }

   //--------------------------------------------------------------------------
   public void setLength(int inLength)
   {
      if (inLength > 0 && inLength < mLength) mLength = inLength;
   }

   //--------------------------------------------------------------------------
   public int length()
   {
      return mLength;
   }

   //--------------------------------------------------------------------------
   public String toString()
   {
      return new String(mArray, 0, mLength);
   }

   //--------------------------------------------------------------------------
   public boolean hasNonwhitespaceContent()
   {
      for (int i = 0; i < mLength; i++)
      {
         if (mArray[i] != ' '
             && mArray[i] != '\t'
             && mArray[i] != '\n'
             && mArray[i] != '\r')
         {
            return true;
         }
      }

      return false;
   }

   //--------------------------------------------------------------------------
   public static void setDefaultSize(int inValue)
   {
      sDefaultSize = inValue;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy