1 /**
2 * Copyright (c) 2004-2011 QOS.ch
3 * All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25 package org.slf4j.migrator.line;
26
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 /**
31 * This class represents a conversion rule It uses a Pattern and defines for
32 * each capturing group of this Pattern a replacement text
33 *
34 * @author jean-noelcharpin
35 *
36 */
37 public class MultiGroupConversionRule implements ConversionRule {
38
39 // It is extremely unlikely to encounter more than 10 groups in one of
40 // our conversion reg-expressions
41 final private static int MAX_GROUPS = 10;
42
43 private final Pattern pattern;
44 private final String[] replacementTable = new String[MAX_GROUPS];
45
46 public MultiGroupConversionRule(Pattern pattern) {
47 this.pattern = pattern;
48 }
49
50 /*
51 * (non-Javadoc)
52 *
53 * @see org.slf4j.converter.ConversionRule#getPattern()
54 */
55 public Pattern getPattern() {
56 return pattern;
57 }
58
59 public void addReplacement(int groupIndex, String replacement) {
60 if (groupIndex == 0) {
61 throw new IllegalArgumentException("regex groups start at 1, not zero");
62 }
63 replacementTable[groupIndex] = replacement;
64 }
65
66 /*
67 * (non-Javadoc)
68 *
69 * @see org.slf4j.converter.ConversionRule#getReplacement(java.lang.Integer)
70 */
71 public String getReplacement(int groupIndex) {
72 return replacementTable[groupIndex];
73 }
74
75 /*
76 * (non-Javadoc)
77 *
78 * @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
79 */
80 public String replace(Matcher matcher) {
81 StringBuilder replacementBuffer = new StringBuilder();
82 String replacementText;
83
84 for (int group = 1; group <= matcher.groupCount(); group++) {
85 replacementText = getReplacement(group);
86 if (replacementText != null) {
87 // System.out.println("replacing group " + group + " : "
88 // + matcher.group(group) + " with " + replacementText);
89 replacementBuffer.append(replacementText);
90 } else {
91 replacementBuffer.append(matcher.group(group));
92 }
93 }
94 return replacementBuffer.toString();
95 }
96
97 public String getAdditionalLine() {
98 return null;
99 }
100 }