1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.Config; 33 import com.caucho.config.NodeBuilder; 34 import com.caucho.config.TypeStrategy; 35 import com.caucho.el.ELParser; 36 import com.caucho.el.Expr; 37 import com.caucho.util.L10N; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.Vfs; 40 41 import org.w3c.dom.Node ; 42 43 import javax.el.ELContext; 44 import javax.el.ELException; 45 46 public class PathTypeStrategy extends TypeStrategy { 47 protected static final L10N L = new L10N(PathTypeStrategy.class); 48 49 56 public Object configure(NodeBuilder builder, Node node, Object parent) 57 throws Exception 58 { 59 String userPath = builder.configureRawString(node); 60 61 return lookupPath(userPath); 62 } 63 64 public static Path lookupPath(String string) 65 throws ELException 66 { 67 return lookupPath(string, 68 Config.getEnvironment(), 69 Vfs.lookup()); 70 } 71 72 public static Path lookupPath(String string, ELContext env) 73 throws ELException 74 { 75 return lookupPath(string, env, Vfs.lookup()); 76 } 77 78 public static Path lookupPath(String string, ELContext env, Path pwd) 79 throws ELException 80 { 81 if (env == null) 82 env = Config.getEnvironment(); 83 84 string = rewritePathString(string); 85 86 Expr expr = new ELParser(env, string).parse(); 87 88 Object obj = expr.evalObject(env); 89 90 if (obj == null) 91 throw new NullPointerException (L.l("Path '{0}' evaluated to null.", 92 string)); 93 if (obj instanceof Path) 94 return (Path) obj; 95 96 String value = Expr.toString(obj, env); 97 98 if (pwd != null) 99 return pwd.lookup(value); 100 else 101 return Vfs.lookup(value); 102 } 103 104 112 public static String rewritePathString(String pathName) 113 { 114 if (pathName == null) 115 return "."; 116 117 StringBuilder cb = new StringBuilder (); 118 119 int length = pathName.length(); 120 for (int i = 0; i < length; i++) { 121 char ch = pathName.charAt(i); 122 123 if (ch != '$') { 124 cb.append(ch); 125 continue; 126 } 127 128 if (i + 1 == length) { 129 cb.append('$'); 130 continue; 131 } 132 133 ch = pathName.charAt(i + 1); 134 135 if ('0' <= ch && ch <= '9') { 136 int value = 0; 137 138 for (i++; 139 i < length && (ch = pathName.charAt(i)) >= '0' && ch <= '9'; 140 i++) { 141 value = 10 * value + ch - '0'; 142 } 143 144 i--; 145 146 cb.append("${regexp[" + value + "]}"); 147 } 148 else if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') { 149 int tail = i + 1; 150 for (; tail < length; tail++) { 151 ch = pathName.charAt(tail); 152 153 if (ch == '/' || ch == '\\' || ch == '$') 154 break; 155 } 156 157 cb.append("${" + pathName.substring(i + 1, tail) + "}"); 158 i = tail - 1; 159 } 160 else 161 cb.append('$'); 162 } 163 164 return cb.toString(); 165 } 166 } 167 | Popular Tags |