View Javadoc

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