MultiGroupConversionRule 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.regex.Matcher;
    28  import java.util.regex.Pattern;
    29
    30
    31  /**
    32   * This class represents a conversion rule It uses a Pattern and defines for
    33   * each capturing group of this Pattern a replacement text
    34   *
    35   * @author jean-noelcharpin
    36   *
    37   */
    38  public class MultiGroupConversionRule implements ConversionRule {
    39
    40    // It is extremely unlikely to encounter more than 10 groups in one of
    41    // our conversion reg-expressions
    42    final private static int MAX_GROUPS = 10;
    43
    44    private Pattern pattern;
    45    private String[] replacementTable = new String[MAX_GROUPS];
    46
    47    public MultiGroupConversionRule(Pattern pattern) {
    48      this.pattern = pattern;
    49    }
    50
    51    /* (non-Javadoc)
    52     * @see org.slf4j.converter.ConversionRule#getPattern()
    53     */
    54    public Pattern getPattern() {
    55      return pattern;
    56    }
    57
    58    public void addReplacement(int groupIndex, String replacement) {
    59      if(groupIndex == 0) {
    60        throw new IllegalArgumentException("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  }