Package org.jostraca.util

Source Code of org.jostraca.util.OroRegExp

/*
* Name:    OroRegExp
* Author:  Richard Rodger
*
* Copyright (c) 2003 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.util;


// import
import org.jostraca.comp.apache.oro.text.regex.Util;
import org.jostraca.comp.apache.oro.text.regex.Pattern;
import org.jostraca.comp.apache.oro.text.regex.PatternMatcher;
import org.jostraca.comp.apache.oro.text.regex.PatternCompiler;
import org.jostraca.comp.apache.oro.text.regex.Substitution;
import org.jostraca.comp.apache.oro.text.regex.PatternMatcherInput;
import org.jostraca.comp.apache.oro.text.regex.MatchResult;
import org.jostraca.comp.apache.oro.text.regex.Perl5Compiler;
import org.jostraca.comp.apache.oro.text.regex.Perl5Matcher;
import org.jostraca.comp.apache.oro.text.regex.Perl5Substitution;
import org.jostraca.comp.apache.oro.text.regex.MalformedPatternException;

import java.util.ArrayList;


/** Adapter for Jakarta ORO regular expressions.  */
public class OroRegExp extends RegExp {

  // public static
  public static final String CN = PackageLocal.PN_DOT+"OroRegExp";


  // private instance
  private Pattern         iPattern       = null;
  private String          iReplace       = Standard.EMPTY;
  private Substitution    iSubstitution  = null;
  private PatternMatcher  iMatcher       = new Perl5Matcher();
  private PatternCompiler iCompiler      = new Perl5Compiler();


  // constructors

  public OroRegExp( String pMatch ) throws RegExpException {
    this( pMatch, Standard.EMPTY, null );
  }

  public OroRegExp( String pMatch, RegExp.ModeSet pModeSet ) throws RegExpException {
    this( pMatch, Standard.EMPTY, pModeSet );
  }

  public OroRegExp( String pSearch, String pReplace ) throws RegExpException {
    this( pSearch, pReplace, null );
  }

  public OroRegExp( String pSearch, String pReplace, RegExp.ModeSet pModeSet ) throws RegExpException {
    String  search  = (StringInternal.null_arg( pSearch );
    String  replace = (StringInternal.null_arg( pReplace );
    // ModeSet can by null

    try {
      int flags = makeFlags( pModeSet );
      iPattern  = iCompiler.compile( search, flags );
      iReplace  = replace;
      iSubstitution = new Perl5Substitution( replace );
    }
    catch( MalformedPatternException e ) {
      throw new RegExpException( RegExpException.CODE_parse, e.getMessage()+" (pattern:'"+pSearch+"')" );
    }
  }



  public String replaceFirst( String pSource ) throws RegExpException {
    String source = (String) Internal.null_arg( pSource );
    return Util.substitute( iMatcher, iPattern, iSubstitution, source, 1 );
  }


  public String replaceAll( String pSource ) throws RegExpException {
    String source = (String) Internal.null_arg( pSource );
    return Util.substitute( iMatcher, iPattern, iSubstitution, source, Util.SUBSTITUTE_ALL );
  }


  public String match( String pSource ) throws RegExpException {
    String  source  = (String) Internal.null_arg( pSource );
    boolean matches = iMatcher.contains( source, iPattern );
    if( matches ) {
      return iMatcher.getMatch().toString();
    }
    else {
      return Standard.EMPTY;
    }
  }


  public boolean matches( String pSource ) throws RegExpException {
    String  source  = (String) Internal.null_arg( pSource );
    boolean matches = iMatcher.contains( source, iPattern );
    return matches;
  }



  public RegExpMatch matchFirst( String pSource ) throws RegExpException {
    String  source  = (String) Internal.null_arg( pSource );
    boolean matches = iMatcher.contains( source, iPattern );
    if( matches ) {
      return new OroRegExpMatch( iMatcher.getMatch() );
    }
    else {
      return new OroRegExpMatch();
    }
  }


  public RegExpMatch[] matchAll( String pSource ) throws RegExpException {
    String             source = (String) Internal.null_arg( pSource );

    PatternMatcherInput pmi = new PatternMatcherInput( pSource );
    ArrayList           mL  = new ArrayList();
   
    while( iMatcher.contains( pmi, iPattern ) ) {
      mL.add( new OroRegExpMatch( iMatcher.getMatch() ) );
    }

    if( 0 == mL.size() ) {
      return new RegExpMatch[] {};
    }
    else {
      RegExpMatch[] rem = new RegExpMatch[ mL.size() ];
      mL.toArray( rem );
      return rem;
    }
  }


  public String matchFirstSub( String pSource ) throws RegExpException {
    return matchImpl( pSource, 1 );
  }

  public String matchSecondSub( String pSource ) throws RegExpException {
    return matchImpl( pSource, 2 );
  }

  public String matchThirdSub( String pSource ) throws RegExpException {
    return matchImpl( pSource, 3 );
  }

  public String matchSub( String pSource, int pOrdinal ) throws RegExpException {
    return matchImpl( pSource, pOrdinal );
  }


  public String toString() {
    return CN+Standard.COLON+iPattern.toString();
  }


  // protected

  protected static String matchImpl( MatchResult pMatchResult, int pSubMatchIndex ) throws RegExpException {
    String ms = pMatchResult.group( pSubMatchIndex );
    if( null == ms ) {
      return Standard.EMPTY;
    }
    else {
      return ms;
    }
  }



  // private

  private String matchImpl( String pSource, int pSubMatchIndex ) throws RegExpException {
    String  source  = (String) Internal.null_arg( pSource );
    boolean matches = iMatcher.contains( source, iPattern );

    if( matches ) {
      MatchResult mr = iMatcher.getMatch();
      return matchImpl( mr, pSubMatchIndex );
    }
    else {
      return Standard.EMPTY;
    }
  }


  private int makeFlags( RegExp.ModeSet pModeSet ) {
    return
      null == pModeSet
      ? Perl5Compiler.READ_ONLY_MASK
      : Perl5Compiler.READ_ONLY_MASK
        | ( (pModeSet.getDotMatchesNewline() ? Perl5Compiler.SINGLELINE_MASK : 0 )
        | (pModeSet.getCaseInsensitive() ? Perl5Compiler.CASE_INSENSITIVE_MASK : 0 ) )
      ;
  }


}
TOP

Related Classes of org.jostraca.util.OroRegExp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.