ceki        2004/04/09 07:02:37

  Modified:    examples/src/joran/helloWorld HelloWorld.java
  Added:       examples/src/joran/calculator ComputationAction1.java
                        MultiplyAction.java calculator3.xml
                        LiteralAction.java Calculator1.java calculator2.xml
                        ComputationAction2.java AddAction.java
                        Calculator2.java calculator1.xml
  Log:
  

  A  joran example illustrating the collaboration of multiple actions using

  the common execution context stack.
  
  Revision  Changes    Path
  1.1                  
logging-log4j/examples/src/joran/calculator/ComputationAction1.java
  
  Index: ComputationAction1.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.calculator;
  
  import java.util.Stack;
  
  import org.apache.joran.ExecutionContext;
  import org.apache.joran.action.Action;
  import org.apache.joran.helper.Option;
  
  import org.xml.sax.Attributes;
  
  
  /**
   * 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.
   * 
   * @author Ceki Gülcü
   */
  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 (Option.isEmpty(nameStr)) {
        // nothing to do
      } else {
        Integer i = (Integer) ec.peekObject();
        System.out.println(
          "The computation named [" + nameStr + "] resulted in the value " + i);
      }
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/MultiplyAction.java
  
  Index: MultiplyAction.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.calculator;
  
  import org.apache.joran.ErrorItem;
  import org.apache.joran.ExecutionContext;
  import org.apache.joran.action.Action;
  
  import org.xml.sax.Attributes;
  
  import java.util.EmptyStackException;
  
  
  /**
   * A trivial action that writes "Hello world" on the console.
   *
   * See the HelloWorld class for integrating with Joran.
   *
   * @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));
    }
  
    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(new ErrorItem(errMsg));
          throw new IllegalArgumentException(errMsg);
        }
      } catch (EmptyStackException ese) {
        ec.addError(
          new ErrorItem(
            "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.
      
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/calculator3.xml
  
  Index: calculator3.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE computation>
  
  <computation name="toto">
    <computation>
      <literal value="7"/>
      <literal value="3"/>
      <add/>
    </computation>   
   
    <literal value="3"/>
    <multiply/>
  </computation>
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/LiteralAction.java
  
  Index: LiteralAction.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.calculator;
  
  import org.apache.joran.ErrorItem;
  import org.apache.joran.ExecutionContext;
  import org.apache.joran.action.Action;
  import org.apache.joran.helper.Option;
  
  import org.xml.sax.Attributes;
  
  
  /**
   *
   * @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 (Option.isEmpty(valueStr)) {
        ec.addError(
          new ErrorItem(
            "The literal element requires a value attribute"));
      }
  
      try {
        Integer i = Integer.valueOf(valueStr);
        ec.pushObject(i);
      } catch (NumberFormatException nfe) {
        ec.addError(
          new ErrorItem(
            "The value [" + valueStr + "] could not be converter 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.
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/Calculator1.java
  
  Index: Calculator1.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.calculator;
  
  import java.util.List;
  
  import org.apache.joran.Interpreter;
  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.
   * 
   * 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>
   *
   *
   * @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.
      RuleStore ruleStore = new SimpleRuleStore();
  
      // Associate "" pattern with  HelloWorldAction
      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());
      
      // 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);
      
      List errorList = ji.getExecutionContext().getErrorList();
      
      if(errorList.size() > 0) {
        System.out.println("The following errors occured");
        System.out.println(errorList);
      }
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/calculator2.xml
  
  Index: calculator2.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE computation>
  
  <computation name="toto">
    <literal value="7"/>
    <literal value="3"/>
    <add/>
    <literal value="3"/>
    <multiply/>
  </computation>
  
  
  1.1                  
logging-log4j/examples/src/joran/calculator/ComputationAction2.java
  
  Index: ComputationAction2.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.calculator;
  
  import java.util.Stack;
  
  import org.apache.joran.ExecutionContext;
  import org.apache.joran.action.Action;
  import org.apache.joran.helper.Option;
  
  import org.xml.sax.Attributes;
  
  
  /**
   * The ComputationAction will print the result of the compuration made by 
   * children elements but only if the compuration itself is named.
   * 
   * @author Ceki G&uuml;lc&uuml;
   */
  public class ComputationAction2 extends Action {
    public static String NAME_ATR = "name";
  
    Stack paremeterStack = new Stack();
    
    
    public void begin(ExecutionContext ec, String name, Attributes attributes) {
      String nameStr = attributes.getValue(NAME_ATR);
      paremeterStack.push(nameStr);
    }
  
    public void end(ExecutionContext ec, String name) {
      String nameStr = (String) paremeterStack.pop();
      if (Option.isEmpty(nameStr)) {
        // nothing to do
      } else {
        Integer i = (Integer) ec.peekObject();
        System.out.println(
          "The computation named [" + nameStr + "] resulted in the value " + i);
      }
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/AddAction.java
  
  Index: AddAction.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.calculator;
  
  import org.apache.joran.ErrorItem;
  import org.apache.joran.ExecutionContext;
  import org.apache.joran.action.Action;
  
  import org.xml.sax.Attributes;
  
  import java.util.EmptyStackException;
  
  
  /**
   * 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 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));
    }
  
    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(new ErrorItem(errMsg));
          throw new IllegalArgumentException(errMsg);
        }
      } catch (EmptyStackException ese) {
        ec.addError(
          new ErrorItem(
            "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.
      
      // The add computation/add element is not expected to have any children.
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/Calculator2.java
  
  Index: Calculator2.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.calculator;
  
  import java.util.List;
  
  import org.apache.joran.Interpreter;
  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
   */
  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
      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);
      
      List errorList = ji.getExecutionContext().getErrorList();
      
      if(errorList.size() > 0) {
        System.out.println("The following errors occured");
        System.out.println(errorList);
      }
    }
  }
  
  
  
  1.1                  logging-log4j/examples/src/joran/calculator/calculator1.xml
  
  Index: calculator1.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE x>
  
  <x>
  <computation name="total">
    <literal value="3"/>
  </computation>
  </x>
  
  
  1.2       +15 -16    logging-log4j/examples/src/joran/helloWorld/HelloWorld.java
  
  Index: HelloWorld.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/examples/src/joran/helloWorld/HelloWorld.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HelloWorld.java   5 Apr 2004 20:03:56 -0000       1.1
  +++ HelloWorld.java   9 Apr 2004 14:02:37 -0000       1.2
  @@ -16,23 +16,24 @@
   
   package joran.helloWorld;
   
  -import javax.xml.parsers.SAXParser;
  -import javax.xml.parsers.SAXParserFactory;
  -
   import org.apache.joran.Interpreter;
   import org.apache.joran.Pattern;
   import org.apache.joran.RuleStore;
   import org.apache.joran.helper.SimpleRuleStore;
   
  +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>
  @@ -41,24 +42,22 @@
    */
   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();
  -    
  +
       // Associate "hello-world" pattern with  HelloWorldAction
  -    ruleStore.addRule(
  -      new Pattern("hello-world"), new 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();
  -    
  +    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);  
  +    saxParser.parse(args[0], ji);
     }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to