01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util.transformers;
11:
12: import java.util.*;
13: import java.util.regex.Pattern;
14:
15: import org.mmbase.util.Entry;
16: import org.mmbase.util.logging.*;
17: import org.mmbase.util.functions.*;
18:
19: /**
20: * Factories new instances of extensions of {@link RegexpReplacer}, for which the replace patterns
21: * are parameterized (using the 'patterns' parameter).
22: *
23: * @author Michiel Meeuwissen
24: * @since MMBase-1.8
25: */
26:
27: public class RegexpReplacerFactory implements
28: ParameterizedTransformerFactory<CharTransformer> {
29: private static final Logger log = Logging
30: .getLoggerInstance(RegexpReplacerFactory.class);
31:
32: protected static final Parameter<Collection> PATTERNS = new Parameter<Collection>(
33: "patterns", Collection.class, Collections.emptyList());
34: protected static final Parameter<String> MODE = new Parameter<String>(
35: "mode", String.class, "WORDS");
36: protected static final Parameter<String> REPLACE_FIRST = new Parameter<String>(
37: "replacefirst", String.class);
38:
39: protected static final Parameter[] PARAMS = new Parameter[] {
40: PATTERNS, MODE, REPLACE_FIRST };
41:
42: public Parameters createParameters() {
43: return new Parameters(PARAMS);
44: }
45:
46: /**
47: * Creates a parameterized transformer.
48: */
49: public CharTransformer createTransformer(final Parameters parameters) {
50: parameters.checkRequiredParameters();
51: if (log.isDebugEnabled()) {
52: log.debug("Creating transformer, with " + parameters);
53: }
54: RegexpReplacer trans = new RegexpReplacer() {
55: private Collection<Entry<Pattern, String>> patterns = new ArrayList<Entry<Pattern, String>>();
56: {
57: addPatterns(parameters.get(PATTERNS), patterns);
58: }
59:
60: public Collection<Entry<Pattern, String>> getPatterns() {
61: return patterns;
62: }
63: };
64: String mode = parameters.get(MODE);
65: Config c = trans.transformers().get(
66: "REGEXPS_" + mode.toUpperCase());
67: if (c == null)
68: c = trans.transformers().get(mode);
69: if (c == null)
70: throw new IllegalArgumentException("" + mode
71: + " cannot be found in " + trans.transformers());
72: String firstParam = parameters.get(REPLACE_FIRST);
73: boolean replaceFirst = "true".equals(firstParam);
74: boolean replaceFirstAll = "all".equals(firstParam);
75: trans.configure(c.config
76: + (replaceFirst ? RegexpReplacer.REPLACE_FIRST : 0)
77: + (replaceFirstAll ? RegexpReplacer.REPLACE_FIRST_ALL
78: : 0));
79: return trans;
80: }
81:
82: public static void main(String[] argv) {
83: RegexpReplacerFactory fact = new RegexpReplacerFactory();
84: Parameters pars = fact.createParameters();
85: pars.set("mode", "ENTIRE");
86: List<Map.Entry<String, String>> patterns = new ArrayList<Map.Entry<String, String>>();
87: patterns.add(new Entry<String, String>("\\s+", " "));
88: pars.set("patterns", patterns);
89: CharTransformer reg = fact.createTransformer(pars);
90:
91: System.out.println(reg.transform(argv[0]));
92:
93: }
94:
95: }
|