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.Arrays;
28 import java.util.Iterator;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32
33 public class LineConverter {
34
35 final RuleSet ruleSet;
36 boolean atLeastOneMatchOccured = false;
37
38 public LineConverter(RuleSet ruleSet) {
39 this.ruleSet = ruleSet;
40 }
41
42 /**
43 * Check if the specified text is matching some conversions rules.
44 * If a rule matches, ask for line replacement.
45 *
46 * <p>In case no rule can be applied, then the input text is
47 * returned without change.
48 *
49 * @param text
50 * @return String
51 */
52 public String[] getReplacement(String text) {
53 ConversionRule conversionRule;
54 Pattern pattern;
55 Matcher matcher;
56 Iterator<ConversionRule> conversionRuleIterator = ruleSet.iterator();
57 String additionalLine = null;
58 while (conversionRuleIterator.hasNext()) {
59 conversionRule = conversionRuleIterator.next();
60 pattern = conversionRule.getPattern();
61 matcher = pattern.matcher(text);
62 if (matcher.find()) {
63 // System.out.println("matching " + text);
64 atLeastOneMatchOccured = true;
65 String replacementText = conversionRule.replace(matcher);
66 text = matcher.replaceAll(replacementText);
67 if(conversionRule.getAdditionalLine() != null) {
68 additionalLine = conversionRule.getAdditionalLine();
69 }
70 }
71 }
72
73 if(additionalLine == null) {
74 return new String[] {text};
75 } else {
76 return new String[] {text, additionalLine};
77 }
78 }
79
80 public String getOneLineReplacement(String text) {
81 String[] r = getReplacement(text);
82 if(r.length != 1) {
83 throw new IllegalStateException("Expecting a single string but got "+Arrays.toString(r));
84 } else {
85 return r[0];
86 }
87 }
88 public boolean atLeastOneMatchOccured() {
89 return atLeastOneMatchOccured;
90 }
91 }