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 pattern converter.
29   */
30  @Plugin(name = "equals", category = PatternConverter.CATEGORY)
31  @ConverterKeys({ "equals" })
32  @PerformanceSensitive("allocation")
33  public final class EqualsReplacementConverter extends EqualsBaseReplacementConverter {
34  
35      /**
36       * Gets an instance of the class.
37       *
38       * @param config  The current Configuration.
39       * @param options pattern options, an array of three elements: pattern, testString, and substitution.
40       * @return instance of class.
41       */
42      public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) {
43          if (options.length != 3) {
44              LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length);
45              return null;
46          }
47          if (options[0] == null) {
48              LOGGER.error("No pattern supplied on equals");
49              return null;
50          }
51          if (options[1] == null) {
52              LOGGER.error("No test string supplied on equals");
53              return null;
54          }
55          if (options[2] == null) {
56              LOGGER.error("No substitution supplied on equals");
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 EqualsReplacementConverter(formatters, p, options[2], parser);
63      }
64  
65      /**
66       * Construct the converter.
67       *
68       * @param formatters   The PatternFormatters to generate the text to manipulate.
69       * @param testString   The test string.
70       * @param substitution The substitution string.
71       * @param parser        The PatternParser.
72       */
73      private EqualsReplacementConverter(final List<PatternFormatter> formatters, final String testString,
74                                         final String substitution, final PatternParser parser) {
75          super("equals", "equals", formatters, testString, substitution, parser);
76      }
77  
78      @Override
79      protected boolean equals(final String str, final StringBuilder buff, final int from, final int len) {
80          return StringBuilders.equals(str, 0, str.length(), buff, from, len);
81      }
82  }