You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2004/04/09 18:03:19 UTC

cvs commit: logging-log4j/src/java/org/apache/joran Interpreter.java Pattern.java

ceki        2004/04/09 09:03:19

  Modified:    examples/src/joran/calculator ComputationAction1.java
                        MultiplyAction.java calculator3.xml
                        LiteralAction.java Calculator1.java AddAction.java
                        Calculator2.java calculator1.xml
               src/java/org/apache/joran Interpreter.java Pattern.java
  Log:
  
  Improved documentation. 
  Small impovements.
  
  Revision  Changes    Path
  1.2       +1 -3      logging-log4j/examples/src/joran/calculator/ComputationAction1.java
  
  Index: ComputationAction1.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/ComputationAction1.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComputationAction1.java	9 Apr 2004 14:02:37 -0000	1.1
  +++ ComputationAction1.java	9 Apr 2004 16:03:19 -0000	1.2
  @@ -16,8 +16,6 @@
   
   package joran.calculator;
   
  -import java.util.Stack;
  -
   import org.apache.joran.ExecutionContext;
   import org.apache.joran.action.Action;
   import org.apache.joran.helper.Option;
  @@ -29,7 +27,7 @@
    * The ComputationAction 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 computation will not print their result.
  + * words, anonymous computations will not print their result.
    * 
    * @author Ceki Gülcü
    */
  
  
  
  1.2       +10 -6     logging-log4j/examples/src/joran/calculator/MultiplyAction.java
  
  Index: MultiplyAction.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/MultiplyAction.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MultiplyAction.java	9 Apr 2004 14:02:37 -0000	1.1
  +++ MultiplyAction.java	9 Apr 2004 16:03:19 -0000	1.2
  @@ -26,19 +26,25 @@
   
   
   /**
  - * A trivial action that writes "Hello world" on the console.
    *
  - * See the HelloWorld class for integrating with Joran.
  + * 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ülcü
    */
   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));
  +    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;
   
  @@ -56,8 +62,7 @@
         }
       } catch (EmptyStackException ese) {
         ec.addError(
  -        new ErrorItem(
  -          "Expecting an integer on the execution stack."));
  +        new ErrorItem("Expecting an integer on the execution stack."));
         throw ese;
       }
       return result;
  @@ -68,6 +73,5 @@
       // In general, the end() method of actions associated with elements
       // having no children do not need to perform any processing in their
       // end() method.
  -    
     }
   }
  
  
  
  1.2       +5 -0      logging-log4j/examples/src/joran/calculator/calculator3.xml
  
  Index: calculator3.xml
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/calculator3.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- calculator3.xml	9 Apr 2004 14:02:37 -0000	1.1
  +++ calculator3.xml	9 Apr 2004 16:03:19 -0000	1.2
  @@ -1,6 +1,11 @@
   <?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"/>
  
  
  
  1.2       +9 -4      logging-log4j/examples/src/joran/calculator/LiteralAction.java
  
  Index: LiteralAction.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/LiteralAction.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LiteralAction.java	9 Apr 2004 14:02:37 -0000	1.1
  +++ LiteralAction.java	9 Apr 2004 16:03:19 -0000	1.2
  @@ -26,6 +26,12 @@
   
   /**
    *
  + * 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 {
  @@ -34,11 +40,10 @@
     public void begin(ExecutionContext ec, String name, Attributes attributes) {
       String valueStr = attributes.getValue(VALUE_ATR);
   
  -  
       if (Option.isEmpty(valueStr)) {
         ec.addError(
  -        new ErrorItem(
  -          "The literal element requires a value attribute"));
  +        new ErrorItem("The literal action requires a value attribute"));
  +      return;
       }
   
       try {
  @@ -47,7 +52,7 @@
       } catch (NumberFormatException nfe) {
         ec.addError(
           new ErrorItem(
  -          "The value [" + valueStr + "] could not be converter to an Integer",
  +          "The value [" + valueStr + "] could not be converted to an Integer",
             nfe));
         throw nfe;
       }
  
  
  
  1.2       +17 -15    logging-log4j/examples/src/joran/calculator/Calculator1.java
  
  Index: Calculator1.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/Calculator1.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Calculator1.java	9 Apr 2004 14:02:37 -0000	1.1
  +++ Calculator1.java	9 Apr 2004 16:03:19 -0000	1.2
  @@ -22,14 +22,12 @@
   import org.apache.joran.Pattern;
   import org.apache.joran.RuleStore;
   import org.apache.joran.helper.SimpleRuleStore;
  -import org.apache.log4j.BasicConfigurator;
   
   import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;
   
   
   /**
  - *
    * This examples illustrates collaboration between multiple actions through the
    * common execution context stack.
    * 
  @@ -43,23 +41,25 @@
       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 {
  -
  -    BasicConfigurator.configure();
       // Create a simple rule store where pattern and action associations will
  -    // be kept.
  +    // be kept. This is a basic requirement before invoking a Joran Interpreter.
       RuleStore ruleStore = new SimpleRuleStore();
   
  -    // Associate "" pattern with  HelloWorldAction
  -    ruleStore.addRule(new Pattern("computation"), new ComputationAction1());
  +    // Associate "/computation" pattern with  ComputationAction1
  +    ruleStore.addRule(new Pattern("/computation"), new ComputationAction1());
   
  -    ruleStore.addRule(new Pattern("computation/literal"), new LiteralAction());
  -    ruleStore.addRule(new Pattern("computation/add"), new AddAction());
  -    ruleStore.addRule(new Pattern("computation/multiply"), new MultiplyAction());
  +    // 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);
  @@ -71,12 +71,14 @@
       // 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 errors if any.
       List errorList = ji.getExecutionContext().getErrorList();
  -    
       if(errorList.size() > 0) {
  -      System.out.println("The following errors occured");
  -      System.out.println(errorList);
  +      System.out.println("The following errors occured:");
  +      for(int i = 0; i < errorList.size(); i++) {
  +        System.out.println("\t"+errorList.get(i));
  +      }
       }
     }
   }
  
  
  
  1.2       +13 -10    logging-log4j/examples/src/joran/calculator/AddAction.java
  
  Index: AddAction.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/AddAction.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AddAction.java	9 Apr 2004 14:02:37 -0000	1.1
  +++ AddAction.java	9 Apr 2004 16:03:19 -0000	1.2
  @@ -26,23 +26,29 @@
   
   
   /**
  - * A trivial action that writes "Hello world" on the console.
  - *
  - * See the HelloWorld class for integrating with Joran.
  - *
  + * 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&uuml;lc&uuml;
    */
   public class AddAction 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));
  +    // 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) {
  @@ -56,8 +62,7 @@
         }
       } catch (EmptyStackException ese) {
         ec.addError(
  -        new ErrorItem(
  -          "Expecting an integer on the execution stack."));
  +        new ErrorItem("Expecting an integer on the execution stack."));
         throw ese;
       }
       return result;
  @@ -66,9 +71,7 @@
     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
  +    // having no children do not need to perform any processing in their
       // end() method.
  -    
  -    // The add computation/add element is not expected to have any children.
     }
   }
  
  
  
  1.2       +17 -22    logging-log4j/examples/src/joran/calculator/Calculator2.java
  
  Index: Calculator2.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/Calculator2.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Calculator2.java	9 Apr 2004 14:02:37 -0000	1.1
  +++ Calculator2.java	9 Apr 2004 16:03:19 -0000	1.2
  @@ -22,35 +22,28 @@
   import org.apache.joran.Pattern;
   import org.apache.joran.RuleStore;
   import org.apache.joran.helper.SimpleRuleStore;
  -import org.apache.log4j.BasicConfigurator;
  -
   import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;
   
   
   /**
  - *
  - * 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
  + * 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 {
  -    BasicConfigurator.configure();
  -    // Create a simple rule store where pattern and action associations will
  -    // be kept.
  +
       RuleStore ruleStore = new SimpleRuleStore();
   
  -    // Associate "hello-world" pattern with  HelloWorldAction
  +    // 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());
  @@ -68,11 +61,13 @@
       // 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 errors if any.
       List errorList = ji.getExecutionContext().getErrorList();
  -    
       if(errorList.size() > 0) {
  -      System.out.println("The following errors occured");
  -      System.out.println(errorList);
  +      System.out.println("The following errors occured:");
  +      for(int i = 0; i < errorList.size(); i++) {
  +        System.out.println("\t"+errorList.get(i));
  +      }
       }
     }
   }
  
  
  
  1.2       +1 -3      logging-log4j/examples/src/joran/calculator/calculator1.xml
  
  Index: calculator1.xml
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/calculator/calculator1.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- calculator1.xml	9 Apr 2004 14:02:37 -0000	1.1
  +++ calculator1.xml	9 Apr 2004 16:03:19 -0000	1.2
  @@ -1,8 +1,6 @@
   <?xml version="1.0" encoding="UTF-8" ?>
  -<!DOCTYPE x>
  +<!DOCTYPE computation>
   
  -<x>
   <computation name="total">
     <literal value="3"/>
   </computation>
  -</x>
  \ No newline at end of file
  
  
  
  1.7       +7 -5      logging-log4j/src/java/org/apache/joran/Interpreter.java
  
  Index: Interpreter.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/Interpreter.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Interpreter.java	9 Apr 2004 14:03:48 -0000	1.6
  +++ Interpreter.java	9 Apr 2004 16:03:19 -0000	1.7
  @@ -49,7 +49,7 @@
    * &lt;a&gt; element is the string <id>"a/b"</id>.
    *
    * <p>The pattern corresponding to an &lt;b&gt; and any level of nesting is
  - * "&star;/b. Thus, the &star; character placed at the beginning of a pattern
  + * "&#42;/b. Thus, the &#42; character placed at the beginning of a pattern
    * serves as a wildcard for the level of nesting.
    *
    * Conceptually, this is very similar to the API of commons-digester. Joran
  @@ -115,9 +115,12 @@
         callBeginAction(applicableActionList, tagName, atts);
       } else {
         actionListStack.add(EMPTY_LIST);
  -      String errMsg = "no applicable action for <" + tagName + ">.";
  +
  +      String errMsg =
  +        "no applicable action for <" + tagName + ">, current pattern is ["
  +        + pattern+"]";
         logger.debug(errMsg);
  -      
  +      ec.addError(new ErrorItem(errMsg));
       }
     }
   
  @@ -206,8 +209,7 @@
         try {
           action.begin(ec, tagName, atts);
         } catch (Exception e) {
  -        ec.addError(
  -          new ErrorItem("Action threw an exception", e));
  +        ec.addError(new ErrorItem("Action threw an exception", e));
         }
       }
     }
  
  
  
  1.7       +12 -1     logging-log4j/src/java/org/apache/joran/Pattern.java
  
  Index: Pattern.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/Pattern.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Pattern.java	3 Apr 2004 14:05:15 -0000	1.6
  +++ Pattern.java	9 Apr 2004 16:03:19 -0000	1.7
  @@ -27,6 +27,12 @@
       components = new ArrayList();
     }
   
  +  /**
  +   * Build a pattern from a string.
  +   * 
  +   * Note that "/x" is equivalent to "x" and to "x/"
  +   * 
  +   */
     public Pattern(String p) {
       this();
   
  @@ -162,6 +168,11 @@
     }
   
     public String toString() {
  -    return components.toString();
  +    int size = components.size();
  +    String result = "";
  +    for(int i = 0; i < size; i++) {
  +      result +=  "/" + components.get(i);
  +    }
  +    return result;
     }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org