lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@apache.org
Subject svn commit: r897357 - in /lucene/solr/trunk/src: java/org/apache/solr/analysis/PatternReplaceCharFilter.java java/org/apache/solr/analysis/PatternReplaceCharFilterFactory.java test/org/apache/solr/analysis/TestPatternReplaceCharFilter.java
Date Sat, 09 Jan 2010 00:45:46 GMT
Author: koji
Date: Sat Jan  9 00:45:45 2010
New Revision: 897357

URL: http://svn.apache.org/viewvc?rev=897357&view=rev
Log:
SOLR-1653: PatternReplaceCharFilter should receive a Pattern object instead of pattern String

Modified:
    lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilter.java
    lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilterFactory.java
    lucene/solr/trunk/src/test/org/apache/solr/analysis/TestPatternReplaceCharFilter.java

Modified: lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilter.java?rev=897357&r1=897356&r2=897357&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilter.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilter.java Sat
Jan  9 00:45:45 2010
@@ -63,24 +63,24 @@
   private String replaceBlockBuffer;
   private int replaceBlockBufferOffset;
   
-  public PatternReplaceCharFilter( String pattern, String replacement, CharStream in ){
+  public PatternReplaceCharFilter( Pattern pattern, String replacement, CharStream in ){
     this( pattern, replacement, DEFAULT_MAX_BLOCK_CHARS, null, in );
   }
 
-  public PatternReplaceCharFilter( String pattern, String replacement,
+  public PatternReplaceCharFilter( Pattern pattern, String replacement,
       int maxBlockChars, CharStream in ){
     this( pattern, replacement, maxBlockChars, null, in );
   }
 
-  public PatternReplaceCharFilter( String pattern, String replacement,
+  public PatternReplaceCharFilter( Pattern pattern, String replacement,
       String blockDelimiters, CharStream in ){
     this( pattern, replacement, DEFAULT_MAX_BLOCK_CHARS, blockDelimiters, in );
   }
 
-  public PatternReplaceCharFilter( String pattern, String replacement,
+  public PatternReplaceCharFilter( Pattern pattern, String replacement,
       int maxBlockChars, String blockDelimiters, CharStream in ){
     super( in );
-    this.pattern = Pattern.compile( pattern );
+    this.pattern = pattern;
     this.replacement = replacement;
     if( maxBlockChars < 1 )
       throw new IllegalArgumentException( "maxBlockChars should be greater than 0, but it
is " + maxBlockChars );

Modified: lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilterFactory.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilterFactory.java?rev=897357&r1=897356&r2=897357&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilterFactory.java
(original)
+++ lucene/solr/trunk/src/java/org/apache/solr/analysis/PatternReplaceCharFilterFactory.java
Sat Jan  9 00:45:45 2010
@@ -18,6 +18,8 @@
 package org.apache.solr.analysis;
 
 import java.util.Map;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 
 import org.apache.lucene.analysis.CharStream;
 
@@ -28,16 +30,20 @@
  */
 public class PatternReplaceCharFilterFactory extends BaseCharFilterFactory {
   
-  private String pattern;
+  private Pattern p;
   private String replacement;
   private int maxBlockChars;
   private String blockDelimiters;
 
   public void init(Map<String, String> args) {
     super.init( args );
-    pattern = args.get( "pattern" );
-    if( pattern == null )
-      pattern = "";
+    try {
+      p = Pattern.compile(args.get("pattern"));
+    } catch (PatternSyntaxException e) {
+      throw new RuntimeException
+        ("Configuration Error: 'pattern' can not be parsed in " +
+         this.getClass().getName(), e);
+    }
     replacement = args.get( "replacement" );
     if( replacement == null )
       replacement = "";
@@ -46,6 +52,6 @@
   }
 
   public CharStream create(CharStream input) {
-    return new PatternReplaceCharFilter( pattern, replacement, maxBlockChars, blockDelimiters,
input );
+    return new PatternReplaceCharFilter( p, replacement, maxBlockChars, blockDelimiters,
input );
   }
 }

Modified: lucene/solr/trunk/src/test/org/apache/solr/analysis/TestPatternReplaceCharFilter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/analysis/TestPatternReplaceCharFilter.java?rev=897357&r1=897356&r2=897357&view=diff
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/analysis/TestPatternReplaceCharFilter.java
(original)
+++ lucene/solr/trunk/src/test/org/apache/solr/analysis/TestPatternReplaceCharFilter.java
Sat Jan  9 00:45:45 2010
@@ -21,6 +21,7 @@
 import java.io.StringReader;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 import org.apache.lucene.analysis.CharReader;
 import org.apache.lucene.analysis.CharStream;
@@ -94,7 +95,7 @@
   // aa##bb###cc dd
   public void test1block1matchLonger() throws IOException {
     final String BLOCK = "aa bb cc dd";
-    CharStream cs = new PatternReplaceCharFilter( "(aa)\\s+(bb)\\s+(cc)", "$1##$2###$3",
+    CharStream cs = new PatternReplaceCharFilter( pattern("(aa)\\s+(bb)\\s+(cc)"), "$1##$2###$3",
           CharReader.get( new StringReader( BLOCK ) ) );
     TokenStream ts = new WhitespaceTokenizer( cs );
     assertTokenStreamContents(ts,
@@ -109,7 +110,7 @@
   //  aa  aa
   public void test1block2matchLonger() throws IOException {
     final String BLOCK = " a  a";
-    CharStream cs = new PatternReplaceCharFilter( "a", "aa",
+    CharStream cs = new PatternReplaceCharFilter( pattern("a"), "aa",
           CharReader.get( new StringReader( BLOCK ) ) );
     TokenStream ts = new WhitespaceTokenizer( cs );
     assertTokenStreamContents(ts,
@@ -125,7 +126,7 @@
   // aa#bb dd
   public void test1block1matchShorter() throws IOException {
     final String BLOCK = "aa  bb   cc dd";
-    CharStream cs = new PatternReplaceCharFilter( "(aa)\\s+(bb)\\s+(cc)", "$1#$2",
+    CharStream cs = new PatternReplaceCharFilter( pattern("(aa)\\s+(bb)\\s+(cc)"), "$1#$2",
           CharReader.get( new StringReader( BLOCK ) ) );
     TokenStream ts = new WhitespaceTokenizer( cs );
     assertTokenStreamContents(ts,
@@ -141,7 +142,7 @@
   //   aa  bb  cc --- aa bb aa  bb  cc
   public void test1blockMultiMatches() throws IOException {
     final String BLOCK = "  aa bb cc --- aa bb aa   bb   cc";
-    CharStream cs = new PatternReplaceCharFilter( "(aa)\\s+(bb)\\s+(cc)", "$1  $2  $3",
+    CharStream cs = new PatternReplaceCharFilter( pattern("(aa)\\s+(bb)\\s+(cc)"), "$1  $2
 $3",
           CharReader.get( new StringReader( BLOCK ) ) );
     TokenStream ts = new WhitespaceTokenizer( cs );
     assertTokenStreamContents(ts,
@@ -157,7 +158,7 @@
   //   aa##bb cc --- aa##bb aa. bb aa##bb cc
   public void test2blocksMultiMatches() throws IOException {
     final String BLOCK = "  aa bb cc --- aa bb aa. bb aa   bb cc";
-    CharStream cs = new PatternReplaceCharFilter( "(aa)\\s+(bb)", "$1##$2", ".",
+    CharStream cs = new PatternReplaceCharFilter( pattern("(aa)\\s+(bb)"), "$1##$2", ".",
           CharReader.get( new StringReader( BLOCK ) ) );
     TokenStream ts = new WhitespaceTokenizer( cs );
     assertTokenStreamContents(ts,
@@ -173,10 +174,10 @@
   //  aa b - c . --- b aa . c c b
   public void testChain() throws IOException {
     final String BLOCK = " a bb - ccc . --- bb a . ccc ccc bb";
-    CharStream cs = new PatternReplaceCharFilter( "a", "aa", ".",
+    CharStream cs = new PatternReplaceCharFilter( pattern("a"), "aa", ".",
         CharReader.get( new StringReader( BLOCK ) ) );
-    cs = new PatternReplaceCharFilter( "bb", "b", ".", cs );
-    cs = new PatternReplaceCharFilter( "ccc", "c", ".", cs );
+    cs = new PatternReplaceCharFilter( pattern("bb"), "b", ".", cs );
+    cs = new PatternReplaceCharFilter( pattern("ccc"), "c", ".", cs );
     TokenStream ts = new WhitespaceTokenizer( cs );
     assertTokenStreamContents(ts,
         new String[] { "aa", "b", "-", "c", ".", "---", "b", "aa", ".", "c", "c", "b" },
@@ -184,4 +185,8 @@
         new int[] { 2, 5, 7, 11, 13, 17, 20, 22, 24, 28, 32, 35 },
         new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
   }
+  
+  private Pattern pattern( String p ){
+    return Pattern.compile( p );
+  }
 }



Mime
View raw message