View Javadoc

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