ceki        2003/09/02 11:36:29

  Modified:    src/java/org/apache/joran JoranParser.java RuleStore.java
                        SimpleRuleStore.java
  Added:       src/java/org/apache/joran ExecutionContext.java
                        ImplicitRule.java
  Log:
  
  Joran can now configure loggers and set their levels. (It still cannot 
  configure appenders or attach them to loggers.)
  
  Revision  Changes    Path
  1.3       +57 -14    jakarta-log4j/src/java/org/apache/joran/JoranParser.java
  
  Index: JoranParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/joran/JoranParser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JoranParser.java  25 Aug 2003 16:17:59 -0000      1.2
  +++ JoranParser.java  2 Sep 2003 18:36:29 -0000       1.3
  @@ -49,6 +49,10 @@
   
   package org.apache.joran;
   
  +import org.apache.joran.action.*;
  +
  +import org.apache.log4j.Logger;
  +
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   import org.w3c.dom.Node;
  @@ -58,31 +62,44 @@
   
   
   public class JoranParser {
  +  static final Logger logger = Logger.getLogger(JoranParser.class);
     private RuleStore ruleStore;
  +  private ExecutionContext ec;
   
     JoranParser(RuleStore rs) {
       ruleStore = rs;
  +    ec = new ExecutionContext(this);
  +  }
  +
  +  public ExecutionContext getExecutionContext() {
  +    return ec;
     }
   
     public void parse(Document document) {
  -    Pattern pattern = new Pattern();
  +    Pattern currentPattern = new Pattern();
       Element e = document.getDocumentElement();
  -    loop(e, pattern);
  +    loop(e, currentPattern);
     }
   
  -  void loop(Node n, Pattern pattern) {
  +  void loop(Node n, Pattern currentPattern) {
       if (n == null) {
         return;
       }
   
       try {
  -      pattern.push(n.getNodeName());
  +      currentPattern.push(n.getNodeName());
   
         if (n instanceof Element) {
  -        System.out.println("pattern is " + pattern);
  +        logger.debug("pattern is " + currentPattern);
         }
   
  -      List applicableActionList = ruleStore.matchActions(pattern);
  +      List applicableActionList = ruleStore.matchActions(currentPattern);
  +
  +      //logger.debug("set of applicable patterns: " + applicableActionList);
  +
  +      if (applicableActionList == null) {
  +        applicableActionList = lookupImplicitAction(currentPattern);
  +      }
   
         if (applicableActionList != null) {
           callBeginAction(applicableActionList, n);
  @@ -90,18 +107,27 @@
   
         if (n.hasChildNodes()) {
           for (Node c = n.getFirstChild(); c != null; c = c.getNextSibling()) {
  -          loop(c, pattern);
  +          loop(c, currentPattern);
           }
         }
   
         if (applicableActionList != null) {
  -        callEndAction(n);
  +        callEndAction(applicableActionList, n);
         }
       } finally {
  -      pattern.pop();
  +      currentPattern.pop();
       }
     }
   
  +  /**
  +   * Check if any implicit actions are applicable. As soon as an applicable
  +   * action is found, it is returned. Thus, the returned list will have at most
  +   * one element.
  +   */
  +  List lookupImplicitAction(Pattern p) {
  +    return null;
  +  }
  +
     void callBeginAction(List applicableActionList, Node n) {
       if (applicableActionList == null) {
         return;
  @@ -115,18 +141,35 @@
   
       Element e = (Element) n;
       String localName = n.getNodeName();
  -    System.out.println("New node: <" + localName + ">");
  -    System.out.println("  node is an element");
  -    System.out.println("  element attribs: " + e.getAttributes());
   
       Iterator i = applicableActionList.iterator();
   
       while (i.hasNext()) {
         Action action = (Action) i.next();
  -      action.begin(e);
  +      action.begin(ec, e);
       }
     }
   
  -  void callEndAction(Node n) {
  +  void callEndAction(List applicableActionList, Node n) {
  +    if (applicableActionList == null) {
  +      return;
  +    }
  +
  +    short type = n.getNodeType();
  +
  +    if (type != Node.ELEMENT_NODE) {
  +      return;
  +    }
  +
  +    Element e = (Element) n;
  +    String localName = n.getNodeName();
  +    //logger.debug("About to call end actions on node: <" + localName + ">");
  +
  +    Iterator i = applicableActionList.iterator();
  +
  +    while (i.hasNext()) {
  +      Action action = (Action) i.next();
  +      action.end(ec, e);
  +    }
     }
   }
  
  
  
  1.3       +2 -0      jakarta-log4j/src/java/org/apache/joran/RuleStore.java
  
  Index: RuleStore.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/joran/RuleStore.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- RuleStore.java    25 Aug 2003 16:17:59 -0000      1.2
  +++ RuleStore.java    2 Sep 2003 18:36:29 -0000       1.3
  @@ -51,6 +51,8 @@
   
   import java.util.List;
   
  +import org.apache.joran.action.*;
  +
   
   public interface RuleStore {
     public void addRule(Pattern pattern, Action action);
  
  
  
  1.3       +2 -0      jakarta-log4j/src/java/org/apache/joran/SimpleRuleStore.java
  
  Index: SimpleRuleStore.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/joran/SimpleRuleStore.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SimpleRuleStore.java      25 Aug 2003 16:17:59 -0000      1.2
  +++ SimpleRuleStore.java      2 Sep 2003 18:36:29 -0000       1.3
  @@ -54,6 +54,8 @@
   import java.util.Iterator;
   import java.util.List;
   
  +import org.apache.joran.action.*;
  +
   
   public class SimpleRuleStore implements RuleStore {
     HashMap rules = new HashMap();
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/joran/ExecutionContext.java
  
  Index: ExecutionContext.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.joran;
  
  import java.util.List;
  import java.util.Stack;
  import java.util.Vector;
  
  
  /**
   *
   * Joran is designed to parse DOM trees
   *
   */
  public class ExecutionContext {
    Stack objectStack;
        Vector errorList;
        JoranParser joranParser;
      
        public ExecutionContext(JoranParser joranParser) {
                this.joranParser = joranParser;
                objectStack = new Stack();
                errorList = new Vector();
        }
        
        public void addError(String error) {
                errorList.add(error);
        }
  
    public List getErrorList() {
      return errorList;
    }
  
    public JoranParser getJoranParser() {
      return joranParser;
    }
  
    public Stack getObjectStack() {
      return objectStack;
    }
  
        public Object peekObject() {
                return objectStack.peek();
        }
  
    public void pushObject(Object o) {
                objectStack.push(o);
    }
    
    public Object popObject() {
      return    objectStack.pop();
    }
    
    public Object getObject(int i) {
                return objectStack.get(i);
    }
  }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/joran/ImplicitRule.java
  
  Index: ImplicitRule.java
  ===================================================================
  package org.apache.joran;
  
  import org.apache.joran.action.*;
  import org.w3c.dom.Element;
  
  public abstract class ImplicitRule {
  
    Action action;
    
        ImplicitRule(Action a) {
                action = a;
        }
        
        public Action getAction() {
          return action;
        }
        
        public abstract boolean isApplicable(Element e, ExecutionContext ec);
        
  }
  
  
  

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

Reply via email to