01: /*
02: * Copyright (c) 2004-2007 QOS.ch
03: * All rights reserved.
04: *
05: * Permission is hereby granted, free of charge, to any person obtaining
06: * a copy of this software and associated documentation files (the
07: * "Software"), to deal in the Software without restriction, including
08: * without limitation the rights to use, copy, modify, merge, publish,
09: * 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 Pattern pattern;
44: private String[] replacementTable = new String[MAX_GROUPS];
45:
46: public MultiGroupConversionRule(Pattern pattern) {
47: this .pattern = pattern;
48: }
49:
50: /* (non-Javadoc)
51: * @see org.slf4j.converter.ConversionRule#getPattern()
52: */
53: public Pattern getPattern() {
54: return pattern;
55: }
56:
57: public void addReplacement(int groupIndex, String replacement) {
58: if (groupIndex == 0) {
59: throw new IllegalArgumentException(
60: "regex groups start at 1, not zero");
61: }
62: replacementTable[groupIndex] = replacement;
63: }
64:
65: /* (non-Javadoc)
66: * @see org.slf4j.converter.ConversionRule#getReplacement(java.lang.Integer)
67: */
68: public String getReplacement(int groupIndex) {
69: return replacementTable[groupIndex];
70: }
71:
72: /* (non-Javadoc)
73: * @see org.slf4j.converter.ConversionRule#replace(java.util.regex.Matcher)
74: */
75: public String replace(Matcher matcher) {
76: StringBuffer replacementBuffer = new StringBuffer();
77: String replacementText;
78:
79: for (int group = 1; group <= matcher.groupCount(); group++) {
80: replacementText = getReplacement(group);
81: if (replacementText != null) {
82: //System.out.println("replacing group " + group + " : "
83: // + matcher.group(group) + " with " + replacementText);
84: replacementBuffer.append(replacementText);
85: } else {
86: replacementBuffer.append(matcher.group(group));
87: }
88: }
89: return replacementBuffer.toString();
90: }
91:
92: public String getAdditionalLine() {
93: return null;
94: }
95: }
|