01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.solr.analysis;
17:
18: import org.apache.lucene.analysis.TokenStream;
19:
20: import java.util.Map;
21: import java.util.regex.Pattern;
22: import java.util.regex.Matcher;
23: import java.util.regex.PatternSyntaxException;
24:
25: /**
26: * @version $Id:$
27: * @see PatternReplaceFilter
28: */
29: public class PatternReplaceFilterFactory extends BaseTokenFilterFactory {
30: Pattern p;
31: String replacement;
32: boolean all = true;
33:
34: public void init(Map<String, String> args) {
35: super .init(args);
36: try {
37: p = Pattern.compile(args.get("pattern"));
38: } catch (PatternSyntaxException e) {
39: throw new RuntimeException(
40: "Configuration Error: 'pattern' can not be parsed in "
41: + this .getClass().getName(), e);
42: }
43:
44: replacement = args.get("replacement");
45:
46: String r = args.get("replace");
47: if (null != r) {
48: if (r.equals("all")) {
49: all = true;
50: } else {
51: if (r.equals("first")) {
52: all = false;
53: } else {
54: throw new RuntimeException(
55: "Configuration Error: 'replace' must be 'first' or 'all' in "
56: + this .getClass().getName());
57: }
58: }
59: }
60:
61: }
62:
63: public TokenStream create(TokenStream input) {
64: return new PatternReplaceFilter(input, p, replacement, all);
65: }
66: }
|