/*
* Name: GnuRegExp
* 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.gnu.regexp.RE;
import org.jostraca.comp.gnu.regexp.REMatch;
import org.jostraca.comp.gnu.regexp.REMatchEnumeration;
import org.jostraca.comp.gnu.regexp.REException;
import java.util.Vector;
/** Facade for regular expression implementations.
*/
public class GnuRegExp extends RegExp {
// public static
public static final String CN = GnuRegExp.class.getName();
// private instance
private RE iRegExp = null;
private String iReplace = Standard.EMPTY;
// public methods
public GnuRegExp( String pMatch ) throws RegExpException {
this( pMatch, Standard.EMPTY, null );
}
public GnuRegExp( String pMatch, RegExp.ModeSet pModeSet ) throws RegExpException {
this( pMatch, Standard.EMPTY, pModeSet );
}
public GnuRegExp( String pSearch, String pReplace ) throws RegExpException {
this( pSearch, pReplace, null );
}
public GnuRegExp( String pSearch, String pReplace, ModeSet pModeSet ) throws RegExpException {
String search = (String) Internal.null_arg( pSearch );
String replace = (String) Internal.null_arg( pReplace );
try {
int flags = makeFlags( pModeSet );
iRegExp = new RE( search, flags );
iReplace = replace;
}
catch( REException ree ) {
throw new RegExpException( RegExpException.CODE_parse, ree.getMessage()+" (pattern:'"+pSearch+"')" );
}
}
public String replaceFirst( String pSource ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
return iRegExp.substitute( source, iReplace );
}
public String replaceAll( String pSource ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
return iRegExp.substituteAll( source, iReplace );
}
public String match( String pSource ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
REMatch m = iRegExp.getMatch( source );
if( null == m ) {
return Standard.EMPTY;
}
else {
return m.toString();
}
}
public boolean matches( String pSource ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
return iRegExp.isMatch( source );
}
public RegExpMatch matchFirst( String pSource ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
REMatch m = iRegExp.getMatch( source );
if( null == m ) {
return new GnuRegExpMatch();
}
else {
return new GnuRegExpMatch( m );
}
}
public RegExpMatch[] matchAll( String pSource ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
REMatchEnumeration me = iRegExp.getMatchEnumeration( source );
if( null == me ) {
return new RegExpMatch[] {};
}
else {
Vector mV = new Vector();
while( me.hasMoreElements() ) {
REMatch m = (REMatch) me.nextElement();
mV.addElement( new GnuRegExpMatch( m ) );
}
RegExpMatch[] rem = new RegExpMatch[ mV.size() ];
mV.copyInto( 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+iRegExp.toString();
}
// private methods
private String matchImpl( String pSource, int pSubMatchIndex ) throws RegExpException {
String source = (String) Internal.null_arg( pSource );
REMatch m = iRegExp.getMatch( source );
if( null == m ) {
return Standard.EMPTY;
}
else {
try {
return m.toString( pSubMatchIndex );
}
catch( ArrayIndexOutOfBoundsException aioobe ) {
return Standard.EMPTY;
}
}
}
private int makeFlags( RegExp.ModeSet pModeSet ) {
return
null == pModeSet
? 0
: ( (pModeSet.getDotMatchesNewline() ? RE.REG_DOT_NEWLINE : 0 )
| (pModeSet.getCaseInsensitive() ? RE.REG_ICASE : 0 ) )
;
}
}