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.calculator;
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.spi.JoranException;
20  import ch.qos.logback.core.joran.spi.Pattern;
21  import ch.qos.logback.core.util.StatusPrinter;
22  import chapter3.SimpleConfigurator;
23  
24  /**
25   * This examples illustrates collaboration between multiple actions through the
26   * common execution context stack.
27   * 
28   * The first and only argument of this application must be the path to the XML
29   * file to interpret. There are sample XML files in the
30   * <em>examples/src/joran/calculator/</em> directory.
31   * 
32   * For example,
33   * 
34   * <pre>
35   *  java joran.calculator.Calculator1 examples/src/joran/calculator/calculator1.xml
36   * </pre>
37   * 
38   * Please refer to the comments in the source code for more information.
39   * 
40   * @author Ceki G&uuml;ulc&uuml;
41   */
42  public class Calculator1 {
43  
44    public static void main(String[] args) throws Exception {
45      Context context = new ContextBase();
46  
47      Map<Pattern, Action> ruleMap = new HashMap<Pattern, Action>();
48  
49      // Associate "/computation" pattern with ComputationAction1
50      ruleMap.put(new Pattern("/computation"), new ComputationAction1());
51  
52      // Other associations
53      ruleMap.put(new Pattern("/computation/literal"), new LiteralAction());
54      ruleMap.put(new Pattern("/computation/add"), new AddAction());
55      ruleMap.put(new Pattern("/computation/multiply"), new MultiplyAction());
56  
57      SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
58      // link the configurator with its context
59      simpleConfigurator.setContext(context);
60  
61      try {
62        simpleConfigurator.doConfigure(args[0]);
63      } catch (JoranException e) {
64        // Print any errors that might have occured.
65        StatusPrinter.print(context);
66      }
67    }
68  }