Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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.jboss.web.rewrite;
 
 import java.util.Map;

Author(s):
Remy Maucherat
 
 public class RewriteCond {
 
     public abstract class Condition {
         public abstract boolean evaluate(String valueResolver resolver);
     }
     
     public class PatternCondition extends Condition {
         public Pattern pattern;
         public Matcher matcher = null;
         public boolean evaluate(String valueResolver resolver) {
             Matcher m = .matcher(value);
             if (m.matches()) {
                  = m;
                 return true;
             } else {
                 return false;
             }
         }
     }
     
     public class LexicalCondition extends Condition {
        
-1: < 0: = 1: >
 
         public int type = 0;
         public String condition;
         public boolean evaluate(String valueResolver resolver) {
             int result = value.compareTo();
             switch () {
             case -1:
                 return (result < 0);
             case 0:
                 return (result == 0);
             case 1:
                 return (result > 0);
             default:
                 return false;
             }
                 
         }
     }
     
     public class ResourceCondition extends Condition {
        
0: -d (is directory ?) 1: -f (is regular file ?) 2: -s (is regular file with size ?)
 
         public int type = 0;
         public boolean evaluate(String valueResolver resolver) {
             switch () {
             case 0:
                 return true;
             case 1:
                 return true;
             case 2:
                 return true;
             default:
                 return false;
             }
                 
         }
     }
     
     protected String testString = null;
     protected String condPattern = null;
     
     public String getCondPattern() {
        return ;
    }
    public void setCondPattern(String condPattern) {
        this. = condPattern;
    }
    public String getTestString() {
        return ;
    }
    public void setTestString(String testString) {
        this. = testString;
    }
    public void parse(Map<StringRewriteMapmaps) {
         = new Substitution();
        .setSub();
        .parse(maps);
        if (.startsWith("!")) {
             = false;
             = .substring(1);
        }
        if (.startsWith("<")) {
            LexicalCondition condition = new LexicalCondition();
            condition.type = -1;
            condition.condition = .substring(1);
        } else if (.startsWith(">")) {
            LexicalCondition condition = new LexicalCondition();
            condition.type = 1;
            condition.condition = .substring(1);
        } else if (.startsWith("=")) {
            LexicalCondition condition = new LexicalCondition();
            condition.type = 0;
            condition.condition = .substring(1);
        } else if (.equals("-d")) {
            ResourceCondition ncondition = new ResourceCondition();
            ncondition.type = 0;
        } else if (.equals("-f")) {
            ResourceCondition ncondition = new ResourceCondition();
            ncondition.type = 1;
        } else if (.equals("-s")) {
            ResourceCondition ncondition = new ResourceCondition();
            ncondition.type = 2;
        } else {
            PatternCondition condition = new PatternCondition();
            int flags = 0;
            if (isNocase()) {
                flags |= .;
            }
            condition.pattern = Pattern.compile(flags);
        }
    }
    
    public Matcher getMatcher() {
        Object condition = this..get();
        if (condition instanceof PatternCondition) {
            return ((PatternConditioncondition).
        }
        return null;
    }
    
    
String representation.
    public String toString() {
        // FIXME: Add flags if possible
        return "RewriteCond " +  + " " + ;
    }
    
    
    protected boolean positive = true;
    
    protected Substitution test = null;
    protected ThreadLocal<Conditioncondition = new ThreadLocal<Condition>();
    
    
This makes the test case-insensitive, i.e., there is no difference between 'A-Z' and 'a-z' both in the expanded TestString and the CondPattern. This flag is effective only for comparisons between TestString and CondPattern. It has no effect on filesystem and subrequest checks.
    public boolean nocase = false;
    
    
Use this to combine rule conditions with a local OR instead of the implicit AND.
    public boolean ornext = false;

    
Evaluate the condition based on the context

Parameters:
rule corresponding matched rule
cond last matched condition
Returns:
    public boolean evaluate(Matcher ruleMatcher condResolver resolver) {
        String value = .evaluate(rulecondresolver);
        if () {
            value = value.toLowerCase(.);
        }
        Condition condition = this..get();
        if (condition == null) {
            if (.startsWith("<")) {
                LexicalCondition ncondition = new LexicalCondition();
                ncondition.type = -1;
                ncondition.condition = .substring(1);
                condition = ncondition;
            } else if (.startsWith(">")) {
                LexicalCondition ncondition = new LexicalCondition();
                ncondition.type = 1;
                ncondition.condition = .substring(1);
                condition = ncondition;
            } else if (.startsWith("=")) {
                LexicalCondition ncondition = new LexicalCondition();
                ncondition.type = 0;
                ncondition.condition = .substring(1);
                condition = ncondition;
            } else if (.equals("-d")) {
                ResourceCondition ncondition = new ResourceCondition();
                ncondition.type = 0;
                condition = ncondition;
            } else if (.equals("-f")) {
                ResourceCondition ncondition = new ResourceCondition();
                ncondition.type = 1;
                condition = ncondition;
            } else if (.equals("-s")) {
                ResourceCondition ncondition = new ResourceCondition();
                ncondition.type = 2;
                condition = ncondition;
            } else {
                PatternCondition ncondition = new PatternCondition();
                int flags = 0;
                if (isNocase()) {
                    flags |= .;
                }
                ncondition.pattern = Pattern.compile(flags);
                condition = ncondition;
            }
            this..set(condition);
        }
        if () {
            return condition.evaluate(valueresolver);
        } else {
            return !condition.evaluate(valueresolver);
        }
    }
        
    public boolean isNocase() {
        return ;
    }
    public void setNocase(boolean nocase) {
        this. = nocase;
    }
    public boolean isOrnext() {
        return ;
    }
    public void setOrnext(boolean ornext) {
        this. = ornext;
    }
    public boolean isPositive() {
        return ;
    }
    public void setPositive(boolean positive) {
        this. = positive;
    }
New to GrepCode? Check out our FAQ X