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.LogEvent;
22  import org.apache.logging.log4j.core.config.Configuration;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.layout.PatternLayout;
25  
26  /**
27   * Equals ignore case pattern converter.
28   */
29  @Plugin(name = "equalsIgnoreCase", category = PatternConverter.CATEGORY)
30  @ConverterKeys({ "equalsIgnoreCase" })
31  public final class EqualsIgnoreCaseReplacementConverter extends LogEventPatternConverter {
32  
33      /**
34       * Gets an instance of the class.
35       *
36       * @param config
37       *            The current Configuration.
38       * @param options
39       *            pattern options, an array of three elements: pattern, testString, and substitution.
40       * @return instance of class.
41       */
42      public static EqualsIgnoreCaseReplacementConverter newInstance(final Configuration config, final String[] options) {
43          if (options.length != 3) {
44              LOGGER.error("Incorrect number of options on equalsIgnoreCase. Expected 3 received " + options.length);
45              return null;
46          }
47          if (options[0] == null) {
48              LOGGER.error("No pattern supplied on equalsIgnoreCase");
49              return null;
50          }
51          if (options[1] == null) {
52              LOGGER.error("No test string supplied on equalsIgnoreCase");
53              return null;
54          }
55          if (options[2] == null) {
56              LOGGER.error("No substitution supplied on equalsIgnoreCase");
57              return null;
58          }
59          final String p = options[1];
60          final PatternParser parser = PatternLayout.createPatternParser(config);
61          final List<PatternFormatter> formatters = parser.parse(options[0]);
62          return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2]);
63      }
64  
65      private final List<PatternFormatter> formatters;
66  
67      private final String substitution;
68  
69      private final String testString;
70  
71      /**
72       * Construct the converter.
73       * 
74       * @param formatters
75       *            The PatternFormatters to generate the text to manipulate.
76       * @param testString
77       *            The test string.
78       * @param substitution
79       *            The substitution string.
80       */
81      private EqualsIgnoreCaseReplacementConverter(final List<PatternFormatter> formatters, final String testString,
82              final String substitution) {
83          super("equals", "equals");
84          this.testString = testString;
85          this.substitution = substitution;
86          this.formatters = formatters;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void format(final LogEvent event, final StringBuilder toAppendTo) {
94          final StringBuilder buf = new StringBuilder();
95          for (final PatternFormatter formatter : formatters) {
96              formatter.format(event, buf);
97          }
98          final String string = buf.toString();
99          toAppendTo.append(testString.equalsIgnoreCase(string) ? substitution : string);
100     }
101 }