LineConverter xref

View Javadoc

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