Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.util » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   // ===========================================================================
   27   // CONTENT  : CLASS StringExaminer
   28   // AUTHOR   : Manfred Duchrow
   29   // VERSION  : 1.0 - 29/09/2002
   30   // HISTORY  :
   31   //  29/09/2002  duma  CREATED
   32   //
   33   // Copyright (c) 2002, by Manfred Duchrow. All rights reserved.
   34   // ===========================================================================
   35   package com.sshtools.daemon.util;
   36   
   37   
   38   // ===========================================================================
   39   // IMPORTS
   40   // ===========================================================================
   41   
   42   /**
   43    * As a subclass of StringScanner this class allows more advanced navigation
   44    * over the underlying string.    <br>
   45    * That includes moving to positions of specific substrings etc.
   46    *
   47    * @author Manfred Duchrow
   48    * @version $Id: StringExaminer.java,v 1.7 2003/09/11 15:37:07 martianx Exp $
   49    */
   50   public class StringExaminer extends StringScanner {
   51       // =========================================================================
   52       // CONSTANTS
   53       // =========================================================================
   54       // =========================================================================
   55       // INSTANCE VARIABLES
   56       // =========================================================================
   57       private boolean ignoreCase = false;
   58   
   59       // =========================================================================
   60       // CLASS METHODS
   61       // =========================================================================
   62       // =========================================================================
   63       // CONSTRUCTORS
   64       // =========================================================================
   65   
   66       /**
   67    * Initialize the new instance with the string to examine.   <br>
   68    * The string will be treated case-sensitive.
   69    *
   70    * @param stringToExamine The string that should be examined
   71    */
   72       public StringExaminer(String stringToExamine) {
   73           this(stringToExamine, false);
   74       }
   75   
   76       // StringExaminer()
   77       // -------------------------------------------------------------------------
   78   
   79       /**
   80    * Initialize the new instance with the string to examine.
   81    *
   82    * @param stringToExamine The string that should be examined
   83    * @param ignoreCase Specified whether or not treating the string case
   84    *        insensitive
   85    */
   86       public StringExaminer(String stringToExamine, boolean ignoreCase) {
   87           super(stringToExamine);
   88           this.ignoreCase(ignoreCase);
   89       }
   90   
   91       // StringExaminer()
   92   
   93       /**
   94    *
   95    *
   96    * @return
   97    */
   98       protected boolean ignoreCase() {
   99           return ignoreCase;
  100       }
  101   
  102       /**
  103    *
  104    *
  105    * @param newValue
  106    */
  107       protected void ignoreCase(boolean newValue) {
  108           ignoreCase = newValue;
  109       }
  110   
  111       // -------------------------------------------------------------------------
  112       // =========================================================================
  113       // PUBLIC INSTANCE METHODS
  114       // =========================================================================
  115   
  116       /**
  117    * Increments the position pointer up to the last character that matched
  118    * the character sequence in the given matchString. Returns true, if the
  119    * matchString was found, otherwise false.
  120    *
  121    * <p>
  122    * If the matchString was found, the next invocation of method nextChar()
  123    * returns the first character after that matchString.
  124    * </p>
  125    *
  126    * @param matchString The string to look up
  127    *
  128    * @return
  129    */
  130       public boolean skipAfter(String matchString) {
  131           char ch = '-';
  132           char matchChar = ' ';
  133           boolean found = false;
  134           int index = 0;
  135   
  136           if ((matchString == null) || (matchString.length() == 0)) {
  137               return false;
  138           }
  139   
  140           ch = this.nextChar();
  141   
  142           while ((endNotReached(ch)) && (!found)) {
  143               matchChar = matchString.charAt(index);
  144   
  145               if (this.charsAreEqual(ch, matchChar)) {
  146                   index++;
  147   
  148                   if (index >= matchString.length()) { // whole matchString checked ?
  149                       found = true;
  150                   } else {
  151                       ch = this.nextChar();
  152                   }
  153               } else {
  154                   if (index == 0) {
  155                       ch = this.nextChar();
  156                   } else {
  157                       index = 0;
  158                   }
  159               }
  160           }
  161   
  162           return found;
  163       }
  164   
  165       // skipAfter()
  166       // -------------------------------------------------------------------------
  167   
  168       /**
  169    * Increments the position pointer up to the first character before the
  170    * character sequence in the given matchString. Returns true, if the
  171    * matchString was found, otherwise false.
  172    *
  173    * <p>
  174    * If the matchString was found, the next invocation of method nextChar()
  175    * returns the first character of that matchString from the position where
  176    * it was found inside the examined string.
  177    * </p>
  178    *
  179    * @param matchString The string to look up
  180    *
  181    * @return
  182    */
  183       public boolean skipBefore(String matchString) {
  184           boolean found;
  185           found = this.skipAfter(matchString);
  186   
  187           if (found) {
  188               this.skip(0 - matchString.length());
  189           }
  190   
  191           return found;
  192       }
  193   
  194       // skipBefore()
  195       // -------------------------------------------------------------------------
  196   
  197       /**
  198    * Returns the a string containing all characters from the current position
  199    * up to the end of the examined string.   <br>
  200    * The character position of the examiner is not changed by this method.
  201    *
  202    * @return
  203    */
  204       public String peekUpToEnd() {
  205           return this.upToEnd(true);
  206       }
  207   
  208       // peekUpToEnd()
  209       // -------------------------------------------------------------------------
  210   
  211       /**
  212    * Returns the a string containing all characters from the current position
  213    * up to the end of the examined string.   <br>
  214    * The character position is put to the end by this method. That means the
  215    * next invocation of nextChar() returns END_REACHED.
  216    *
  217    * @return
  218    */
  219       public String upToEnd() {
  220           return this.upToEnd(false);
  221       }
  222   
  223       // upToEnd()
  224   
  225       /**
  226    *
  227    *
  228    * @param char1
  229    * @param char2
  230    *
  231    * @return
  232    */
  233       protected boolean charsAreEqual(char char1, char char2) {
  234           return (this.ignoreCase())
  235           ? (Character.toUpperCase(char1) == Character.toUpperCase(char2))
  236           : (char1 == char2);
  237       }
  238   
  239       // charsAreEqual()
  240       // -------------------------------------------------------------------------
  241   
  242       /**
  243    * Returns the a string containing all characters from the current position
  244    * up to the end of the examined string.   <br>
  245    * Depending on the peek flag the character position of the examiner  is
  246    * unchanged (true) after calling this method or points behind the strings
  247    * last character.
  248    *
  249    * @param peek
  250    *
  251    * @return
  252    */
  253       protected String upToEnd(boolean peek) {
  254           char result = '-';
  255           int lastPosition = 0;
  256           StringBuffer buffer = new StringBuffer(100);
  257           lastPosition = this.getPosition();
  258           result = this.nextChar();
  259   
  260           while (endNotReached(result)) {
  261               buffer.append(result);
  262               result = this.nextChar();
  263           }
  264   
  265           if (peek) {
  266               this.setPosition(lastPosition);
  267           }
  268   
  269           return buffer.toString();
  270       }
  271   
  272       // upToEnd()
  273       // -------------------------------------------------------------------------
  274   }
  275   
  276   
  277   // class StringExaminer

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.daemon.util » [javadoc | source]