|
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.util.Locale; |
|
19 |
| |
|
20 |
| import javax.servlet.ServletContext; |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class LocalizedContextResourceFinder |
|
33 |
| { |
|
34 |
| private ServletContext _context; |
|
35 |
| |
|
36 |
7
| public LocalizedContextResourceFinder(ServletContext context)
|
|
37 |
| { |
|
38 |
7
| _context = context;
|
|
39 |
| } |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
7
| public LocalizedResource resolve(String contextPath, Locale locale)
|
|
50 |
| { |
|
51 |
7
| int dotx = contextPath.lastIndexOf('.');
|
|
52 |
7
| String basePath;
|
|
53 |
7
| String suffix;
|
|
54 |
7
| if (dotx >= 0) {
|
|
55 |
6
| basePath = contextPath.substring(0, dotx);
|
|
56 |
6
| suffix = contextPath.substring(dotx);
|
|
57 |
| } |
|
58 |
| else |
|
59 |
| { |
|
60 |
| |
|
61 |
1
| basePath = contextPath;
|
|
62 |
1
| suffix = "";
|
|
63 |
| } |
|
64 |
| |
|
65 |
7
| LocalizedNameGenerator generator = new LocalizedNameGenerator(basePath, locale, suffix);
|
|
66 |
| |
|
67 |
7
| while (generator.more())
|
|
68 |
| { |
|
69 |
12
| String candidatePath = generator.next();
|
|
70 |
| |
|
71 |
12
| if (isExistingResource(candidatePath))
|
|
72 |
4
| return new LocalizedResource(candidatePath, generator.getCurrentLocale());
|
|
73 |
| } |
|
74 |
| |
|
75 |
3
| return null;
|
|
76 |
| } |
|
77 |
| |
|
78 |
12
| private boolean isExistingResource(String path)
|
|
79 |
| { |
|
80 |
12
| try
|
|
81 |
| { |
|
82 |
12
| return _context.getResource(path) != null;
|
|
83 |
| } |
|
84 |
| catch (MalformedURLException ex) |
|
85 |
| { |
|
86 |
2
| return false;
|
|
87 |
| } |
|
88 |
| } |
|
89 |
| } |