View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.joran.action;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.xml.sax.Attributes;
20  
21  import ch.qos.logback.core.CoreConstants;
22  import ch.qos.logback.core.joran.JoranConstants;
23  import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
24  import ch.qos.logback.core.util.OptionHelper;
25  
26  public class ConversionRuleAction extends Action {
27      boolean inError = false;
28  
29      /**
30       * Instantiates a layout of the given class and sets its name.
31       *
32       */
33      @SuppressWarnings("unchecked")
34      public void begin(SaxEventInterpretationContext ec, String localName, Attributes attributes) {
35          // Let us forget about previous errors (in this object)
36          inError = false;
37  
38          String errorMsg;
39          String conversionWord = attributes.getValue(JoranConstants.CONVERSION_WORD_ATTRIBUTE);
40          String converterClass = attributes.getValue(JoranConstants.CONVERTER_CLASS_ATTRIBUTE);
41  
42          if (OptionHelper.isNullOrEmptyOrAllSpaces(conversionWord)) {
43              inError = true;
44              errorMsg = "No 'conversionWord' attribute in <conversionRule>";
45              addError(errorMsg);
46  
47              return;
48          }
49  
50          if (OptionHelper.isNullOrEmptyOrAllSpaces(converterClass)) {
51              inError = true;
52              errorMsg = "No 'converterClass' attribute in <conversionRule>";
53              ec.addError(errorMsg);
54  
55              return;
56          }
57  
58          try {
59              Map<String, String> ruleRegistry = (Map<String, String>) context
60                      .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
61              if (ruleRegistry == null) {
62                  ruleRegistry = new HashMap<String, String>();
63                  context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
64              }
65              // put the new rule into the rule registry
66              addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
67              ruleRegistry.put(conversionWord, converterClass);
68          } catch (Exception oops) {
69              inError = true;
70              errorMsg = "Could not add conversion rule to PatternLayout.";
71              addError(errorMsg);
72          }
73      }
74  
75      /**
76       * Once the children elements are also parsed, now is the time to activate the
77       * appender options.
78       */
79      public void end(SaxEventInterpretationContext ec, String n) {
80      }
81  
82      public void finish(SaxEventInterpretationContext ec) {
83      }
84  }