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.spi.InterpretationContext;
23  import ch.qos.logback.core.util.OptionHelper;
24  
25  public class ConversionRuleAction extends Action {
26      boolean inError = false;
27  
28      /**
29       * Instantiates an layout of the given class and sets its name.
30       *
31       */
32      @SuppressWarnings("unchecked")
33      public void begin(InterpretationContext ec, String localName, Attributes attributes) {
34          // Let us forget about previous errors (in this object)
35          inError = false;
36  
37          String errorMsg;
38          String conversionWord = attributes.getValue(ActionConst.CONVERSION_WORD_ATTRIBUTE);
39          String converterClass = attributes.getValue(ActionConst.CONVERTER_CLASS_ATTRIBUTE);
40  
41          if (OptionHelper.isEmpty(conversionWord)) {
42              inError = true;
43              errorMsg = "No 'conversionWord' attribute in <conversionRule>";
44              addError(errorMsg);
45  
46              return;
47          }
48  
49          if (OptionHelper.isEmpty(converterClass)) {
50              inError = true;
51              errorMsg = "No 'converterClass' attribute in <conversionRule>";
52              ec.addError(errorMsg);
53  
54              return;
55          }
56  
57          try {
58              Map<String, String> ruleRegistry = (Map<String, String>) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
59              if (ruleRegistry == null) {
60                  ruleRegistry = new HashMap<String, String>();
61                  context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry);
62              }
63              // put the new rule into the rule registry
64              addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
65              ruleRegistry.put(conversionWord, converterClass);
66          } catch (Exception oops) {
67              inError = true;
68              errorMsg = "Could not add conversion rule to PatternLayout.";
69              addError(errorMsg);
70          }
71      }
72  
73      /**
74       * Once the children elements are also parsed, now is the time to activate
75       * the appender options.
76       */
77      public void end(InterpretationContext ec, String n) {
78      }
79  
80      public void finish(InterpretationContext ec) {
81      }
82  }