View Javadoc

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