From bfdb95f01b1af2e07ea42e549c87cf3264eb894a Mon Sep 17 00:00:00 2001 From: "richm%stanfordalumni.org" Date: Fri, 23 Sep 2005 15:05:55 +0000 Subject: [PATCH] Bug: 309518 Description: Replace ORO Regexp with java 1.4 Pattern/Matcher Fix Description: I removed any and all references to oro from build.properties and build.xml. For the code replacement, the conversion was pretty straightforward. I had to slightly modify the regular expressions but for the most part they just work the same as the oro ones. ORO uses a PatternInput to hold the state of the iteration over the string to match, but the Matcher class does that implicitly. For the most part, it didn't matter because the code didn't really use the state, it would just always reset the PatternInput to the beginning of the string, so I just used the String instead. There was one place where I needed to keep track of state, so I used the Matcher object instead of a Perl5Pattern + PatternInput. Thanks to nkinder@redhat.com for the review. git-svn-id: svn://10.0.0.236/trunk@180864 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/directory/java-sdk/build.properties | 11 +--- mozilla/directory/java-sdk/build.xml | 8 +-- .../ldap/util/BadFilterException.java | 2 +- .../ldap/util/LDAPFilterDescriptor.java | 62 +++++++++---------- .../netscape/ldap/util/LDAPIntFilterList.java | 23 +++---- .../netscape/ldap/util/LDAPIntFilterSet.java | 40 +++--------- 6 files changed, 48 insertions(+), 98 deletions(-) diff --git a/mozilla/directory/java-sdk/build.properties b/mozilla/directory/java-sdk/build.properties index 393a0fc9c17..a3463c461c2 100644 --- a/mozilla/directory/java-sdk/build.properties +++ b/mozilla/directory/java-sdk/build.properties @@ -4,14 +4,5 @@ # This is an example "build.properties" file, used to customize building # Mozilla Java LDAP SDK for your local environment. # -# $Id: build.properties,v 1.1 2003-06-10 01:00:05 miodrag%netscape.com Exp $ +# $Id: build.properties,v 1.2 2005-09-23 15:05:54 richm%stanfordalumni.org Exp $ # ----------------------------------------------------------------------------- - - -# ----- OROMatcher regular expression jar file location ----- -# place the location of this file in the following line. -# the library may be downloaded from http://www.oroinc.com - -#ororegexp.jar=../lib/oroinc.jar - - diff --git a/mozilla/directory/java-sdk/build.xml b/mozilla/directory/java-sdk/build.xml index 9e0f987116b..dd790587d50 100644 --- a/mozilla/directory/java-sdk/build.xml +++ b/mozilla/directory/java-sdk/build.xml @@ -85,10 +85,6 @@ clean : will remove all compiled files and packages clean-dists : will remove all the jars and zips clean-builds : will remove all the compiled classes - - NOTE: in order to compile the filter classes you will need to - edit the build.properties file and place the path to - your oro pattern matching jar in it. @@ -186,11 +182,9 @@ - - diff --git a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/BadFilterException.java b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/BadFilterException.java index 7568db62aa6..79fa616f5e3 100644 --- a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/BadFilterException.java +++ b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/BadFilterException.java @@ -80,7 +80,7 @@ public class BadFilterException extends Exception { * this error occurred. */ void setErrorLineNumber ( int nErrorLineNumber ) { - m_nLine = m_nLine; + m_nLine = nErrorLineNumber; } } diff --git a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPFilterDescriptor.java b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPFilterDescriptor.java index 3c07a10db78..6c18eae48ee 100644 --- a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPFilterDescriptor.java +++ b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPFilterDescriptor.java @@ -23,8 +23,10 @@ package netscape.ldap.util; import java.io.*; import java.util.*; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.util.regex.PatternSyntaxException; import java.net.*; -import com.oroinc.text.regex.*; import netscape.ldap.*; /** @@ -179,17 +181,16 @@ public class LDAPFilterDescriptor { private void init ( Object inputObj) throws BadFilterException { - String strCommentPattern = "(?:^\\s*#|^\\s*$)"; - String strDataPattern = "(?:\\s*\"([^\"]*)\"|([^\\s]*))\\s*"; - Perl5Compiler compiler = new Perl5Compiler(); - Perl5Pattern patComment; - Perl5Pattern patData; + String strCommentPattern = "^\\s*#|$"; + String strDataPattern = "\\s*(?:\"([^\"]*)\")|([^\\s]*)\\s*"; + Pattern patComment; + Pattern patData; Vector vStrings = new Vector ( 5 ); try { - patComment = (Perl5Pattern)compiler.compile ( strCommentPattern ); - patData = (Perl5Pattern)compiler.compile ( strDataPattern ); - } catch ( MalformedPatternException e ) { + patComment = Pattern.compile ( strCommentPattern ); + patData = Pattern.compile ( strDataPattern ); + } catch ( PatternSyntaxException e ) { // This should NEVER happen... System.out.println ( "FATAL Error, couldn't compile pattern"); System.out.println ( " " + e.getMessage() ); @@ -229,23 +230,21 @@ public class LDAPFilterDescriptor { } } - private void setFilter(Perl5Pattern patComment, Perl5Pattern patData, + private void setFilter(Pattern patComment, Pattern patData, Vector vStrings) throws IOException, BadFilterException { - MatchResult result; - Perl5Matcher matcher = new Perl5Matcher(); - PatternMatcherInput input; + Matcher dataMatcher; LDAPFilter tmpFilter = null; - input = new PatternMatcherInput ( m_strLine ); - - if ( ! ( matcher.contains ( input, patComment ) ) ) { - input.setCurrentOffset(input.getBeginOffset()); + if ( ! patComment.matcher(m_strLine).lookingAt() ) { +// System.out.println("comment pattern " + patComment.pattern() + +// " does not match " + m_strLine); + dataMatcher = patData.matcher(m_strLine); // System.out.println ( "\nNEW LINE: " + m_strLine ); if ( ! vStrings.isEmpty() ) { vStrings.removeAllElements(); } - while ( matcher.contains ( input, patData ) ) { + while ( dataMatcher.find() ) { // Within this while loop, we're looking for // all the data tokens. Our regular // expression is setup to look for words @@ -254,13 +253,13 @@ public class LDAPFilterDescriptor { // of the regexp is that we have two // backreferences, only one will have data at // any time. - result = matcher.getMatch(); - for ( int i = 1; i <=2; i++ ) { - if ( result.group(i) != null ) { - if ( ! result.group(i).equals ( "" ) ) { - //System.out.println ( "Match #" + i + - // ": \"" + result.group(i) + "\"" ); - vStrings.addElement ( result.group(i)); + int groupCount = dataMatcher.groupCount(); + for ( int i = 1; i <= groupCount; i++ ) { + if ( dataMatcher.group(i) != null ) { + if ( ! dataMatcher.group(i).equals ( "" ) ) { +// System.out.println ( "Match #" + i + +// ": \"" + dataMatcher.group(i) + "\"" ); + vStrings.addElement ( dataMatcher.group(i)); } } } @@ -437,20 +436,15 @@ public class LDAPFilterDescriptor { LDAPFilterList retList = new LDAPFilterList(); - Perl5Compiler compiler = new Perl5Compiler(); - Perl5Pattern patTag; // The strTagPat that's compiled - - // For efficiency, we're pre-allocating a patternMatcherInput - // here. - PatternMatcherInput matcherValue = new PatternMatcherInput ( strValue ); + Pattern patTag; // The strTagPat that's compiled // first we need to make a new regexp from the strTagPat // For efficiency, we're precompiling the strTagPat into // a pattern here. That pattern doesn't change, the Tag string // changes per LDAPFIlterSet. try { - patTag = (Perl5Pattern)compiler.compile ( strTagPat ); - } catch ( MalformedPatternException e ) { + patTag = Pattern.compile ( strTagPat ); + } catch ( PatternSyntaxException e ) { throw new IllegalArgumentException ( "The parameter: " + strTagPat + " is not valid" ); } @@ -462,7 +456,7 @@ public class LDAPFilterDescriptor { while ( ! bMatched ) { Vector vMatchingFilters = ((LDAPIntFilterSet)m_vFilterSet.elementAt ( i )).getFilters - (patTag, matcherValue ); + (patTag, strValue ); if ( vMatchingFilters.size() > 0 ) { for ( int j = 0; j < vMatchingFilters.size(); j++ ) { diff --git a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterList.java b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterList.java index e8b23e06a45..03ca0d1aa63 100644 --- a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterList.java +++ b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterList.java @@ -22,7 +22,8 @@ package netscape.ldap.util; import java.util.*; -import com.oroinc.text.regex.*; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; /** * Represents an Internal LDAPFilterList object. This is an internal object @@ -34,24 +35,15 @@ public class LDAPIntFilterList { private Vector m_vFilter; private String m_strMatchPattern; // a regexp pattern of m_strMatchPattern - private Perl5Pattern m_patMatch = null; - private Perl5Matcher m_matcher; - private Perl5Compiler m_compiler; + private Pattern m_patMatch = null; LDAPIntFilterList ( LDAPFilter filter ) throws BadFilterException { m_strMatchPattern = filter.getMatchPattern(); - // We're going to compile the pattern for strMatchPattern - // now, so that we can throw an exception if it is a bad - // pattern. - m_matcher = new Perl5Matcher(); - m_compiler = new Perl5Compiler(); - try { - m_patMatch = (Perl5Pattern)m_compiler.compile - ( m_strMatchPattern ); - } catch ( MalformedPatternException e ) { + m_patMatch = Pattern.compile( m_strMatchPattern ); + } catch ( PatternSyntaxException e ) { throw new BadFilterException ( "The Regular Expression for this filter is bad. " + @@ -109,9 +101,8 @@ public class LDAPIntFilterList { * m_strMatchPattern) to the value that the user typed in (the * parameter to this method). */ - boolean MatchFilter ( PatternMatcherInput matcherValue ) { - matcherValue.setCurrentOffset ( matcherValue.getBeginOffset() ); - return m_matcher.contains ( matcherValue, m_patMatch ); + boolean MatchFilter ( String matcherValue ) { + return m_patMatch.matcher(matcherValue).matches(); } } diff --git a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterSet.java b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterSet.java index 050c3585404..bbc607eb9cd 100644 --- a/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterSet.java +++ b/mozilla/directory/java-sdk/ldapfilter/netscape/ldap/util/LDAPIntFilterSet.java @@ -22,7 +22,9 @@ package netscape.ldap.util; import java.util.*; -import com.oroinc.text.regex.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Represents an LDAPIntFilterSet object. This is an internal object that * should never be instantiated directly by the developer. @@ -33,8 +35,7 @@ public class LDAPIntFilterSet { private Vector m_vLDAPIntFilterList; private String m_strTag; - private PatternMatcherInput m_matcherTag = null; - private Perl5Matcher m_matcher = null; + private Matcher m_matcher = null; /** * Return a Vector of filters that match botht the tag pattern * (in Perl5Pattern form), and the string strValue. This method @@ -43,21 +44,18 @@ public class LDAPIntFilterSet { // remember, we have the string (m_strTag), the pattern has // been precompiled by the LDAPFilterDescriptor (patTag) - Vector getFilters ( Perl5Pattern patTag, - PatternMatcherInput matcherValue ) { + Vector getFilters ( Pattern patTag, + String matcherValue ) { Vector vRet = new Vector(); - if ( m_matcherTag == null ) { - m_matcher = new Perl5Matcher(); - m_matcherTag = new PatternMatcherInput ( m_strTag ); - } else { - m_matcherTag.setCurrentOffset ( m_matcherTag.getBeginOffset() ); + if ( m_matcher == null ) { + m_matcher = patTag.matcher(m_strTag); } // Check to see if the strTag (converted into patTag) // matches the tag string from the file (converted into // m_matcherTag) - if ( m_matcher.contains ( m_matcherTag, patTag ) ) { + if ( m_matcher.find() ) { LDAPIntFilterList tmpIntFilterList; LDAPFilter tmpFilter; for ( int i = 0; i < m_vLDAPIntFilterList.size(); i++ ) { @@ -121,25 +119,7 @@ public class LDAPIntFilterSet { * string that is passed in. */ boolean match ( String strTagPat ) { - Perl5Matcher matcher = new Perl5Matcher(); - Perl5Compiler compiler = new Perl5Compiler(); - PatternMatcherInput input; - Perl5Pattern patTag; - MatchResult result; - - try { - patTag = (Perl5Pattern)compiler.compile ( strTagPat ); - } catch ( MalformedPatternException e ) { - // Need to do something here. - return false; - } - - input = new PatternMatcherInput ( m_strTag ); - if ( matcher.contains ( input, patTag ) ) { - return true; - } else { - return false; - } + return Pattern.matches(strTagPat, m_strTag); } /**