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/03 16:05:16 UTC

cvs commit: logging-log4j/src/java/org/apache/joran/action ParamAction.java NewRuleAction.java NestComponentIA.java Action.java

ceki        2004/04/03 06:05:16

  Modified:    src/java/org/apache/joran ExecutionContext.java
                        Interpreter.java Pattern.java
               src/java/org/apache/joran/helper Option.java
               src/java/org/apache/joran/action ParamAction.java
                        NewRuleAction.java NestComponentIA.java Action.java
  Added:       src/java/org/apache/joran ErrorItem.java
               src/java/org/apache/joran/helper SimpleRuleStore.java
                        SAXErrorHandler.java
  Removed:     src/java/org/apache/joran SimpleRuleStore.java
  Log:
  Various refactoring attempts in Joran. Mostly cosmetic
  
  Revision  Changes    Path
  1.5       +6 -2      logging-log4j/src/java/org/apache/joran/ExecutionContext.java
  
  Index: ExecutionContext.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/ExecutionContext.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ExecutionContext.java	31 Mar 2004 19:02:14 -0000	1.4
  +++ ExecutionContext.java	3 Apr 2004 14:05:15 -0000	1.5
  @@ -24,6 +24,7 @@
   
   import org.apache.log4j.Logger;
   import org.apache.log4j.helpers.OptionConverter;
  +import org.xml.sax.Locator;
   
   
   /**
  @@ -49,14 +50,17 @@
   		errorList = new Vector();
   	}
   	
  -	public void addError(String error) {
  -		errorList.add(error);
  +	public void addError(ErrorItem errorItem) {
  +		errorList.add(errorItem);
   	}
   
     public List getErrorList() {
       return errorList;
     }
   
  +  public Locator getLocator() {
  +    return joranParser.getLocator();
  +  }
     public Interpreter getJoranParser() {
       return joranParser;
     }
  
  
  
  1.3       +87 -13    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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Interpreter.java	31 Mar 2004 19:45:43 -0000	1.2
  +++ Interpreter.java	3 Apr 2004 14:05:15 -0000	1.3
  @@ -18,10 +18,14 @@
   
   import org.apache.joran.action.*;
   
  +
   import org.apache.log4j.Logger;
   
   import org.xml.sax.Attributes;
  +import org.xml.sax.ErrorHandler;
   import org.xml.sax.Locator;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.SAXParseException;
   import org.xml.sax.helpers.DefaultHandler;
   
   import java.util.ArrayList;
  @@ -30,28 +34,58 @@
   import java.util.Stack;
   import java.util.Vector;
   
  +
   // TODO Errors should be reported in Error objects instead of just strings.
   // TODO Interpreter should set its own ErrorHander for XML parsing errors.
  -
  +/**
  + * <id>Interpreter</id> is Joran's main driving class. It acts as a SAX 
  + * {@link ContentHandler} which invokes various 
  + * {@link Action actions} according to predefined patterns. 
  + * 
  + * <p>Patterns are kept in a {@link RuleStore} which is programmed to store and
  + * then later produce the applicable actions for a given pattern.
  + * 
  + * <p>The pattern corresponding to a top level &lt;a&gt; element is the string 
  + * <id>"a"</id>.
  + * 
  + * <p>The pattern corresponding to an element &lt;b&gt; embeded within a top level 
  + * &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
  + * serves as a wildcard for the level of nesting.  
  + * 
  + * Conceptually, this is very similar to the API of commons-digester. Joran 
  + * offers several small advantages. First and foremost, it offers support for
  + * implicit actions which result in a significant leap in flexibility. Second,
  + * in our opinion better error reporting capability. Third, it is self-reliant.
  + * It does not depend on other APIs, in particular commons-logging which is
  + * a big no-no for log4j. Last but not least, joran is quite tiny and is 
  + * expected to remain so.  
  + *   
  + * @author Ceki G&uuml;lcu&uuml;
  + *
  + */
   public class Interpreter extends DefaultHandler {
     static final Logger logger = Logger.getLogger(Interpreter.class);
     private static List EMPTY_LIST = new Vector(0);
  -  
     private RuleStore ruleStore;
     private ExecutionContext ec;
     private ArrayList implicitActions;
     Pattern pattern;
     Locator locator;
  +  ErrorHandler saxExceptionHandler;
  +  
     /**
  -   * The <code>actionListStack</code> contains a list of actions that are 
  +   * The <id>actionListStack</id> contains a list of actions that are
      * executing for the given XML element.
  -   * 
  +   *
      * A list of actions is pushed by the {link #startElement} and popped by
  -   * {@link #endElement}. 
  -   * 
  +   * {@link #endElement}.
  +   *
      */
     Stack actionListStack;
  -  
  +
     Interpreter(RuleStore rs) {
       ruleStore = rs;
       ec = new ExecutionContext(this);
  @@ -71,11 +105,11 @@
     public void startElement(
       String namespaceURI, String localName, String qName, Attributes atts) {
       String x = null;
  - 
  +
       String tagName = getTagName(localName, qName);
   
       logger.debug("in startElement <" + tagName + ">");
  -      
  +
       pattern.push(tagName);
   
       List applicableActionList = getapplicableActionList(pattern);
  @@ -85,7 +119,7 @@
         callBeginAction(applicableActionList, tagName, atts);
       } else {
         actionListStack.add(EMPTY_LIST);
  -      logger.debug("no applicable action for <"+tagName+">.");
  +      logger.debug("no applicable action for <" + tagName + ">.");
       }
     }
   
  @@ -100,14 +134,14 @@
       pattern.pop();
     }
   
  -
  -  public Locator getDocumentLocator() {
  +  public Locator getLocator() {
       return locator;
     }
  +
     public void setDocumentLocator(Locator l) {
       locator = l;
     }
  -  
  +
     String getTagName(String localName, String qName) {
       String tagName = localName;
   
  @@ -192,5 +226,45 @@
   
     public void setRuleStore(RuleStore ruleStore) {
       this.ruleStore = ruleStore;
  +  }
  +
  +  public void endDocument() {
  +  }
  +
  +  public void error(SAXParseException spe) throws SAXException {
  +    if(saxExceptionHandler != null) {
  +      saxExceptionHandler.error(spe);  
  +    }
  +  }
  +  
  +  public void fatalError(SAXParseException spe)  throws SAXException {
  +    if(saxExceptionHandler != null) {
  +      saxExceptionHandler.fatalError(spe);  
  +    }
  +  }
  +  
  +  public void warning(SAXParseException spe) throws SAXException {
  +    if(saxExceptionHandler != null) {
  +      saxExceptionHandler.warning(spe);  
  +    }
  +  } 
  +
  +
  +
  +  public void endPrefixMapping(java.lang.String prefix) {
  +  }
  +
  +  public void ignorableWhitespace(char[] ch, int start, int length) {
  +  }
  +
  +  public void processingInstruction(
  +    java.lang.String target, java.lang.String data) {
  +  }
  +
  +  public void skippedEntity(java.lang.String name) {
  +  }
  +
  +  public void startPrefixMapping(
  +    java.lang.String prefix, java.lang.String uri) {
     }
   }
  
  
  
  1.6       +3 -3      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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Pattern.java	30 Mar 2004 17:13:22 -0000	1.5
  +++ Pattern.java	3 Apr 2004 14:05:15 -0000	1.6
  @@ -59,7 +59,7 @@
       //System.out.println(components);
     }
   
  -  void push(String s) {
  +  public void push(String s) {
       components.add(s);
     }
   
  @@ -71,13 +71,13 @@
       return (String) components.get(i);
     }
   
  -  void pop() {
  +  public void pop() {
       if (!components.isEmpty()) {
         components.remove(components.size() - 1);
       }
     }
     
  -  String peekLast() {
  +  public String peekLast() {
       if (!components.isEmpty()) {
         int size = components.size();
         return (String) components.get(size - 1);
  
  
  
  1.1                  logging-log4j/src/java/org/apache/joran/ErrorItem.java
  
  Index: ErrorItem.java
  ===================================================================
  package org.apache.joran;
  
  import org.xml.sax.Locator;
  
  
  /**
   * @author ceki
   *
   * To change the template for this generated type comment go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  public class ErrorItem {
    String message;
    int colNumber;
    int lineNulber;
    Throwable exception;
  
    public ErrorItem(String message, Locator locator, Exception e) {
      this.message = message;
  
      if (locator != null) {
        colNumber = locator.getColumnNumber();
        lineNulber = locator.getLineNumber();
      }
  
      exception = e;
    }
  
    public ErrorItem(String message, Locator locator) {
      this(message, locator, null);
    }
  
    public int getColNumber() {
      return colNumber;
    }
  
    public void setColNumber(int colNumber) {
      this.colNumber = colNumber;
    }
  
    public Throwable getException() {
      return exception;
    }
  
    public void setException(Throwable exception) {
      this.exception = exception;
    }
  
    public int getLineNulber() {
      return lineNulber;
    }
  
    public void setLineNulber(int lineNulber) {
      this.lineNulber = lineNulber;
    }
  
    public String getMessage() {
      return message;
    }
  
    public void setMessage(String message) {
      this.message = message;
    }
  }
  
  
  
  1.2       +20 -7     logging-log4j/src/java/org/apache/joran/helper/Option.java
  
  Index: Option.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/helper/Option.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Option.java	12 Sep 2003 18:24:26 -0000	1.1
  +++ Option.java	3 Apr 2004 14:05:15 -0000	1.2
  @@ -1,17 +1,30 @@
  +/*
  + * 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 org.apache.joran.helper;
   
   /**
  - * 
  - * 
  + *
  + *
    * @author Ceki G&uuml;lc&uuml;
    */
   public class Option {
  -
     static final String EMPTY_STR = "";
  -  
  -   static public boolean isEmpty(String val) {
  -     return (val == null || EMPTY_STR.equals(val));
  -   }
   
  +  public static boolean isEmpty(String val) {
  +    return ((val == null) || EMPTY_STR.equals(val));
  +  }
   }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/joran/helper/SimpleRuleStore.java
  
  Index: SimpleRuleStore.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 org.apache.joran.helper;
  
  import org.apache.joran.*;
  import org.apache.joran.RuleStore;
  import org.apache.joran.action.*;
  
  import org.apache.log4j.Logger;
  import org.apache.log4j.helpers.OptionConverter;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  
  
  public class SimpleRuleStore implements RuleStore {
    final static Logger logger = Logger.getLogger(SimpleRuleStore.class);
  
    HashMap rules = new HashMap();
  
    public void addRule(Pattern pattern, Action action) {
      //System.out.println("pattern to add is:" + pattern + "hashcode:" + pattern.hashCode());
      List a4p = (List) rules.get(pattern);
  
      if (a4p == null) {
        a4p = new ArrayList();
        rules.put(pattern, a4p);
      }
  
      a4p.add(action);
    }
  
    public void addRule(Pattern pattern, String actionClassName) {
      Action action =
        (Action) OptionConverter.instantiateByClassName(
          actionClassName, Action.class, null);
  
      if (action != null) {
        addRule(pattern, action);
      } else {
        logger.warn("Could not intantiate Action of class ["+actionClassName+"].");
      }
    }
  
    public List matchActions(Pattern pattern) {
      //System.out.println("pattern to search for:" + pattern + ", hashcode: " + pattern.hashCode());
      //System.out.println("rules:" + rules);
      ArrayList a4p = (ArrayList) rules.get(pattern);
  
      if (a4p != null) {
        return a4p;
      } else {
        Iterator patternsIterator = rules.keySet().iterator();
        int max = 0;
        Pattern longestMatch = null;
  
        while (patternsIterator.hasNext()) {
          Pattern p = (Pattern) patternsIterator.next();
  
          if ((p.size() > 1) && p.get(0).equals("*")) {
            int r = pattern.tailMatch(p);
  
            //System.out.println("tailMatch " +r);
            if (r > max) {
              //System.out.println("New longest match "+p);
              max = r;
              longestMatch = p;
            }
          }
        }
  
        if (longestMatch != null) {
          return (ArrayList) rules.get(longestMatch);
        } else {
          return null;
        }
      }
    }
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/joran/helper/SAXErrorHandler.java
  
  Index: SAXErrorHandler.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 org.apache.joran.helper;
  
  import org.apache.log4j.helpers.LogLog;
  
  import org.xml.sax.ErrorHandler;
  import org.xml.sax.SAXParseException;
  
  
  public class SAXErrorHandler implements ErrorHandler {
    public void error(SAXParseException ex) {
      LogLog.error(
        "Parsing error on line " + ex.getLineNumber() + " and column "
        + ex.getColumnNumber());
      LogLog.error(ex.getMessage(), ex.getException());
    }
  
    public void fatalError(SAXParseException ex) {
      error(ex);
    }
  
    public void warning(SAXParseException ex) {
      LogLog.warn(
        "Parsing problem on line " + ex.getLineNumber() + " and column "
        + ex.getColumnNumber());
      LogLog.warn(ex.getMessage(), ex.getException());
    }
  }
  
  
  
  1.7       +3 -2      logging-log4j/src/java/org/apache/joran/action/ParamAction.java
  
  Index: ParamAction.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/action/ParamAction.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ParamAction.java	31 Mar 2004 13:13:15 -0000	1.6
  +++ ParamAction.java	3 Apr 2004 14:05:15 -0000	1.7
  @@ -1,5 +1,6 @@
   package org.apache.joran.action;
   
  +import org.apache.joran.ErrorItem;
   import org.apache.joran.ExecutionContext;
   
   import org.apache.log4j.Logger;
  @@ -21,14 +22,14 @@
       if(name==null) {
   			inError = true;
   			logger.error(NO_NAME);
  -			ec.addError(NO_NAME);	
  +			ec.addError(new ErrorItem(NO_NAME, ec.getLocator()));	
       	return;
       }
   
   		if(value==null) {
   			inError = true;
   			logger.error(NO_VALUE);
  -			ec.addError(NO_VALUE);	
  +			ec.addError(new ErrorItem(NO_VALUE, ec.getLocator()));	
   			return;
   		}
       
  
  
  
  1.7       +4 -5      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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NewRuleAction.java	31 Mar 2004 13:13:15 -0000	1.6
  +++ NewRuleAction.java	3 Apr 2004 14:05:15 -0000	1.7
  @@ -16,18 +16,17 @@
   
   package org.apache.joran.action;
   
  +import org.apache.joran.ErrorItem;
   import org.apache.joran.ExecutionContext;
   import org.apache.joran.Pattern;
   import org.apache.joran.helper.Option;
   
  -import org.apache.log4j.Layout;
   import org.apache.log4j.Logger;
   import org.xml.sax.Attributes;
   
   
   public class NewRuleAction extends Action {
     static final Logger logger = Logger.getLogger(NewRuleAction.class);
  -  Layout layout;
   
     /**
      * Instantiates an layout of the given class and sets its name.
  @@ -44,7 +43,7 @@
          inError = true;
          errorMsg = "No 'pattern' attribute in <newRule>";
          logger.warn(errorMsg);
  -       ec.addError(errorMsg);
  +       ec.addError(new ErrorItem(errorMsg, ec.getLocator()));
          return;
        }
       
  @@ -52,7 +51,7 @@
            inError = true;
            errorMsg = "No 'actionClass' attribute in <newRule>";
            logger.warn(errorMsg);
  -         ec.addError(errorMsg);
  +         ec.addError(new ErrorItem(errorMsg, ec.getLocator()));
            return;
        }
          
  @@ -63,7 +62,7 @@
         inError = true;
         errorMsg =  "Could not add new Joran parsing rule ["+pattern+","+actionClass+"]"; 
         logger.error(errorMsg, oops);
  -      ec.addError(errorMsg);
  +      ec.addError(new ErrorItem(errorMsg, ec.getLocator()));
       }
     }
   
  
  
  
  1.8       +4 -3      logging-log4j/src/java/org/apache/joran/action/NestComponentIA.java
  
  Index: NestComponentIA.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/action/NestComponentIA.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- NestComponentIA.java	31 Mar 2004 19:02:14 -0000	1.7
  +++ NestComponentIA.java	3 Apr 2004 14:05:15 -0000	1.8
  @@ -16,6 +16,7 @@
   
   package org.apache.joran.action;
   
  +import org.apache.joran.ErrorItem;
   import org.apache.joran.ExecutionContext;
   import org.apache.joran.helper.Option;
   
  @@ -56,7 +57,7 @@
         
         default: 
         inError= true;
  -      ec.addError("PropertySetter.canContainComponent returned "+containmentType);
  +      ec.addError(new ErrorItem("PropertySetter.canContainComponent returned "+containmentType, ec.getLocator()));
         return false;
       }
     }
  @@ -71,7 +72,7 @@
           inError = true;
           String errMsg = "No class name attribute in <"+localName+">";
           logger.error(errMsg);
  -        ec.addError(errMsg);
  +        ec.addError(new ErrorItem(errMsg, ec.getLocator()));
           return;
         }
         
  @@ -87,7 +88,7 @@
           inError = true;      
           String msg =  "Could not create component <"+localName+">.";
           logger.error(msg, oops);
  -        ec.addError(msg);
  +        ec.addError(new ErrorItem(msg, ec.getLocator()));
         }
     }
   
  
  
  
  1.11      +2 -2      logging-log4j/src/java/org/apache/joran/action/Action.java
  
  Index: Action.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/joran/action/Action.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Action.java	31 Mar 2004 19:34:16 -0000	1.10
  +++ Action.java	3 Apr 2004 14:05:15 -0000	1.11
  @@ -67,7 +67,7 @@
     
     protected int getColumnNumber(ExecutionContext ec) {
       Interpreter jp = ec.getJoranParser();
  -    Locator locator = jp.getDocumentLocator();
  +    Locator locator = jp.getLocator();
       if(locator != null) {
         return locator.getColumnNumber();
       }
  @@ -76,7 +76,7 @@
     
     protected int getLineNumber(ExecutionContext ec) {
       Interpreter jp = ec.getJoranParser();
  -    Locator locator = jp.getDocumentLocator();
  +    Locator locator = jp.getLocator();
       if(locator != null) {
         return locator.getLineNumber();
       }
  
  
  

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