1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.util; |
16 |
| |
17 |
| import java.net.MalformedURLException; |
18 |
| import java.net.URL; |
19 |
| import java.util.Locale; |
20 |
| |
21 |
| import javax.servlet.ServletContext; |
22 |
| |
23 |
| import org.apache.commons.logging.Log; |
24 |
| import org.apache.commons.logging.LogFactory; |
25 |
| import org.apache.hivemind.Resource; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public class ContextResource extends AbstractResource |
38 |
| { |
39 |
| private static final Log LOG = LogFactory.getLog(ContextResource.class); |
40 |
| |
41 |
| private ServletContext _context; |
42 |
| |
43 |
9
| public ContextResource(ServletContext context, String path)
|
44 |
| { |
45 |
9
| this(context, path, null);
|
46 |
| } |
47 |
| |
48 |
11
| public ContextResource(ServletContext context, String path, Locale locale)
|
49 |
| { |
50 |
11
| super(path, locale);
|
51 |
| |
52 |
11
| _context = context;
|
53 |
| } |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
3
| public Resource getLocalization(Locale locale)
|
61 |
| { |
62 |
3
| LocalizedContextResourceFinder finder = new LocalizedContextResourceFinder(_context);
|
63 |
| |
64 |
3
| String path = getPath();
|
65 |
3
| LocalizedResource localizedResource = finder.resolve(path, locale);
|
66 |
| |
67 |
3
| if (localizedResource == null)
|
68 |
1
| return null;
|
69 |
| |
70 |
2
| String localizedPath = localizedResource.getResourcePath();
|
71 |
2
| Locale pathLocale = localizedResource.getResourceLocale();
|
72 |
| |
73 |
2
| if (localizedPath == null)
|
74 |
0
| return null;
|
75 |
| |
76 |
2
| if (path.equals(localizedPath))
|
77 |
1
| return this;
|
78 |
| |
79 |
1
| return new ContextResource(_context, localizedPath, pathLocale);
|
80 |
| } |
81 |
| |
82 |
5
| public URL getResourceURL()
|
83 |
| { |
84 |
5
| try
|
85 |
| { |
86 |
5
| return _context.getResource(getPath());
|
87 |
| } |
88 |
| catch (MalformedURLException ex) |
89 |
| { |
90 |
0
| LOG.warn(UtilMessages.unableToReferenceContextPath(getPath(), ex), ex);
|
91 |
| |
92 |
0
| return null;
|
93 |
| } |
94 |
| } |
95 |
| |
96 |
1
| public String toString()
|
97 |
| { |
98 |
1
| return "context:" + getPath();
|
99 |
| } |
100 |
| |
101 |
0
| public int hashCode()
|
102 |
| { |
103 |
0
| return 4197 & getPath().hashCode();
|
104 |
| } |
105 |
| |
106 |
1
| protected Resource newResource(String path)
|
107 |
| { |
108 |
1
| return new ContextResource(_context, path);
|
109 |
| } |
110 |
| |
111 |
| } |