KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > PathBuilder


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * Special builder for path variables.
51  */

52 public class PathBuilder {
53   private static final L10N L = new L10N(PathBuilder.class);
54
55   private String JavaDoc _userPath;
56
57   /**
58    * Sets the text.
59    */

60   public void addText(RawString text)
61   {
62     _userPath = text.getValue().trim();
63   }
64
65   /**
66    * Replace with the real path.
67    */

68   public Path replaceObject()
69     throws ELException
70   {
71     return lookupPath(_userPath, Config.getEnvironment());
72   }
73
74   public static Path lookupPath(String JavaDoc string)
75     throws ELException
76   {
77     return lookupPath(string, Config.getEnvironment());
78   }
79
80   public static Path lookupPath(String JavaDoc string, ArrayList JavaDoc vars)
81     throws ELException
82   {
83     HashMap JavaDoc<String JavaDoc,Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
84     map.put("regexp", vars);
85     
86     return lookupPath(string, map);
87   }
88
89   public static Path lookupPath(String JavaDoc string, Map JavaDoc<String JavaDoc,Object JavaDoc> 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 JavaDoc string, ELContext env)
105     throws ELException
106   {
107     return lookupPath(string, env, Vfs.lookup());
108   }
109
110   public static Path lookupPath(String JavaDoc 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 JavaDoc obj = expr.evalObject(env);
121
122     if (obj instanceof Path)
123       return (Path) obj;
124
125     String JavaDoc 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   /**
134    * Rewrites the path string into proper JSP EL.
135    *
136    * Returns the native path for a configured path name. The special cases
137    * $app-dir and $resin-home specify the root directory.
138    *
139    * @return a real path corresponding to the path name
140    */

141   public static String JavaDoc rewritePathString(String JavaDoc 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