View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.pattern;
18  
19  import java.util.List;
20  
21  import org.apache.logging.log4j.core.config.Configuration;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.layout.PatternLayout;
24  import org.apache.logging.log4j.util.PerformanceSensitive;
25  import org.apache.logging.log4j.util.StringBuilders;
26  
27  /**
28   * Equals ignore case pattern converter.
29   */
30  @Plugin(name = "equalsIgnoreCase", category = PatternConverter.CATEGORY)
31  @ConverterKeys({ "equalsIgnoreCase" })
32  @PerformanceSensitive("allocation")
33  public final class EqualsIgnoreCaseReplacementConverter extends EqualsBaseReplacementConverter {
34  
35      /**
36       * Gets an instance of the class.
37       *
38       * @param config
39       *            The current Configuration.
40       * @param options
41       *            pattern options, an array of three elements: pattern, testString, and substitution.
42       * @return instance of class.
43       */
44      public static EqualsIgnoreCaseReplacementConverter newInstance(final Configuration config, final String[] options) {
45          if (options.length != 3) {
46              LOGGER.error("Incorrect number of options on equalsIgnoreCase. Expected 3 received " + options.length);
47              return null;
48          }
49          if (options[0] == null) {
50              LOGGER.error("No pattern supplied on equalsIgnoreCase");
51              return null;
52          }
53          if (options[1] == null) {
54              LOGGER.error("No test string supplied on equalsIgnoreCase");
55              return null;
56          }
57          if (options[2] == null) {
58              LOGGER.error("No substitution supplied on equalsIgnoreCase");
59              return null;
60          }
61          final String p = options[1];
62          final PatternParser parser = PatternLayout.createPatternParser(config);
63          final List<PatternFormatter> formatters = parser.parse(options[0]);
64          return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2], parser);
65      }
66  
67      /**
68       * Construct the converter.
69       *
70       * @param formatters   The PatternFormatters to generate the text to manipulate.
71       * @param testString   The test string.
72       * @param substitution The substitution string.
73       * @param parser        The PatternParser.
74       */
75      private EqualsIgnoreCaseReplacementConverter(final List<PatternFormatter> formatters, final String testString,
76              final String substitution, final PatternParser parser) {
77          super("equalsIgnoreCase", "equalsIgnoreCase", formatters, testString, substitution, parser);
78      }
79  
80      @Override
81      protected boolean equals(final String str, final StringBuilder buff, final int from, final int len) {
82          return StringBuilders.equalsIgnoreCase(str, 0, str.length(), buff, from, len);
83      }
84  }