1 package org.apache.turbine.util;
2
3
4 import java.io.File;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import java.util.StringTokenizer;
27
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletContext;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.turbine.Turbine;
33
34
35
36
37
38
39
40
41 public class ServletUtils
42 {
43
44
45
46
47
48
49
50
51
52
53 public static String expandRelative(ServletConfig config,
54 String text)
55 {
56 if (StringUtils.isEmpty(text))
57 {
58 return text;
59 }
60
61 if (config == null)
62 {
63 return null;
64 }
65
66 final String expandedText;
67
68
69 if (!text.startsWith("/") && !text.startsWith("./")
70 && !text.startsWith("\\") && !text.startsWith(".\\"))
71 {
72 StringBuilder sb = new StringBuilder();
73 sb.append("./");
74 sb.append(text);
75 expandedText = sb.toString();
76 }
77 else
78 {
79 expandedText = text;
80 }
81
82 ServletContext context = config.getServletContext();
83 String base = StringUtils.defaultIfEmpty(context.getRealPath("/"),
84 Turbine.getApplicationRoot());
85
86 if (StringUtils.isEmpty(base))
87 {
88 return expandedText;
89 }
90
91 StringTokenizer tokenizer = new StringTokenizer(expandedText, File.pathSeparator);
92 StringBuilder buffer = new StringBuilder();
93 while (tokenizer.hasMoreTokens())
94 {
95 buffer.append(base).append(tokenizer.nextToken());
96 if (tokenizer.hasMoreTokens())
97 {
98 buffer.append(File.pathSeparator);
99 }
100 }
101 return buffer.toString();
102 }
103 }