001package com.hfg.util;
002
003
004//------------------------------------------------------------------------------
005/**
006 Baeza-Yates, Perleberg string matching pattern object.
007 <div>
008 @author J. Alex Taylor, hairyfatguy.com
009 </div>
010 */
011//------------------------------------------------------------------------------
012// com.hfg XML/HTML Coding Library
013//
014// This library is free software; you can redistribute it and/or
015// modify it under the terms of the GNU Lesser General Public
016// License as published by the Free Software Foundation; either
017// version 2.1 of the License, or (at your option) any later version.
018//
019// This library is distributed in the hope that it will be useful,
020// but WITHOUT ANY WARRANTY; without even the implied warranty of
021// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
022// Lesser General Public License for more details.
023//
024// You should have received a copy of the GNU Lesser General Public
025// License along with this library; if not, write to the Free Software
026// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
027//
028// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
029// jataylor@hairyfatguy.com
030//------------------------------------------------------------------------------
031
032public class BYPStringPattern
033{
034   private String  mPattern;
035   private int     mMaxMismatches = 0;
036   private boolean mCaseInsensitive;
037
038
039   //###########################################################################
040   // CONSTRUCTORS
041   //###########################################################################
042
043   //---------------------------------------------------------------------------
044   /**
045    Creates a new Baeza-Yates, Perleberg string matching pattern.
046    Use when mismatches are allowed but there is no ambiguity within positions or
047    range specifications.
048    * @param inValue the search (query) string
049    */
050   public BYPStringPattern(String inValue)
051   {
052      mPattern = inValue;
053
054      if (null == mPattern
055            || 0 == mPattern.length())
056      {
057         throw new RuntimeException("No search pattern specified!");
058      }
059      else if (mPattern.length() > 128)
060      {
061         throw new RuntimeException("The pattern cannot be longer that 128 characters!");
062      }
063   }
064
065   //###########################################################################
066   // PUBLIC METHODS
067   //###########################################################################
068
069   //---------------------------------------------------------------------------
070   public boolean isCaseInsensitive()
071   {
072      return mCaseInsensitive;
073   }
074
075   //---------------------------------------------------------------------------
076   public BYPStringPattern setCaseInsensitive(boolean inValue)
077   {
078      mCaseInsensitive = inValue;
079      return this;
080   }
081
082   //---------------------------------------------------------------------------
083   public BYPStringPattern setMaxMismatches(int inValue)
084   {
085      if (inValue >= mPattern.length())
086      {
087         throw new RuntimeException("The number of allowed mismatches cannot be greater or equal to the pattern length!");
088      }
089
090      mMaxMismatches = inValue;
091      return this;
092   }
093
094   //---------------------------------------------------------------------------
095   public int getMaxMismatches()
096   {
097      return mMaxMismatches;
098   }
099
100   //---------------------------------------------------------------------------
101   public String getPatternString()
102   {
103      return mPattern;
104   }
105
106   //---------------------------------------------------------------------------
107   public int length()
108   {
109      return mPattern.length();
110   }
111
112   //---------------------------------------------------------------------------
113   public BYPStringMatcher matcher(String inTargetString)
114   {
115      return new BYPStringMatcher(this, inTargetString);
116   }
117}