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