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