package util;
import org.apache.oro.text.regex.*;
import org.apache.oro.text.perl.*;
import java.util.*;
/**
*provided as a wrapper so that we could substitute these with
* other calls in case we cant deploy the perl libraries currently used.
@author rahul kumar
@version $Id: PerlWrapper.java,v 1.1 2004/01/01 06:46:33 rahul_kumar Exp rahul $
*/
public class PerlWrapper {
public static Perl5Util perl = new Perl5Util();
/**
* Takes a regular expression and string as input and reports all the
* pattern matches in the string.
* <p>
* @param args[] The array of arguments to the program. The first
* argument should be a Perl5 regular expression, and the second
* should be an input string.
*/
/*
public static final void main(String args[]) {
MultipleMatchModel mmm = new MultipleMatchModel(){
Hashtable ht = new Hashtable();
String value;
public void setValueAt(String s, int row, int group){
if (group==1) value = s;
else
if (group==2)
ht.put(s,value);
}
public String getValueAt (int row, int group){
return null;
}
public Object getObject (){
return ht;
}
};
try {
getMultipleMatches(
"((?:\\w+\\.)*[A-Z]\\w*)\\s+(\\w+)", "java.io.Exception exc; Date f1 = new Date(ccc); f1.get(ZZZ); Random ran = new Random(); Date rand ;", mmm);
Hashtable hta = (Hashtable) mmm.getObject();
System.out.println( "f1:"+ (String)hta.get("f1"));
System.out.println( "exc:"+ (String)hta.get("exc"));
System.out.println( "ran:"+ (String)hta.get("ran"));
System.out.println( "rand:"+ (String)hta.get("rand"));
} catch (Exception exc) { System.err.println( "pw EXC:"+ exc.toString()); }
}
*/
/** receives a regular expression and a string to search in,
* it updates the given model object.
* The purpose of the model object is that we do not know how many
* groups the regexp has, and what to do with those groups. Does
* the user want a hashtable with a particular group as key, and
* another as value or what.
*/
public static void getMultipleMatches (String regexp,
String content, MultipleMatchModel mmm)
throws MalformedPatternException {
int groups;
PatternMatcher matcher;
PatternCompiler compiler;
Pattern pattern = null;
PatternMatcherInput input;
MatchResult result;
// Create Perl5Compiler and Perl5Matcher instances.
compiler = new Perl5Compiler();
matcher = new Perl5Matcher();
// Attempt to compile the pattern. If the pattern is not valid,
// report the error and exit.
pattern = compiler.compile(regexp);
// Create a PatternMatcherInput instance to keep track of the position
// where the last match finished, so that the next match search will
// start from there. You always create a PatternMatcherInput instance
// when you want to search a string for all of the matches it contains,
// and not just the first one.
input = new PatternMatcherInput(content);
// Loop until there are no more matches left.
int row = 0;
while(matcher.contains(input, pattern)) {
// Since we're still in the loop, fetch match that was found.
result = matcher.getMatch();
// Retrieve the number of matched groups. A group corresponds to
// a parenthesized set in a pattern.
groups = result.groups();
for(int group = 1; group < groups; group++) {
mmm.setValueAt( result.group(group), row, group);
}
row++;
}
}
/** returns true if the regular expression and text match.
* Slashes will be placed around the regex sent.
*/
public static boolean isMatching(String regex, String text){
return (perl.match("/"+regex+"/", text));
}
/** caller must supply / on both sides with whatever flags he wants
* such as i for ignore case.
* e.g. /CREATE/i
*/
public static boolean isMatchingRE(String regex, String text){
return (perl.match(regex, text));
}
/** returns the results of a single match on a string, returning all
* matched groups.
* e.g.
* perlMatch("(public|protected)\\s+(class|interface)\\s+(\w+)",
* "public class Hey") would return an array {"public","class","Hey"}
*/
public static String[] perlMatch(String regex, String text){
if (perl.match("/"+regex+"/", text)){
int gr = perl.groups();
String[] result = new String[gr-1];
for (int i = 1; i < gr; i++){
result[i-1] = perl.group(i);
}
return result;
}
return null;
}
public static String[] perlMatchRE(String regex, String text){
if (perl.match(regex, text)){
int gr = perl.groups();
String[] result = new String[gr-1];
for (int i = 1; i < gr; i++){
result[i-1] = perl.group(i);
}
return result;
}
return null;
}
/** will substitute according to the pattern in the text and return
* the new text.
* e.g. line = perlSubstitute("s/$ID/+ var +"/g", line);
* for interpolations use $1 $2 etc
* /g means global replace
*/
public static String perlSubstitute (String pattern, String text){
return perl.substitute(pattern, text);
}
/** it may be preferable to use perl's split for single matches */
public static MultipleMatchModel SimpleMultipleMatchModel(){
return new MultipleMatchModel(){
ArrayList al = new ArrayList();
String value;
public void setValueAt(String s, int row, int group){
if (group==1) al.add(s);
}
public String getValueAt (int row, int group){
return (String) al.get(row);
}
public Object getObject (){
return al;
}
};
}
/** splits based on regexp sent in format '/regexp/'
*/
public static void perlSplit (Collection coll, String pattern, String input)
throws MalformedPerl5PatternException {
perl.split(coll, pattern, input);
}
/** splits based on regexp sent in format '/regexp/'
* The regexp is the split string.
*/
public static String[] perlSplit (String pattern, String input)
throws MalformedPerl5PatternException {
ArrayList al = new ArrayList(16);
perl.split(al, pattern, input);
String[] sarr = new String[ al.size() ];
al.toArray(sarr);
return (sarr);
}
public static void main (String args[]){
String input = "insert into tt ';' ;\ninsert into t1;\ninsert into mary;\n";
String pattern = null;
if (args.length==1) pattern = args[0];
else pattern = "/;\n/";
Vector v = new Vector();
perl.split(v, pattern, input);
System.out.println( v.size());
for( int i = 0; i < v.size(); i++ ){
System.out.println(i+" "+v.get(i));
}
String ss = "abc<tab></tab>ad sda";
System.out.println( "\n original 1:"+ss);
String news = PerlWrapper.perlSubstitute("s/<tab>.*?<\\/tab>/<tab>HAHA<\\/tab>/g", ss);
System.out.println( " put HAHA in 2:"+news);
news = PerlWrapper.perlSubstitute("s/<tab>.*?<\\/tab>/<tab>HAHA\n<\\/tab>/g", news);
System.out.println( " put HAHA with newline 3:"+news);
// XXX this fails since new line inside
news = PerlWrapper.perlSubstitute("s/<tab>.*?<\\/tab>/<tab>HANEWHA<\\/tab>/g", news);
System.out.println( " put HENEWHA 4:"+news);
String sql = "SELECT * from table where tsdate > #m-3#";
String m[] = perlMatch("#(\\S+)#", sql);
System.out.println( "match:"+ m[0]);
}
}
|