KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
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   /**
50    * Returns the type's configured value
51    *
52    * @param builder the context builder
53    * @param node the configuration node
54    * @param parent
55    */

56   public Object JavaDoc configure(NodeBuilder builder, Node JavaDoc node, Object JavaDoc parent)
57     throws Exception JavaDoc
58   {
59     String JavaDoc userPath = builder.configureRawString(node);
60
61     return lookupPath(userPath);
62   }
63
64   public static Path lookupPath(String JavaDoc string)
65     throws ELException
66   {
67     return lookupPath(string,
68               Config.getEnvironment(),
69               Vfs.lookup());
70   }
71
72   public static Path lookupPath(String JavaDoc string, ELContext env)
73     throws ELException
74   {
75     return lookupPath(string, env, Vfs.lookup());
76   }
77
78   public static Path lookupPath(String JavaDoc 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 JavaDoc obj = expr.evalObject(env);
89
90     if (obj == null)
91       throw new NullPointerException JavaDoc(L.l("Path '{0}' evaluated to null.",
92                      string));
93     if (obj instanceof Path)
94       return (Path) obj;
95
96     String JavaDoc 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   /**
105    * Rewrites the path string into proper JSP EL.
106    *
107    * Returns the native path for a configured path name. The special cases
108    * $app-dir and $resin-home specify the root directory.
109    *
110    * @return a real path corresponding to the path name
111    */

112   public static String JavaDoc rewritePathString(String JavaDoc pathName)
113   {
114     if (pathName == null)
115       return ".";
116     
117     StringBuilder JavaDoc cb = new StringBuilder JavaDoc();
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