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 20:39:33 UTC

cvs commit: logging-log4j/examples/src/joran/newRule NewRuleCalculator.java newRuleCalculator.xml

ceki        2004/04/09 11:39:32

  Modified:    src/java/org/apache/joran/action NewRuleAction.java
  Added:       examples/src/joran/newRule NewRuleCalculator.java
                        newRuleCalculator.xml
  Log:
  
  - Yet another Joran example covering on the flyrule creation.
  - Cosmetic change to the NewRuleAction
  
  Revision  Changes    Path
  1.9       +2 -2      logging-log4j/src/java/org/apache/joran/action/NewRuleAction.java
  
  Index: NewRuleAction.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/action/NewRuleAction.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- NewRuleAction.java	9 Apr 2004 14:03:48 -0000	1.8
  +++ NewRuleAction.java	9 Apr 2004 18:39:32 -0000	1.9
  @@ -36,8 +36,8 @@
   		// Let us forget about previous errors (in this object)
   		inError = false; 
       String errorMsg;
  -    String pattern =  attributes.getValue(PATTERN_ATTRIBUTE);
  -    String actionClass =  attributes.getValue(ACTION_CLASS_ATTRIBUTE);
  +    String pattern =  attributes.getValue(Action.PATTERN_ATTRIBUTE);
  +    String actionClass =  attributes.getValue(Action.ACTION_CLASS_ATTRIBUTE);
   
       if(Option.isEmpty(pattern)) {
          inError = true;
  
  
  
  1.1                  logging-log4j/examples/src/joran/newRule/NewRuleCalculator.java
  
  Index: NewRuleCalculator.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package joran.newRule;
  
  import joran.calculator.ComputationAction2;
  
  import org.apache.joran.Interpreter;
  import org.apache.joran.Pattern;
  import org.apache.joran.RuleStore;
  import org.apache.joran.action.NewRuleAction;
  import org.apache.joran.helper.SimpleRuleStore;
  
  import java.util.List;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  
  /**
   * 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üulcü
   */
  public class NewRuleCalculator {
    
    
    public static void main(String[] args) throws Exception {
      // Uncomment the following line in order to enable log statements generated
      // from joran itself.
      // org.apache.log4j.BasicConfigurator.configure();
      // As usual, we create a simple rule store.
      RuleStore ruleStore = new SimpleRuleStore();
  
      // 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 errors if any.
      List errorList = ji.getExecutionContext().getErrorList();
  
      if (errorList.size() > 0) {
        System.out.println("The following errors occured:");
  
        for (int i = 0; i < errorList.size(); i++) {
          System.out.println("\t" + errorList.get(i));
        }
      }
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/newRule/newRuleCalculator.xml
  
  Index: newRuleCalculator.xml
  ===================================================================
  <?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="bart">
  
    <!-- Let Joran learn the rules from this XML file instead of programatically -->
    <new-rule pattern="*/computation/literal" actionClass="joran.calculator.LiteralAction"/>
    <new-rule pattern="*/computation/add" actionClass="joran.calculator.AddAction"/>
    <new-rule pattern="*/computation/multiply" actionClass="joran.calculator.MultiplyAction"/>
    
    <!-- Let the computation begin -->
    <computation>
      <literal value="7"/>
      <literal value="3"/>
      <add/>
    </computation>   
   
    <literal value="4"/>
    <multiply/>
  </computation>
  
  

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