001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config.types;
031:
032: import com.caucho.config.Config;
033: import com.caucho.config.ConfigELContext;
034: import com.caucho.el.*;
035: import com.caucho.util.L10N;
036: import com.caucho.vfs.Path;
037: import com.caucho.vfs.Vfs;
038:
039: import javax.el.ELContext;
040: import javax.el.ELException;
041: import javax.el.ELResolver;
042: import java.util.ArrayList;
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: /**
047: * Special builder for path variables.
048: */
049: public class PathBuilder {
050: private static final L10N L = new L10N(PathBuilder.class);
051:
052: private String _userPath;
053:
054: /**
055: * Sets the text.
056: */
057: public void addText(RawString text) {
058: _userPath = text.getValue().trim();
059: }
060:
061: /**
062: * Replace with the real path.
063: */
064: public Path replaceObject() throws ELException {
065: return lookupPath(_userPath, Config.getEnvironment());
066: }
067:
068: public static Path lookupPath(String string) throws ELException {
069: return lookupPath(string, Config.getEnvironment());
070: }
071:
072: public static Path lookupPath(String string, ArrayList vars)
073: throws ELException {
074: HashMap<String, Object> map = new HashMap<String, Object>();
075: map.put("regexp", vars);
076:
077: return lookupPath(string, map);
078: }
079:
080: public static Path lookupPath(String string, Map<String, Object> map)
081: throws ELException {
082: ELContext context;
083:
084: if (map != null) {
085: ELResolver resolver = new MapVariableResolver(map);
086: context = new ConfigELContext(resolver);
087: } else
088: context = new ConfigELContext();
089:
090: return lookupPath(string, context);
091: }
092:
093: public static Path lookupPath(String string, ELContext env)
094: throws ELException {
095: return lookupPath(string, env, Vfs.lookup());
096: }
097:
098: public static Path lookupPath(String string, ELContext env, Path pwd)
099: throws ELException {
100: if (env == null)
101: env = Config.getEnvironment();
102:
103: string = rewritePathString(string);
104:
105: Expr expr = new ELParser(env, string).parse();
106:
107: Object obj = expr.evalObject(env);
108:
109: if (obj instanceof Path)
110: return (Path) obj;
111:
112: String value = Expr.toString(obj, env);
113:
114: if (pwd != null)
115: return pwd.lookup(value);
116: else
117: return Vfs.lookup(value);
118: }
119:
120: /**
121: * Rewrites the path string into proper JSP EL.
122: *
123: * Returns the native path for a configured path name. The special cases
124: * $app-dir and $resin-home specify the root directory.
125: *
126: * @return a real path corresponding to the path name
127: */
128: public static String rewritePathString(String pathName) {
129: StringBuilder cb = new StringBuilder();
130:
131: int length = pathName.length();
132: for (int i = 0; i < length; i++) {
133: char ch = pathName.charAt(i);
134:
135: if (ch != '$') {
136: cb.append(ch);
137: continue;
138: }
139:
140: if (i + 1 == length) {
141: cb.append('$');
142: continue;
143: }
144:
145: ch = pathName.charAt(i + 1);
146:
147: if ('0' <= ch && ch <= '9') {
148: int value = 0;
149:
150: for (i++; i < length
151: && (ch = pathName.charAt(i)) >= '0'
152: && ch <= '9'; i++) {
153: value = 10 * value + ch - '0';
154: }
155:
156: i--;
157:
158: cb.append("${regexp[" + value + "]}");
159: } else if ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') {
160: int tail = i + 1;
161: for (; tail < length; tail++) {
162: ch = pathName.charAt(tail);
163:
164: if (ch == '/' || ch == '\\' || ch == '$')
165: break;
166: }
167: // cb.append("${Var[\"" + pathName.substring(i + 1, tail) + "\"]}");
168: cb.append("${" + pathName.substring(i + 1, tail) + "}");
169: i = tail - 1;
170: } else
171: cb.append('$');
172: }
173:
174: return cb.toString();
175: }
176: }
|