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 2005/01/31 21:30:44 UTC

cvs commit: logging-log4j/src/java/org/apache/log4j/lbel Parser.java TokenStream.java

ceki        2005/01/31 12:30:44

  Modified:    src/java/org/apache/log4j/lbel/comparator
                        LoggerComparator.java MessageComparator.java
               src/java/org/apache/log4j/lbel Parser.java TokenStream.java
  Added:       src/java/org/apache/log4j/lbel/comparator
                        PropertyComparator.java StringComparator.java
  Log:
  Factoring out duplicated code
  
  Revision  Changes    Path
  1.4       +23 -60    logging-log4j/src/java/org/apache/log4j/lbel/comparator/LoggerComparator.java
  
  Index: LoggerComparator.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/comparator/LoggerComparator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LoggerComparator.java	27 Jan 2005 21:37:48 -0000	1.3
  +++ LoggerComparator.java	31 Jan 2005 20:30:44 -0000	1.4
  @@ -1,80 +1,43 @@
   /*
  - * Created on Jan 27, 2005
  + * Copyright 1999,2004 The Apache Software Foundation.
    *
  - * To change the template for this generated file go to
  - * Window>Preferences>Java>Code Generation>Code and Comments
  + * 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.log4j.lbel.comparator;
   
   import org.apache.log4j.lbel.Operator;
   import org.apache.log4j.lbel.ScanError;
   import org.apache.log4j.spi.LoggingEvent;
  -import org.apache.oro.text.regex.MalformedPatternException;
  -import org.apache.oro.text.regex.Pattern;
  -import org.apache.oro.text.regex.Perl5Compiler;
  -import org.apache.oro.text.regex.Perl5Matcher;
   
   
   /**
  - * Compare the logger of an event passed as parameter to the logger name and 
  - * comparison operator set in the constructor. 
  - * 
  + * Compare the logger of an event passed as parameter to the logger name and
  + * comparison operator set in the constructor.
  + *
    * <p>Allowed comparison operators are '=', '!=', '>', '>=', '<', '<=', '~' and
    * '!~' where '~' stands for regular expression match.
  - * 
  + *
    * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
    * @author Scott Deboy
    */
  -public class LoggerComparator implements Comparator {
  -
  -  Operator operator;
  -  String rightSide;
  -  Pattern rightSidePattern;
  -  Perl5Matcher matcher;
  -  
  -  public LoggerComparator(Operator operator, String rightSide) throws ScanError  {
  -    this.operator = operator;
  -    this.rightSide = rightSide;
  -    
  -    
  -    if(operator.isRegex()) {
  -      Perl5Compiler compiler = new Perl5Compiler();
  -      matcher = new Perl5Matcher();
  -      try {
  -        rightSidePattern = compiler.compile(rightSide);
  -      } catch(MalformedPatternException mfpe) {
  -        throw new ScanError("Malformed pattern ["+rightSide+"]", mfpe);
  -      }
  -    }
  -    
  +public class LoggerComparator extends StringComparator {
  +  public LoggerComparator(final Operator operator, String rightSide)
  +    throws ScanError {
  +    super(operator, rightSide);
     }
  -  
   
  -  public boolean compare(LoggingEvent event) {
  -    if(operator.isRegex()) {
  -      boolean match = matcher.contains(event.getLoggerName(), rightSidePattern);
  -      if(operator.getCode() == Operator.REGEX_MATCH) {
  -        return match;
  -      } else {
  -        return !match;
  -      }
  -    }
  -
  -    if(operator.getCode() == Operator.CHILDOF) {
  -      return event.getLoggerName().startsWith(rightSide);
  -    }
  -  
  -    int compResult = event.getLoggerName().compareTo(rightSide);
  -    switch(operator.getCode()) {
  -    case Operator.EQUAL: return compResult == 0;   
  -    case Operator.NOT_EQUAL: return compResult != 0;      
  -    case Operator.GREATER: return compResult > 0;   
  -    case Operator.GREATER_OR_EQUAL: return compResult >= 0;   
  -    case Operator.LESS: return compResult < 0;   
  -    case Operator.LESS_OR_EQUAL: return compResult <= 0;    
  -    }
  -    
  -    throw new IllegalStateException("Unreachable state reached, operator "+operator);
  +  protected String getLeftSide(LoggingEvent event) {
  +    return event.getLoggerName();
     }
  -
   }
  
  
  
  1.4       +4 -64     logging-log4j/src/java/org/apache/log4j/lbel/comparator/MessageComparator.java
  
  Index: MessageComparator.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/comparator/MessageComparator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MessageComparator.java	27 Jan 2005 21:37:48 -0000	1.3
  +++ MessageComparator.java	31 Jan 2005 20:30:44 -0000	1.4
  @@ -26,11 +26,6 @@
   import org.apache.log4j.lbel.ScanError;
   import org.apache.log4j.spi.LoggingEvent;
   
  -import org.apache.oro.text.regex.MalformedPatternException;
  -import org.apache.oro.text.regex.Pattern;
  -import org.apache.oro.text.regex.Perl5Compiler;
  -import org.apache.oro.text.regex.Perl5Matcher;
  -
   
   /**
    * Compare the message of an event passed as parameter to the logger name and
  @@ -42,68 +37,13 @@
    * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
    * @author Scott Deboy
    */
  -public class MessageComparator implements Comparator {
  -  Operator operator;
  -  String rightSide;
  -  Pattern rightSidePattern;
  -  Perl5Matcher matcher;
  -
  +public class MessageComparator extends StringComparator {
     public MessageComparator(final Operator operator, String rightSide)
       throws ScanError {
  -    this.operator = operator;
  -    this.rightSide = rightSide;
  -
  -    if (operator.isRegex()) {
  -      Perl5Compiler compiler = new Perl5Compiler();
  -      matcher = new Perl5Matcher();
  -
  -      try {
  -        rightSidePattern = compiler.compile(rightSide);
  -      } catch (MalformedPatternException mfpe) {
  -        throw new ScanError("Malformed pattern [" + rightSide + "]", mfpe);
  -      }
  -    }
  +    super(operator, rightSide);
     }
   
  -  public boolean compare(LoggingEvent event) {
  -    if (operator.isRegex()) {
  -      boolean match =
  -        matcher.contains(event.getRenderedMessage(), rightSidePattern);
  -      if (operator.getCode() == Operator.REGEX_MATCH) {
  -        return match;
  -      } else {
  -        return !match;
  -      }
  -    }
  -    int compResult;
  -    
  -    String leftSide = event.getRenderedMessage();
  -    if(leftSide == null) {
  -      compResult = -1;
  -    } else {
  -      compResult = event.getRenderedMessage().compareTo(rightSide);
  -    }
  -    
  -    switch (operator.getCode()) {
  -    case Operator.EQUAL:
  -      return compResult == 0;
  -    case Operator.NOT_EQUAL:
  -      return compResult != 0;
  -    case Operator.GREATER:
  -      return compResult > 0;
  -    case Operator.GREATER_OR_EQUAL:
  -      return compResult >= 0;
  -    case Operator.LESS:
  -      return compResult < 0;
  -    case Operator.LESS_OR_EQUAL:
  -      return compResult <= 0;
  -    }
  -
  -    throw new IllegalStateException(
  -      "Unreachable state reached, operator " + operator);
  -  }
  -  
  -  public String toString() {
  -    return "MessageComparator("+operator+", "+rightSide+")";
  +  protected String getLeftSide(LoggingEvent event) {
  +    return event.getRenderedMessage();
     }
   }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/lbel/comparator/PropertyComparator.java
  
  Index: PropertyComparator.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.
   */
  
  /*
   * Created on Jan 27, 2005
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  package org.apache.log4j.lbel.comparator;
  
  import org.apache.log4j.lbel.Operator;
  import org.apache.log4j.lbel.ScanError;
  import org.apache.log4j.spi.LoggingEvent;
  
  /**
   * Compare a property of an event passed as parameter to the string value and
   * comparison operator set in the constructor.
   *
   * <p>Allowed comparison operators are '=', '!=', '>', '>=', '<', '<=', '~' and
   * '!~' where '~' stands for regular expression match.
   *
   * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
   * @author Scott Deboy
   */
  public class PropertyComparator extends StringComparator {
    String key;
  
    public PropertyComparator(final Operator operator, String key, String rightSide)
      throws ScanError {
      super(operator, rightSide);
      this.key = key;
    }
    
    protected String getLeftSide(LoggingEvent event) {
      return  event.getProperty(key);
    }
    
    public String toString() {
      return "PropertyComparator("+key+","+operator+", "+rightSide+")";
    }
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/lbel/comparator/StringComparator.java
  
  Index: StringComparator.java
  ===================================================================
  /*
   * Created on Jan 27, 2005
   *
   * To change the template for this generated file go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  package org.apache.log4j.lbel.comparator;
  
  import org.apache.log4j.lbel.Operator;
  import org.apache.log4j.lbel.ScanError;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.oro.text.regex.MalformedPatternException;
  import org.apache.oro.text.regex.Pattern;
  import org.apache.oro.text.regex.Perl5Compiler;
  import org.apache.oro.text.regex.Perl5Matcher;
  
  
  /**
   * Compare the logger of an event passed as parameter to the logger name and 
   * comparison operator set in the constructor. 
   * 
   * <p>Allowed comparison operators are '=', '!=', '>', '>=', '<', '<=', '~' and
   * '!~' where '~' stands for regular expression match.
   * 
   * @author <a href="http://www.qos.ch/log4j/">Ceki G&uuml;lc&uuml;</a>
   * @author Scott Deboy
   */
  abstract public class StringComparator implements Comparator {
  
    Operator operator;
    String rightSide;
    String rightSideWithDotSuffix;
    Pattern rightSidePattern;
    Perl5Matcher matcher;
  
    abstract protected String getLeftSide(LoggingEvent event);
    
    public StringComparator(Operator operator, String rightSide) throws ScanError  {
      this.operator = operator;
      this.rightSide = rightSide;
      
      
      if(operator.isRegex()) {
        Perl5Compiler compiler = new Perl5Compiler();
        matcher = new Perl5Matcher();
        try {
          rightSidePattern = compiler.compile(rightSide);
        } catch(MalformedPatternException mfpe) {
          throw new ScanError("Malformed pattern ["+rightSide+"]", mfpe);
        }
      }
      
      // if CHILDOF operator and rightSide does not end with a dot add one
      if(operator.getCode() == Operator.CHILDOF && !rightSide.endsWith(".")) {
        this.rightSideWithDotSuffix = rightSide + ".";
      }
    }
    
  
    public boolean compare(LoggingEvent event) {
      
      String leftSide = getLeftSide(event);
      
      if(operator.isRegex()) {
        boolean match = matcher.contains(leftSide, rightSidePattern);
        if(operator.getCode() == Operator.REGEX_MATCH) {
          return match;
        } else {
          return !match;
        }
      }
  
      if(operator.getCode() == Operator.CHILDOF) {
        if(leftSide.equals(rightSide)) {
          return true;
        } else {
          return leftSide.startsWith(rightSideWithDotSuffix);
        }
      }
    
      int compResult;
  
      if(leftSide == null) {
        // Assuming rightside can never be null, if leftSide == null, then it is
        // defined to be lexicographically smaller
        compResult = -1;
      } else {
        compResult = leftSide.compareTo(rightSide);
      }
      
      switch(operator.getCode()) {
      case Operator.EQUAL: return compResult == 0;   
      case Operator.NOT_EQUAL: return compResult != 0;      
      case Operator.GREATER: return compResult > 0;   
      case Operator.GREATER_OR_EQUAL: return compResult >= 0;   
      case Operator.LESS: return compResult < 0;   
      case Operator.LESS_OR_EQUAL: return compResult <= 0;    
      }
      
      throw new IllegalStateException("Unreachable state reached, operator "+operator);
    }
  
    
    public String toString() {
      String full = this.getClass().getName();
      int i = full.lastIndexOf(".");
      return full.substring(i)+"("+operator+", "+rightSide+")";
    }
  
  }
  
  
  
  1.7       +9 -0      logging-log4j/src/java/org/apache/log4j/lbel/Parser.java
  
  Index: Parser.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/Parser.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Parser.java	27 Jan 2005 19:57:52 -0000	1.6
  +++ Parser.java	31 Jan 2005 20:30:44 -0000	1.7
  @@ -7,6 +7,7 @@
   import org.apache.log4j.lbel.comparator.LevelComparator;
   import org.apache.log4j.lbel.comparator.LoggerComparator;
   import org.apache.log4j.lbel.comparator.MessageComparator;
  +import org.apache.log4j.lbel.comparator.PropertyComparator;
   import org.apache.log4j.lbel.comparator.TimestampComparator;
   
   /**
  @@ -185,7 +186,15 @@
         operator = getOperator();
         ts.next();
         return new Node(Node.COMPARATOR, new TimestampComparator(operator, getLong()));
  +    case Token.PROPERTY:
  +      String key = (String) token.getValue();
  +      ts.next();
  +      operator = getOperator();
  +      ts.next();
  +      literal = getLiteral();
  +      return new Node(Node.COMPARATOR, new PropertyComparator(operator, key, literal));
       default: throw new IllegalStateException("Unexpected token " +token);
  +    
       }
    	}
     
  
  
  
  1.5       +26 -9     logging-log4j/src/java/org/apache/log4j/lbel/TokenStream.java
  
  Index: TokenStream.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/lbel/TokenStream.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TokenStream.java	27 Jan 2005 21:37:48 -0000	1.4
  +++ TokenStream.java	31 Jan 2005 20:30:44 -0000	1.5
  @@ -37,12 +37,11 @@
     	keywordMap.put("not", new Token(Token.NOT));
     	keywordMap.put("and", new Token(Token.AND));
     	keywordMap.put("or", new Token(Token.OR));
  -  	keywordMap.put("childof", new Token(Token.OPERATOR, "childof"));
  +    keywordMap.put("childof", new Token(Token.OPERATOR, "childof"));
       keywordMap.put("logger", new Token(Token.LOGGER, "logger"));
       keywordMap.put("message", new Token(Token.MESSAGE, "message"));
       keywordMap.put("level", new Token(Token.LEVEL, "level"));
       keywordMap.put("thread", new Token(Token.THREAD, "thread"));
  -    keywordMap.put("property", new Token(Token.PROPERTY, "property"));
       keywordMap.put("date", new Token(Token.DATE, "date"));
     }
     
  @@ -86,13 +85,19 @@
   		    current = new Token(Token.NUMBER, new Long((long) nval));
   		    break;
     		case StreamTokenizer.TT_WORD:
  -  			String key = tokenizer.sval;
  -  		  Token result = (Token) keywordMap.get(key.toLowerCase());
  -  		  if(result != null) {
  -  		  	current = result;
  -  		  } else {
  -  		  	current = new Token(Token.LITERAL, tokenizer.sval);
  -  		  }
  +  			String txt = tokenizer.sval;
  +        String lowerCaseTxt = txt.toLowerCase();
  +
  +        if(txt.startsWith("property.")) {
  +          current = extractPropertyToken(txt);  
  +        } else {
  +    		  Token result = (Token) keywordMap.get(lowerCaseTxt);
  +  		    if(result != null) {
  +  		  	  current = result;
  +  		    } else {
  +  		  	 current = new Token(Token.LITERAL, tokenizer.sval);
  +  		    }
  +        }
           break;  		
     		case '"':
     	  case '\'':
  @@ -144,4 +149,16 @@
     		
     	}
     }
  +  
  +  Token extractPropertyToken(String txt) throws ScanError {
  +    int point = txt.indexOf('.');
  +    String key = txt.substring(point+1);
  +    // Is the key empty? (An empty key is the only thing that can go wrong at
  +    // this stage.
  +    if(key == null || key.length() == 0) {
  +      throw new ScanError("["+txt+"] has zero-legnth key.");
  +    } else {
  +      return new Token(Token.PROPERTY, key);
  +    }
  +  }
   }
  
  
  

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