View Javadoc

1   /**
2    * Logback: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2006, 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 chapter3.newRule;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.joran.action.Action;
19  import ch.qos.logback.core.joran.action.NewRuleAction;
20  import ch.qos.logback.core.joran.spi.JoranException;
21  import ch.qos.logback.core.joran.spi.Pattern;
22  import ch.qos.logback.core.util.StatusPrinter;
23  import chapter3.SimpleConfigurator;
24  import chapter3.calculator.ComputationAction2;
25  
26  
27  /**
28   * This example illustrates the usage of NewRuleAction which allows the Joran
29   * interpreter to learn new rules on the fly from the XML file being
30   * interpreted.
31   *
32   * This example relies heavily on the code from the joran.calculator package.
33   *
34   * @author Ceki Güulcü
35   */
36  public class NewRuleCalculator {
37    public static void main(String[] args) throws Exception {
38    
39      Context context = new ContextBase();
40    
41      
42      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
43  
44      // we start with the rule for the top-most (root) element
45      ruleMap.put(new Pattern("*/computation"), new ComputationAction2());
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(
52        new Pattern("/computation/new-rule"), new NewRuleAction());
53  
54      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
55      // link the configurator with its context
56      simpleConfigurator.setContext(context);
57  
58      try {
59        simpleConfigurator.doConfigure(args[0]);
60      } catch (JoranException e) {
61        // Print any errors that might have occured.
62        StatusPrinter.print(context);
63      }
64        }
65  }