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 org.xml.sax.Attributes;
17  
18  import ch.qos.logback.core.joran.spi.InterpretationContext;
19  import ch.qos.logback.core.joran.spi.Pattern;
20  import ch.qos.logback.core.util.OptionHelper;
21  
22  
23  public class NewRuleAction extends Action {
24    boolean inError = false;
25  
26    /**
27     * Instantiates an layout of the given class and sets its name.
28     */
29    public void begin(InterpretationContext ec, String localName, Attributes attributes) {
30      // Let us forget about previous errors (in this object)
31      inError = false;
32      String errorMsg;
33      String pattern = attributes.getValue(Action.PATTERN_ATTRIBUTE);
34      String actionClass = attributes.getValue(Action.ACTION_CLASS_ATTRIBUTE);
35  
36      if (OptionHelper.isEmpty(pattern)) {
37        inError = true;
38        errorMsg = "No 'pattern' attribute in <newRule>";
39        addError(errorMsg);
40        return;
41      }
42  
43      if (OptionHelper.isEmpty(actionClass)) {
44        inError = true;
45        errorMsg = "No 'actionClass' attribute in <newRule>";
46        addError(errorMsg);
47        return;
48      }
49  
50      try {
51        addInfo("About to add new Joran parsing rule [" + pattern + ","
52            + actionClass + "].");
53        ec.getJoranInterpreter().getRuleStore().addRule(new Pattern(pattern),
54            actionClass);
55      } catch (Exception oops) {
56        inError = true;
57        errorMsg = "Could not add new Joran parsing rule [" + pattern + ","
58            + actionClass + "]";
59        addError(errorMsg);
60      }
61    }
62  
63    /**
64     * Once the children elements are also parsed, now is the time to activate the
65     * appender options.
66     */
67    public void end(InterpretationContext ec, String n) {
68    }
69  
70    public void finish(InterpretationContext ec) {
71    }
72  }