001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package chapters.onJoran.calculator;
015
016import java.util.HashMap;
017import java.util.Map;
018
019import ch.qos.logback.core.Context;
020import ch.qos.logback.core.ContextBase;
021import ch.qos.logback.core.joran.action.Action;
022import ch.qos.logback.core.joran.spi.ElementSelector;
023import ch.qos.logback.core.joran.spi.JoranException;
024import ch.qos.logback.core.util.StatusPrinter;
025import chapters.onJoran.SimpleConfigurator;
026
027/**
028 * This examples illustrates collaboration between multiple actions through the
029 * common execution context stack.
030 * 
031 * It differs from Calculator1 in that it supports arbitrary nesting of 
032 * computation elements.
033 * 
034 * You can test this application with the sample XML file <em>calculator3.xml</em>.
035 * 
036 * @author Ceki G&uuml;lc&uuml;
037 */
038public class Calculator2 {
039    public static void main(String[] args) throws Exception {
040        Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();
041
042        // Note the wild card character '*', in the paterns, signifying any level
043        // of nesting.
044        ruleMap.put(new ElementSelector("*/computation"), new ComputationAction2());
045
046        ruleMap.put(new ElementSelector("*/computation/literal"), new LiteralAction());
047        ruleMap.put(new ElementSelector("*/computation/add"), new AddAction());
048        ruleMap.put(new ElementSelector("*/computation/multiply"), new MultiplyAction());
049
050        Context context = new ContextBase();
051        SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
052        // link the configurator with its context
053        simpleConfigurator.setContext(context);
054
055        try {
056            simpleConfigurator.doConfigure(args[0]);
057        } catch (JoranException e) {
058            // Print any errors that might have occured.
059            StatusPrinter.print(context);
060        }
061    }
062}