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 chapters.onJoran.newRule;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import ch.qos.logback.core.Context;
20  import ch.qos.logback.core.ContextBase;
21  import ch.qos.logback.core.joran.action.Action;
22  import ch.qos.logback.core.joran.action.NewRuleAction;
23  import ch.qos.logback.core.joran.spi.ElementSelector;
24  import ch.qos.logback.core.util.StatusPrinter;
25  import chapters.onJoran.SimpleConfigurator;
26  import chapters.onJoran.calculator.ComputationAction1;
27  
28  /**
29   * This example illustrates the usage of NewRuleAction which allows the Joran
30   * interpreter to learn new rules on the fly.
31   * 
32   * <p>This example relies heavily on the code from the joran.calculator
33   * package.
34   * 
35   * @author Ceki G&uuml;lc&uuml;
36   */
37  public class NewRuleCalculator {
38      public static void main(String[] args) throws Exception {
39  
40          Context context = new ContextBase();
41  
42          Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();
43  
44          // we start with the rule for the top-most (root) element
45          ruleMap.put(new ElementSelector("*/computation"), new ComputationAction1());
46  
47          // Associate "/new-rule" pattern with NewRuleAction from the
48          // org.apache.joran.action package.
49          //
50          // We will let the XML file to teach the Joran interpreter about new rules
51          ruleMap.put(new ElementSelector("/computation/newRule"), new NewRuleAction());
52  
53          SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
54          // link the configurator with its context
55          simpleConfigurator.setContext(context);
56  
57          simpleConfigurator.doConfigure(args[0]);
58  
59          // Print any errors that might have occured.
60          StatusPrinter.printInCaseOfErrorsOrWarnings(context);
61      }
62  
63  }