1 28 29 package com.caucho.config.types; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.log.Log; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.util.L10N; 35 36 import javax.annotation.PostConstruct; 37 import java.util.logging.Logger ; 38 import java.util.regex.Pattern ; 39 import java.util.regex.PatternSyntaxException ; 40 41 44 public class PathPatternType { 45 static final L10N L = new L10N(PathPatternType.class); 46 static final Logger log = Log.open(PathPatternType.class); 47 48 private Pattern _pattern; 49 50 public PathPatternType() 51 { 52 } 53 54 public PathPatternType(String pattern) 55 throws ConfigException, PatternSyntaxException 56 { 57 setName(pattern); 58 } 59 60 63 public void setName(String pattern) 64 throws ConfigException, PatternSyntaxException 65 { 66 CharBuffer cb = new CharBuffer(); 67 68 cb.append("^"); 69 70 int i = 0; 71 int length = pattern.length(); 72 73 while (i < length && pattern.charAt(i) == '/') 74 i++; 75 76 for (; i < length; i++) { 77 char ch = pattern.charAt(i); 78 79 if (ch == '/') 80 cb.append('/'); 81 else if (ch != '*') 82 cb.append(ch); 83 else if (length <= i + 1 || pattern.charAt(i + 1) != '*') 84 cb.append("[^/]*"); 85 else if (i > 0 && pattern.charAt(i - 1) != '/') 86 throw new ConfigException(L.l("'{0}' is an invalid pattern at '**'", 87 pattern)); 88 else if (i + 2 < length && pattern.charAt(i + 2) == '/') { 89 cb.append("([^/]*/)*"); 90 i += 2; 91 } 92 else if (i + 2 < length) 93 throw new ConfigException(L.l("'{0}' is an invalid pattern at '**'", 94 pattern)); 95 else { 96 cb.append(".*"); 97 i++; 98 } 99 } 100 101 cb.append("$"); 102 103 _pattern = Pattern.compile(cb.toString()); 104 } 105 106 109 public void addText(String text) 110 throws ConfigException, PatternSyntaxException 111 { 112 text = text.trim(); 113 114 if (! text.equals("")) 115 setName(text); 116 } 117 118 121 @PostConstruct 122 public void init() 123 throws ConfigException 124 { 125 if (_pattern == null) 126 throw new ConfigException(L.l("pattern requires 'name' attribute.")); 127 } 128 129 132 public boolean isMatch(String path) 133 { 134 return _pattern.matcher(path).matches(); 135 } 136 137 public String toString() 138 { 139 return "PathPatternType[" + _pattern.pattern() + "]"; 140 } 141 } 142 | Popular Tags |