001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.migrator.line;
026
027import java.util.Arrays;
028import java.util.Iterator;
029import java.util.regex.Matcher;
030import java.util.regex.Pattern;
031
032public class LineConverter {
033
034    final RuleSet ruleSet;
035    boolean atLeastOneMatchOccured = false;
036
037    public LineConverter(RuleSet ruleSet) {
038        this.ruleSet = ruleSet;
039    }
040
041    /**
042     * Check if the specified text is matching some conversions rules. 
043     * If a rule matches, ask for line replacement.
044     * 
045     * <p>In case no rule can be applied, then the input text is
046     * returned without change.
047     * 
048     * @param text
049     * @return String
050     */
051    public String[] getReplacement(String text) {
052        ConversionRule conversionRule;
053        Pattern pattern;
054        Matcher matcher;
055        Iterator<ConversionRule> conversionRuleIterator = ruleSet.iterator();
056        String additionalLine = null;
057        while (conversionRuleIterator.hasNext()) {
058            conversionRule = conversionRuleIterator.next();
059            pattern = conversionRule.getPattern();
060            matcher = pattern.matcher(text);
061            if (matcher.find()) {
062                // System.out.println("matching " + text);
063                atLeastOneMatchOccured = true;
064                String replacementText = conversionRule.replace(matcher);
065                text = matcher.replaceAll(replacementText);
066                if (conversionRule.getAdditionalLine() != null) {
067                    additionalLine = conversionRule.getAdditionalLine();
068                }
069            }
070        }
071
072        if (additionalLine == null) {
073            return new String[] { text };
074        } else {
075            return new String[] { text, additionalLine };
076        }
077    }
078
079    public String getOneLineReplacement(String text) {
080        String[] r = getReplacement(text);
081        if (r.length != 1) {
082            throw new IllegalStateException("Expecting a single string but got " + Arrays.toString(r));
083        } else {
084            return r[0];
085        }
086    }
087
088    public boolean atLeastOneMatchOccured() {
089        return atLeastOneMatchOccured;
090    }
091}