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   * Returns the event's rendered message in a StringBuilder.
28   */
29  @Plugin(name = "encode", category = PatternConverter.CATEGORY)
30  @ConverterKeys({ "enc", "encode" })
31  public final class EncodingPatternConverter extends LogEventPatternConverter {
32  
33      private final List<PatternFormatter> formatters;
34  
35      /**
36       * Private constructor.
37       *
38       * @param formatters The PatternFormatters to generate the text to manipulate.
39       */
40      private EncodingPatternConverter(final List<PatternFormatter> formatters) {
41          super("encode", "encode");
42          this.formatters = formatters;
43      }
44  
45      /**
46       * Obtains an instance of pattern converter.
47       *
48       * @param config  The Configuration.
49       * @param options options, may be null.
50       * @return instance of pattern converter.
51       */
52      public static EncodingPatternConverter newInstance(final Configuration config, final String[] options) {
53          if (options.length != 1) {
54              LOGGER.error("Incorrect number of options on escape. Expected 1, received " + options.length);
55              return null;
56          }
57          if (options[0] == null) {
58              LOGGER.error("No pattern supplied on escape");
59              return null;
60          }
61          final PatternParser parser = PatternLayout.createPatternParser(config);
62          final List<PatternFormatter> formatters = parser.parse(options[0]);
63          return new EncodingPatternConverter(formatters);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public void format(final LogEvent event, final StringBuilder toAppendTo) {
71          final StringBuilder buf = new StringBuilder();
72          for (final PatternFormatter formatter : formatters) {
73              formatter.format(event, buf);
74          }
75          for (int i = 0; i < buf.length(); i++) {
76              final char c = buf.charAt(i);
77              switch (c) {
78                  case '\r':
79                      toAppendTo.append("\\r");
80                      break;
81                  case '\n':
82                      toAppendTo.append("\\n");
83                      break;
84                  case '&':
85                      toAppendTo.append("&amp;");
86                      break;
87                  case '<':
88                      toAppendTo.append("&lt;");
89                      break;
90                  case '>':
91                      toAppendTo.append("&gt;");
92                      break;
93                  case '"':
94                      toAppendTo.append("&quot;");
95                      break;
96                  case '\'':
97                      toAppendTo.append("&apos;");
98                      break;
99                  case '/':
100                     toAppendTo.append("&#x2F;");
101                     break;
102                 default:
103                     toAppendTo.append(c);
104                     break;
105             }
106         }
107     }
108 }