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.newRule;
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.action.NewRuleAction;
023import ch.qos.logback.core.joran.spi.ElementSelector;
024import ch.qos.logback.core.util.StatusPrinter;
025import chapters.onJoran.SimpleConfigurator;
026import chapters.onJoran.calculator.ComputationAction1;
027
028/**
029 * This example illustrates the usage of NewRuleAction which allows the Joran
030 * interpreter to learn new rules on the fly.
031 * 
032 * <p>This example relies heavily on the code from the joran.calculator
033 * package.
034 * 
035 * @author Ceki G&uuml;lc&uuml;
036 */
037public class NewRuleCalculator {
038    public static void main(String[] args) throws Exception {
039
040        Context context = new ContextBase();
041
042        Map<ElementSelector, Action> ruleMap = new HashMap<ElementSelector, Action>();
043
044        // we start with the rule for the top-most (root) element
045        ruleMap.put(new ElementSelector("*/computation"), new ComputationAction1());
046
047        // Associate "/new-rule" pattern with NewRuleAction from the
048        // org.apache.joran.action package.
049        //
050        // We will let the XML file to teach the Joran interpreter about new rules
051        ruleMap.put(new ElementSelector("/computation/newRule"), new NewRuleAction());
052
053        SimpleConfigurator simpleConfigurator = new SimpleConfigurator(ruleMap);
054        // link the configurator with its context
055        simpleConfigurator.setContext(context);
056
057        simpleConfigurator.doConfigure(args[0]);
058
059        // Print any errors that might have occured.
060        StatusPrinter.printInCaseOfErrorsOrWarnings(context);
061    }
062
063}