View Javadoc

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