[logback-dev] svn commit: r633 - in logback/trunk/logback-core/examples/src/joran: . calculator helloWorld implicit newRule

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sun Oct 8 18:14:14 CEST 2006


Author: ceki
Date: Sun Oct  8 18:14:14 2006
New Revision: 633

Added:
   logback/trunk/logback-core/examples/src/joran/
   logback/trunk/logback-core/examples/src/joran/calculator/
   logback/trunk/logback-core/examples/src/joran/calculator/AddAction.java
   logback/trunk/logback-core/examples/src/joran/calculator/Calculator1.java
   logback/trunk/logback-core/examples/src/joran/calculator/Calculator2.java
   logback/trunk/logback-core/examples/src/joran/calculator/ComputationAction1.java
   logback/trunk/logback-core/examples/src/joran/calculator/ComputationAction2.java
   logback/trunk/logback-core/examples/src/joran/calculator/LiteralAction.java
   logback/trunk/logback-core/examples/src/joran/calculator/MultiplyAction.java
   logback/trunk/logback-core/examples/src/joran/calculator/calculator1.xml
   logback/trunk/logback-core/examples/src/joran/calculator/calculator2.xml
   logback/trunk/logback-core/examples/src/joran/calculator/calculator3.xml
   logback/trunk/logback-core/examples/src/joran/helloWorld/
   logback/trunk/logback-core/examples/src/joran/helloWorld/HelloWorld.java
   logback/trunk/logback-core/examples/src/joran/helloWorld/HelloWorldAction.java
   logback/trunk/logback-core/examples/src/joran/helloWorld/hello.xml
   logback/trunk/logback-core/examples/src/joran/implicit/
   logback/trunk/logback-core/examples/src/joran/implicit/NOPAction.java
   logback/trunk/logback-core/examples/src/joran/implicit/PrintMe.java
   logback/trunk/logback-core/examples/src/joran/implicit/PrintMeImplicitAction.java
   logback/trunk/logback-core/examples/src/joran/implicit/implicit1.xml
   logback/trunk/logback-core/examples/src/joran/newRule/
   logback/trunk/logback-core/examples/src/joran/newRule/NewRuleCalculator.java

Log:
Added joran tutorial

Added: logback/trunk/logback-core/examples/src/joran/calculator/AddAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/AddAction.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,71 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.calculator;
+
+
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+
+import java.util.EmptyStackException;
+
+
+/**
+ * This action adds the two integers at the top of the stack (they are removed)
+ * and pushes the result to the top the stack.  
+ * 
+ * @author Ceki Gülcü
+ */
+public class AddAction extends Action {
+  
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    int first = fetchInteger(ec);
+    int second = fetchInteger(ec);
+    // Push the result of the addition for the following actions.
+    ec.pushObject(new Integer(first + second));
+  }
+
+  /**
+   * Pop the Integer object at the top of the stack.
+   * This code illustrates usage of Joran's error handling paradigm. 
+   */
+  int fetchInteger(ExecutionContext ec) {
+    int result = 0;
+
+    try {
+      // Pop the object at the top of the exection context stack.
+      Object o1 = ec.popObject();
+
+      if (o1 instanceof Integer) {
+        result = ((Integer) o1).intValue();
+      } else {
+        String errMsg =
+          "Object [" + o1
+          + "] currently at the top of the stack is not an integer.";
+        ec.addError(errMsg);
+        throw new IllegalArgumentException(errMsg);
+      }
+    } catch (EmptyStackException ese) {
+      ec.addError(("Expecting an integer on the execution stack."));
+      throw ese;
+    }
+    return result;
+  }
+
+  public void end(ExecutionContext ec, String name) {
+    // Nothing to do here.
+    // In general, the end() method of actions associated with elements
+    // having no children do not need to perform any processing in their
+    // end() method.
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/Calculator1.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/Calculator1.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,76 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.calculator;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.joran.spi.Interpreter;
+import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.joran.spi.RuleStore;
+import ch.qos.logback.core.joran.spi.SimpleRuleStore;
+import ch.qos.logback.core.util.StatusPrinter;
+
+
+/**
+ * This examples illustrates collaboration between multiple actions through the
+ * common execution context stack.
+ * 
+ * The first and only argument of this application must be the path to
+ * the XML file to interpret. There are sample XML files in the 
+ * <em>examples/src/joran/calculator/</em> directory. 
+ *
+ * For example,
+ *
+<pre>
+    java joran.calculator.Calculator1 examples/src/joran/calculator/calculator1.xml
+</pre>
+ *
+ * Please refer to the comments in the source code for more information.
+ * 
+ * @author Ceki G&uuml;ulc&uuml;
+ */
+public class Calculator1 {
+  
+  
+  public static void main(String[] args) throws Exception {
+    Context context = new ContextBase();
+    
+    // Create a simple rule store where pattern and action associations will
+    // be kept. This is a basic requirement before invoking a Joran Interpreter.
+    RuleStore ruleStore = new SimpleRuleStore(context);
+
+    // Associate "/computation" pattern with  ComputationAction1
+    ruleStore.addRule(new Pattern("/computation"), new ComputationAction1());
+
+    // Other associations
+    ruleStore.addRule(new Pattern("/computation/literal"), new LiteralAction());
+    ruleStore.addRule(new Pattern("/computation/add"), new AddAction());
+    ruleStore.addRule(new Pattern("/computation/multiply"), new MultiplyAction());
+    
+    // Create a new Joran Interpreter and hand it our simple rule store.
+    Interpreter ji = new Interpreter(ruleStore);
+
+    // Create a SAX parser
+    SAXParserFactory spf = SAXParserFactory.newInstance();
+    SAXParser saxParser = spf.newSAXParser();
+
+    // Parse the file given as the application's first argument and
+    // set the SAX ContentHandler to the Joran Interpreter we just created.
+    saxParser.parse(args[0], ji);
+
+    // The file has been parsed and interpreted. We now print any errors that 
+    // might have occured.
+    StatusPrinter.print(context);
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/Calculator2.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/Calculator2.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,65 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package joran.calculator;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.joran.spi.Interpreter;
+import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.joran.spi.RuleStore;
+import ch.qos.logback.core.joran.spi.SimpleRuleStore;
+import ch.qos.logback.core.util.StatusPrinter;
+
+
+/**
+ * This examples illustrates collaboration between multiple actions through the
+ * common execution context stack.
+ * 
+ * It differs from Calculator1 in that it supoorts arbitrary nesting of 
+ * computation elements.
+ * 
+ * You can test this application with the sample XML file <em>calculator3.xml</em>.
+ * 
+ * @author Ceki G&uuml;ulc&uuml;
+ */
+public class Calculator2 {
+  public static void main(String[] args) throws Exception {
+    Context context = new ContextBase();
+    RuleStore ruleStore = new SimpleRuleStore(context);
+   
+    
+    // Note the wild card character '*', in the paterns, signifying any level 
+    // of nesting.
+    ruleStore.addRule(new Pattern("*/computation"), new ComputationAction2());
+
+    ruleStore.addRule(new Pattern("*/computation/literal"), new LiteralAction());
+    ruleStore.addRule(new Pattern("*/computation/add"), new AddAction());
+    ruleStore.addRule(new Pattern("*/computation/multiply"), new MultiplyAction());
+    
+    // Create a new Joran Interpreter and hand it our simple rule store.
+    Interpreter ji = new Interpreter(ruleStore);
+
+    // Create a SAX parser
+    SAXParserFactory spf = SAXParserFactory.newInstance();
+    SAXParser saxParser = spf.newSAXParser();
+
+    // Parse the file given as the application's first argument and
+    // set the SAX ContentHandler to the Joran Interpreter we just created.
+    saxParser.parse(args[0], ji);
+    
+    // The file has been parsed and interpreted. We now print any errors that 
+    // might have occured.
+    StatusPrinter.print(context);
+    
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/ComputationAction1.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/ComputationAction1.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,58 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.calculator;
+
+
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+import ch.qos.logback.core.util.OptionHelper;
+
+
+/**
+ * ComputationAction1 will print the result of the compuration made by 
+ * children elements but only if the compuration itself is named, that is if the
+ * name attribute of the associated computation element is not null. In other
+ * words, anonymous computations will not print their result.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class ComputationAction1 extends Action {
+  public static String NAME_ATR = "name";
+
+  String nameStr;
+
+  /**
+   * Store the value of the name attribute for future use.
+   */
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    nameStr = attributes.getValue(NAME_ATR);
+  }
+
+  /**
+   * Children elements have been processed. The sesults should be an integer 
+   * placed at the top of the execution stack.
+   * 
+   * This value will be printed on the console but only if the action is 
+   * named. Anonymous computation will not print their result.
+   */
+  public void end(ExecutionContext ec, String name) {
+    if (OptionHelper.isEmpty(nameStr)) {
+      // nothing to do
+    } else {
+      Integer i = (Integer) ec.peekObject();
+      System.out.println(
+        "The computation named [" + nameStr + "] resulted in the value " + i);
+    }
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/ComputationAction2.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/ComputationAction2.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,83 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.calculator;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+import ch.qos.logback.core.util.OptionHelper;
+
+
+/**
+ * ComputationAction2 will print the result of the compuration made by 
+ * children elements but only if the compuration itself is named, that is if the
+ * name attribute of the associated computation element is not null. In other
+ * words, anonymous computations will not print their result.
+ * 
+ * ComputationAction2 differs from ComputationAction1 in its handling of
+ * instance variables. ComputationAction1 has a simple <Code>nameStr</code>
+ * instance variable. This variable is set when the begin() method is called 
+ * and then later used within the end() method. 
+ * 
+ * This simple approach works properly if the begin() and end()
+ * method of a given action are expected to be called in sequence. However,
+ * there are situations where the begin() method of the same action instance is 
+ * invoked multiple times before the matching end() method is invoked. 
+ * 
+ * When this happens, the second call to begin() overwrites values set by
+ * the first invocation to begin(). The solution is to save parameter values 
+ * into a separate stack. The well-formedness of XML will guarantee that a value
+ * saved by one begin() will be consumed only by the matching end() method.
+ * 
+ * Note that in the vast majority of cases there is no need to resort to a 
+ * separate stack for each variable. The situation of successibe begin() 
+ * invocations can only occur if: 
+ * 
+ * 1) the associated pattern contains a wildcard, i.e. the &#42; character
+ * 
+ * and
+ * 
+ * 2) the associated element tag can contain itself as a child 
+ *  
+ * For example, "&#42;/computation" pattern means that computations can contain
+ * other computation elements as children. 
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class ComputationAction2 extends Action {
+  public static String NAME_ATR = "name";
+
+  Stack nameStrStack = new Stack();
+  
+  
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    String nameStr = attributes.getValue(NAME_ATR);
+    // save nameStr value in a special stack. Note that the value is saved
+    // even if it is empty or null.
+    nameStrStack.push(nameStr);
+  }
+
+  public void end(ExecutionContext ec, String name) {
+    // pop nameStr value from the special stack
+    String nameStr = (String) nameStrStack.pop();
+    
+    if (OptionHelper.isEmpty(nameStr)) {
+      // nothing to do
+    } else {
+      Integer i = (Integer) ec.peekObject();
+      System.out.println(
+        "The computation named [" + nameStr + "] resulted in the value " + i);
+    }
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/LiteralAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/LiteralAction.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,56 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.calculator;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+import ch.qos.logback.core.util.OptionHelper;
+
+/**
+ *
+ * This action converts the value attribute of the associated element to
+ * an integer and pushes the resulting Integer object on top of the execution
+ * context stack.
+ *
+ * It also illustrates usage of Joran's error handling paradigm.
+ *
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class LiteralAction extends Action {
+  public static String VALUE_ATR = "value";
+
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    String valueStr = attributes.getValue(VALUE_ATR);
+
+    if (OptionHelper.isEmpty(valueStr)) {
+      ec.addError("The literal action requires a value attribute");
+      return;
+    }
+
+    try {
+      Integer i = Integer.valueOf(valueStr);
+      ec.pushObject(i);
+    } catch (NumberFormatException nfe) {
+      ec.addError("The value [" + valueStr + "] could not be converted to an Integer",
+          nfe);
+      throw nfe;
+    }
+  }
+
+  public void end(ExecutionContext ec, String name) {
+    // Nothing to do here.
+    // In general, the end() method of actions associated with elements
+    // having no children do not need to perform any processing in their
+    // end() method.
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/MultiplyAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/MultiplyAction.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,70 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.calculator;
+
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+
+import java.util.EmptyStackException;
+
+
+/**
+ *
+ * This action multiplies the two integers at the top of the stack (they are removed)
+ * and pushes the result on top the stack.
+ *
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class MultiplyAction extends Action {
+  
+  
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    int first = fetchInteger(ec);
+    int second = fetchInteger(ec);
+    ec.pushObject(new Integer(first * second));
+  }
+
+  /**
+   * Pop the Integer object at the top of the stack.
+   * This code illustrates usage of Joran's error handling paradigm.
+   */
+  int fetchInteger(ExecutionContext ec) {
+    int result = 0;
+
+    try {
+      Object o1 = ec.popObject();
+
+      if (o1 instanceof Integer) {
+        result = ((Integer) o1).intValue();
+      } else {
+        String errMsg =
+          "Object [" + o1
+          + "] currently at the top of the stack is not an integer.";
+        ec.addError(errMsg);
+        throw new IllegalArgumentException(errMsg);
+      }
+    } catch (EmptyStackException ese) {
+      ec.addError("Expecting an integer on the execution stack.");
+      throw ese;
+    }
+    return result;
+  }
+
+  public void end(ExecutionContext ec, String name) {
+    // Nothing to do here.
+    // In general, the end() method of actions associated with elements
+    // having no children do not need to perform any processing in their
+    // end() method.
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/calculator/calculator1.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/calculator1.xml	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE computation>
+
+<computation name="total">
+  <literal value="3"/>
+</computation>

Added: logback/trunk/logback-core/examples/src/joran/calculator/calculator2.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/calculator2.xml	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE computation>
+
+<computation name="toto">
+  <literal value="7"/>
+  <literal value="3"/>
+  <add/>
+  <literal value="3"/>
+  <multiply/>
+</computation>
\ No newline at end of file

Added: logback/trunk/logback-core/examples/src/joran/calculator/calculator3.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/calculator/calculator3.xml	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE computation>
+
+<!-- This file is intended to be executed by Caculator2. 
+     It is not suited for Calculator1 due to nested computation 
+     elements.
+     -->
+
+<computation name="toto">
+  <computation>
+    <literal value="7"/>
+    <literal value="3"/>
+    <add/>
+  </computation>   
+ 
+  <literal value="3"/>
+  <multiply/>
+</computation>
\ No newline at end of file

Added: logback/trunk/logback-core/examples/src/joran/helloWorld/HelloWorld.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/helloWorld/HelloWorld.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,50 @@
+
+package joran.helloWorld;
+
+
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import ch.qos.logback.core.joran.spi.Interpreter;
+import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.joran.spi.RuleStore;
+import ch.qos.logback.core.joran.spi.SimpleRuleStore;
+
+
+/**
+ *
+ * A hello world example using Joran.
+ *
+ * The first and only argument of this application must be the path to
+ * the XML file to interpret.
+ *
+ * For example,
+ *
+<pre>
+    java joran.helloWorld.HelloWorld examples/src/joran/helloWorld/hello.xml
+</pre>
+ *
+ * @author Ceki
+ */
+public class HelloWorld {
+  public static void main(String[] args) throws Exception {
+    // Create a simple rule store where pattern and action associations will
+    // be kept.
+    RuleStore ruleStore = new SimpleRuleStore(null);
+
+    // Associate "hello-world" pattern with  HelloWorldAction
+    ruleStore.addRule(new Pattern("hello-world"), new HelloWorldAction());
+
+    // Create a new Joran Interpreter and hand it our simple rule store.
+    Interpreter ji = new Interpreter(ruleStore);
+
+    // Create a SAX parser
+    SAXParserFactory spf = SAXParserFactory.newInstance();
+    SAXParser saxParser = spf.newSAXParser();
+
+    // Parse the file given as the application's first argument and
+    // set the SAX ContentHandler to the Joran Interpreter we just created.
+    saxParser.parse(args[0], ji);
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/helloWorld/HelloWorldAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/helloWorld/HelloWorldAction.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,26 @@
+
+package joran.helloWorld;
+
+
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+
+
+/**
+ * A trivial action that writes "Hello world" on the console.
+ * 
+ * See the HelloWorld class for integrating with Joran.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class HelloWorldAction extends Action {
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    System.out.println("Hello World");
+  }
+
+  public void end(ExecutionContext ec, String name) {
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/helloWorld/hello.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/helloWorld/hello.xml	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE hello-world>
+
+<hello-world>
+</hello-world>

Added: logback/trunk/logback-core/examples/src/joran/implicit/NOPAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/implicit/NOPAction.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,32 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package joran.implicit;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+
+
+
+/**
+ * No operation (NOP) action that does strictly nothing. 
+ *  
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class NOPAction extends Action {
+  
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+  }
+
+
+  public void end(ExecutionContext ec, String name) {
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/implicit/PrintMe.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/implicit/PrintMe.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,67 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+
+package joran.implicit;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.joran.spi.Interpreter;
+import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.joran.spi.RuleStore;
+import ch.qos.logback.core.joran.spi.SimpleRuleStore;
+import ch.qos.logback.core.util.StatusPrinter;
+
+
+/**
+ * This example illustrates the usage of implcit actions.
+ * 
+ * The crucial point to remember about implicit actions is that they
+ * are not associated with a pattern. Moreover, they are added directly to
+ * a Joran Interpreter instead of a rule store.
+ * 
+ * @author Ceki G&uuml;ulc&uuml;
+ */
+public class PrintMe {
+  
+  
+  public static void main(String[] args) throws Exception {
+    Context context = new ContextBase();
+    
+    RuleStore ruleStore = new SimpleRuleStore(context);
+
+    // we start with the rule for the top-most (root) element
+    ruleStore.addRule(new Pattern("*/foo"), new NOPAction());
+
+
+    // Create a new Joran Interpreter and hand it our simple rule store.
+    Interpreter ji = new Interpreter(ruleStore);
+
+    // --------------------------+
+    // Add an implicit action.   |
+    // --------------------------+
+    ji.addImplicitAction(new PrintMeImplicitAction());
+    
+    // Create a SAX parser
+    SAXParserFactory spf = SAXParserFactory.newInstance();
+    SAXParser saxParser = spf.newSAXParser();
+
+    // Parse the file given as the application's first argument and
+    // set the SAX ContentHandler to the Joran Interpreter we just created.
+    saxParser.parse(args[0], ji);
+
+    // The file has been parsed and interpreted. We now print any errors that 
+    // might have occured.
+    StatusPrinter.print(context);
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/implicit/PrintMeImplicitAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/implicit/PrintMeImplicitAction.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,44 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.implicit;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.ImplicitAction;
+import ch.qos.logback.core.joran.spi.ExecutionContext;
+import ch.qos.logback.core.joran.spi.Pattern;
+
+
+
+/**
+ *
+ * A rather trivial implicit action which is applicable as soon as an
+ * element has a printme attribute set to true. 
+ *
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class PrintMeImplicitAction extends ImplicitAction {
+  
+  public boolean isApplicable(
+    Pattern pattern, Attributes attributes, ExecutionContext ec) {
+    String printmeStr = attributes.getValue("printme");
+
+    return Boolean.valueOf(printmeStr).booleanValue();
+  }
+
+  public void begin(ExecutionContext ec, String name, Attributes attributes) {
+    System.out.println("Element <"+name+"> asked to be printed.");
+   }
+
+ 
+  public void end(ExecutionContext ec, String name) {
+  }
+}

Added: logback/trunk/logback-core/examples/src/joran/implicit/implicit1.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/implicit/implicit1.xml	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE foo>
+
+<foo>
+
+  <!-- These elements will print due to the implcit rule -->
+  <xyz printme="true">
+    <abc printme="true"/>
+  </xyz>
+
+  <!-- This element has no associated rule and no implicit rule 
+       applies for it because the printme attribute is not set. -->  
+  <xyz/>
+
+  
+  
+  <!-- This element will not be printed even if its printme 
+       attribute  is set because implicit rules are invoked only 
+       if no explicit rule matches the element. The */foo rule
+       mathches the following element.
+       --> 
+  <foo printme="true"/>
+
+</foo>
\ No newline at end of file

Added: logback/trunk/logback-core/examples/src/joran/newRule/NewRuleCalculator.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/examples/src/joran/newRule/NewRuleCalculator.java	Sun Oct  8 18:14:14 2006
@@ -0,0 +1,68 @@
+/**
+ * Logback: the reliable, fast and flexible logging library for Java.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package joran.newRule;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import joran.calculator.ComputationAction2;
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.joran.action.NewRuleAction;
+import ch.qos.logback.core.joran.spi.Interpreter;
+import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.joran.spi.RuleStore;
+import ch.qos.logback.core.joran.spi.SimpleRuleStore;
+import ch.qos.logback.core.util.StatusPrinter;
+
+
+/**
+ * This example illustrates the usage of NewRuleAction which allows the Joran
+ * interpreter to learn new rules on the fly from the XML file being
+ * interpreted.
+ *
+ * This example relies heavily on the code from the joran.calculator package.
+ *
+ * @author Ceki G&uuml;ulc&uuml;
+ */
+public class NewRuleCalculator {
+  public static void main(String[] args) throws Exception {
+    // As usual, we create a simple rule store.
+    Context context = new ContextBase();
+    RuleStore ruleStore = new SimpleRuleStore(context);
+    
+    // we start with the rule for the top-most (root) element
+    ruleStore.addRule(new Pattern("*/computation"), new ComputationAction2());
+
+    // Associate "/new-rule" pattern with NewRuleAction from the 
+    // org.apache.joran.action package.
+    // 
+    // We will let the XML file to teach the Joran interpreter about new rules 
+    ruleStore.addRule(
+      new Pattern("/computation/new-rule"), new NewRuleAction());
+
+    // Create a new Joran Interpreter and hand it our simple rule store.
+    Interpreter ji = new Interpreter(ruleStore);
+
+    // Create a SAX parser
+    SAXParserFactory spf = SAXParserFactory.newInstance();
+    SAXParser saxParser = spf.newSAXParser();
+
+    // Parse the file given as the application's first argument and
+    // set the SAX ContentHandler to the Joran Interpreter we just created.
+    saxParser.parse(args[0], ji);
+
+
+    // The file has been parsed and interpreted. We now print any errors that 
+    // might have occured.
+    StatusPrinter.print(context);
+  }
+}



More information about the logback-dev mailing list