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  package chapter10.calculator;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import ch.qos.logback.core.Context;
16  import ch.qos.logback.core.ContextBase;
17  import ch.qos.logback.core.joran.action.Action;
18  import ch.qos.logback.core.joran.spi.JoranException;
19  import ch.qos.logback.core.joran.spi.Pattern;
20  import ch.qos.logback.core.util.StatusPrinter;
21  import chapter10.SimpleConfigurator;
22  
23  
24  /**
25   * This examples illustrates collaboration between multiple actions through the
26   * common execution context stack.
27   * 
28   * It differs from Calculator1 in that it supports arbitrary nesting of 
29   * computation elements.
30   * 
31   * You can test this application with the sample XML file <em>calculator3.xml</em>.
32   * 
33   * @author Ceki G&uuml;ulc&uuml;
34   */
35  public class Calculator2 {
36    public static void main(String[] args) throws Exception {
37      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
38     
39      
40      // Note the wild card character '*', in the paterns, signifying any level 
41      // of nesting.
42      ruleMap.put(new Pattern("*/computation"), new ComputationAction2());
43  
44      ruleMap.put(new Pattern("*/computation/literal"), new LiteralAction());
45      ruleMap.put(new Pattern("*/computation/add"), new AddAction());
46      ruleMap.put(new Pattern("*/computation/multiply"), new MultiplyAction());
47      
48      Context context = new ContextBase();
49      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
50      // link the configurator with its context
51      simpleConfigurator.setContext(context);
52  
53      try {
54        simpleConfigurator.doConfigure(args[0]);
55      } catch (JoranException e) {
56        // Print any errors that might have occured.
57        StatusPrinter.print(context);
58      }
59    }
60  }