1 19 20 package com.sslexplorer.core.stringreplacement; 21 22 import java.util.regex.Matcher ; 23 import java.util.regex.Pattern ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import com.sslexplorer.boot.Replacer; 29 import com.sslexplorer.boot.Util; 30 31 public abstract class AbstractReplacementVariableReplacer implements Replacer { 32 33 public final static Log log = LogFactory.getLog(AbstractReplacementVariableReplacer.class); 34 35 public abstract String processReplacementVariable(Pattern pattern, Matcher matcher, String replacementPattern, String type, String key) throws Exception ; 36 37 public String getReplacement(Pattern pattern, Matcher matcher, String replacementPattern) { 38 String match = matcher.group(); 39 String key = match.substring(2, match.length() - 1); 40 try { 41 int idx = key.indexOf(":"); 43 if (idx == -1) { 44 throw new Exception ("String replacement pattern is in incorrect format for " + key 45 + ". Must be <TYPE>:<key>"); 46 } 47 String type = key.substring(0, idx); 48 key = key.substring(idx + 1); 49 if (log.isDebugEnabled()) 50 log.debug("Found replacement variable " + type + ":" + key); 51 52 char enc = 'p'; 54 if(type.startsWith("[")) { 55 enc = type.charAt(1); 56 type = type.substring(3); 57 } 58 String val = processReplacementVariable(pattern, matcher, replacementPattern, type, key); 59 if(val != null) { 60 switch(enc) { 61 case 'u': 62 val = Util.urlEncode(val); 64 break; 65 case 'p': 66 break; 68 default: 69 throw new Exception ("Invalid encoding '" + enc + "'"); 70 } 71 } 72 return val; 73 } catch (Exception e) { 74 log.error("A replacement failed for " + key + ".", e); 75 } 76 return null; 77 } 78 79 } 80 | Popular Tags |