~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/queries/src/java/org/apache/lucene/search/regex/JakartaRegexpCapabilities.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search.regex;
2
 
 
3
 
/**
4
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
5
 
 * contributor license agreements.  See the NOTICE file distributed with
6
 
 * this work for additional information regarding copyright ownership.
7
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
8
 
 * (the "License"); you may not use this file except in compliance with
9
 
 * the License.  You may obtain a copy of the License at
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
13
 
 * Unless required by applicable law or agreed to in writing, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 */
19
 
 
20
 
import org.apache.regexp.RE;
21
 
import org.apache.regexp.REProgram;
22
 
import java.lang.reflect.Field;
23
 
import java.lang.reflect.Method;
24
 
 
25
 
/**
26
 
 * Implementation tying <a href="http://jakarta.apache.org/regexp">Jakarta
27
 
 * Regexp</a> to RegexQuery. Jakarta Regepx internally supports a
28
 
 * {@link #prefix} implementation which can offer performance gains under
29
 
 * certain circumstances. Yet, the implementation appears to be rather shaky as
30
 
 * it doesn't always provide a prefix even if one would exist.
31
 
 */
32
 
public class JakartaRegexpCapabilities implements RegexCapabilities {
33
 
  private RE regexp;
34
 
 
35
 
  private static Field prefixField;
36
 
  private static Method getPrefixMethod;
37
 
  static {
38
 
    try {
39
 
      getPrefixMethod = REProgram.class.getMethod("getPrefix");
40
 
    } catch (Exception e) {
41
 
      getPrefixMethod = null;
42
 
    }
43
 
    try {
44
 
      prefixField = REProgram.class.getDeclaredField("prefix");
45
 
      prefixField.setAccessible(true);
46
 
    } catch (Exception e) {
47
 
      prefixField = null;
48
 
    }
49
 
  }
50
 
  
51
 
  // Define the flags that are possible. Redefine them here
52
 
  // to avoid exposing the RE class to the caller.
53
 
  
54
 
  private int flags = RE.MATCH_NORMAL;
55
 
 
56
 
  /**
57
 
   * Flag to specify normal, case-sensitive matching behaviour. This is the default.
58
 
   */
59
 
  public static final int FLAG_MATCH_NORMAL = RE.MATCH_NORMAL;
60
 
  
61
 
  /**
62
 
   * Flag to specify that matching should be case-independent (folded)
63
 
   */
64
 
  public static final int FLAG_MATCH_CASEINDEPENDENT = RE.MATCH_CASEINDEPENDENT;
65
 
 
66
 
  /**
67
 
   * Constructs a RegexCapabilities with the default MATCH_NORMAL match style.
68
 
   */
69
 
  public JakartaRegexpCapabilities() {}
70
 
  
71
 
  /**
72
 
   * Constructs a RegexCapabilities with the provided match flags.
73
 
   * Multiple flags should be ORed together.
74
 
   * 
75
 
   * @param flags The matching style
76
 
   */
77
 
  public JakartaRegexpCapabilities(int flags)
78
 
  {
79
 
    this.flags = flags;
80
 
  }
81
 
  
82
 
  public void compile(String pattern) {
83
 
    regexp = new RE(pattern, this.flags);
84
 
  }
85
 
 
86
 
  public boolean match(String string) {
87
 
    return regexp.match(string);
88
 
  }
89
 
 
90
 
  public String prefix() {
91
 
    try {
92
 
      final char[] prefix;
93
 
      if (getPrefixMethod != null) {
94
 
        prefix = (char[]) getPrefixMethod.invoke(regexp.getProgram());
95
 
      } else if (prefixField != null) {
96
 
        prefix = (char[]) prefixField.get(regexp.getProgram());
97
 
      } else {
98
 
        return null;
99
 
      }
100
 
      return prefix == null ? null : new String(prefix);
101
 
    } catch (Exception e) {
102
 
      // if we cannot get the prefix, return none
103
 
      return null;
104
 
    }
105
 
  }
106
 
 
107
 
  @Override
108
 
  public boolean equals(Object o) {
109
 
    if (this == o) return true;
110
 
    if (o == null || getClass() != o.getClass()) return false;
111
 
 
112
 
    final JakartaRegexpCapabilities that = (JakartaRegexpCapabilities) o;
113
 
 
114
 
    if (regexp != null ? !regexp.equals(that.regexp) : that.regexp != null) return false;
115
 
 
116
 
    return true;
117
 
  }
118
 
 
119
 
  @Override
120
 
  public int hashCode() {
121
 
    return (regexp != null ? regexp.hashCode() : 0);
122
 
  }
123
 
}