1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.Config; 33 import com.caucho.config.ConfigELContext; 34 import com.caucho.el.ELParser; 35 import com.caucho.el.Expr; 36 import com.caucho.el.MapVariableResolver; 37 import com.caucho.util.CharBuffer; 38 import com.caucho.util.L10N; 39 import com.caucho.vfs.Path; 40 import com.caucho.vfs.Vfs; 41 42 import javax.el.ELContext; 43 import javax.el.ELException; 44 import javax.el.ELResolver; 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 49 52 public class PathBuilder { 53 private static final L10N L = new L10N(PathBuilder.class); 54 55 private String _userPath; 56 57 60 public void addText(RawString text) 61 { 62 _userPath = text.getValue().trim(); 63 } 64 65 68 public Path replaceObject() 69 throws ELException 70 { 71 return lookupPath(_userPath, Config.getEnvironment()); 72 } 73 74 public static Path lookupPath(String string) 75 throws ELException 76 { 77 return lookupPath(string, Config.getEnvironment()); 78 } 79 80 public static Path lookupPath(String string, ArrayList vars) 81 throws ELException 82 { 83 HashMap <String ,Object > map = new HashMap <String ,Object >(); 84 map.put("regexp", vars); 85 86 return lookupPath(string, map); 87 } 88 89 public static Path lookupPath(String string, Map <String ,Object > map) 90 throws ELException 91 { 92 ELContext context = Config.getEnvironment(); 93 ELResolver parent = context != null ? context.getELResolver() : null; 94 ELResolver resolver; 95 96 if (map != null) 97 resolver = new MapVariableResolver(map); 98 else 99 resolver = parent; 100 101 return lookupPath(string, new ConfigELContext(resolver)); 102 } 103 104 public static Path lookupPath(String string, ELContext env) 105 throws ELException 106 { 107 return lookupPath(string, env, Vfs.lookup()); 108 } 109 110 public static Path lookupPath(String string, ELContext env, Path pwd) 111 throws ELException 112 { 113 if (env == null) 114 env = Config.getEnvironment(); 115 116 string = rewritePathString(string); 117 118 Expr expr = new ELParser(env, string).parse(); 119 120 Object obj = expr.evalObject(env); 121 122 if (obj instanceof Path) 123 return (Path) obj; 124 125 String value = Expr.toString(obj, env); 126 127 if (pwd != null) 128 return pwd.lookup(value); 129 else 130 return Vfs.lookup(value); 131 } 132 133 141 public static String rewritePathString(String pathName) 142 { 143 CharBuffer cb = CharBuffer.allocate(); 144 145 int length = pathName.length(); 146 for (int i = 0; i < length; i++) { 147 char ch = pathName.charAt(i); 148 149 if (ch != '$') { 150 cb.append(ch); 151 continue; 152 } 153 154 if (i + 1 == length) { 155 cb.append('$'); 156 continue; 157 } 158 159 ch = pathName.charAt(i + 1); 160 161 if ('0' <= ch && ch <= '9') { 162 int value = 0; 163 164 for (i++; 165 i < length && (ch = pathName.charAt(i)) >= '0' && ch <= '9'; 166 i++) { 167 value = 10 * value + ch - '0'; 168 } 169 170 i--; 171 172 cb.append("${regexp[" + value + "]}"); 173 } 174 else if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') { 175 int tail = i + 1; 176 for (; tail < length; tail++) { 177 ch = pathName.charAt(tail); 178 179 if (ch == '/' || ch == '\\' || ch == '$') 180 break; 181 } 182 183 cb.append("${Var[\"" + pathName.substring(i + 1, tail) + "\"]}"); 184 i = tail - 1; 185 } 186 else 187 cb.append('$'); 188 } 189 190 return cb.close(); 191 } 192 } 193 | Popular Tags |