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.util.PerformanceSensitive;
23  
24  /**
25   * Equals pattern converter.
26   */
27  @PerformanceSensitive("allocation")
28  public abstract class EqualsBaseReplacementConverter extends LogEventPatternConverter {
29      private final List<PatternFormatter> formatters;
30      private final List<PatternFormatter> substitutionFormatters;
31      private final String substitution;
32      private final String testString;
33  
34      /**
35       * Construct the converter.
36       *
37       * @param name          converter name
38       * @param style         converter style
39       * @param formatters   The PatternFormatters to generate the text to manipulate.
40       * @param testString   The test string.
41       * @param substitution The substitution string.
42       * @param parser        The PatternParser.
43       */
44      protected EqualsBaseReplacementConverter(final String name, final String style,
45                                              final List<PatternFormatter> formatters, final String testString,
46                                              final String substitution, final PatternParser parser) {
47          super(name, style);
48          this.testString = testString;
49          this.substitution = substitution;
50          this.formatters = formatters;
51  
52          // check if substitution needs to be parsed
53          substitutionFormatters = substitution.contains("%") ? parser.parse(substitution) : null;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public void format(final LogEvent event, final StringBuilder toAppendTo) {
61          final int initialSize = toAppendTo.length();
62          for (int i = 0; i < formatters.size(); i++) {
63              final PatternFormatter formatter = formatters.get(i);
64              formatter.format(event, toAppendTo);
65          }
66          if (equals(testString, toAppendTo, initialSize, toAppendTo.length() - initialSize)) {
67              toAppendTo.setLength(initialSize);
68              parseSubstitution(event, toAppendTo);
69          }
70      }
71  
72      /**
73       * Returns true if the specified String equals the specified section of the specified StringBuilder.
74       *
75       * @param str the String to compare
76       * @param buff the StringBuilder to compare a section of
77       * @param from start index in the StringBuilder
78       * @param len length of the section in the StringBuilder
79       * @return true if equal, false otherwise
80       */
81      protected abstract boolean equals(String str, StringBuilder buff, int from, int len);
82  
83      /**
84       * Adds the parsed substitution text to the specified buffer.
85       *
86       * @param event the current log event
87       * @param substitutionBuffer the StringBuilder to append the parsed substitution text to
88       */
89      void parseSubstitution(final LogEvent event, final StringBuilder substitutionBuffer) {
90          if (substitutionFormatters != null) {
91              for (int i = 0; i < substitutionFormatters.size(); i++) {
92                  final PatternFormatter formatter = substitutionFormatters.get(i);
93                  formatter.format(event, substitutionBuffer);
94              }
95          } else {
96              substitutionBuffer.append(substitution);
97          }
98      }
99  }