1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.asset; |
16 |
| |
17 |
| import java.util.Locale; |
18 |
| |
19 |
| import org.apache.hivemind.ApplicationRuntimeException; |
20 |
| import org.apache.hivemind.Location; |
21 |
| import org.apache.hivemind.Resource; |
22 |
| import org.apache.tapestry.IAsset; |
23 |
| import org.apache.tapestry.l10n.ResourceLocalizer; |
24 |
| import org.apache.tapestry.web.WebContext; |
25 |
| import org.apache.tapestry.web.WebContextResource; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class ContextAssetFactory implements AssetFactory |
35 |
| { |
36 |
| private String _contextPath; |
37 |
| |
38 |
| private AssetFactory _classpathAssetFactory; |
39 |
| |
40 |
| private WebContext _webContext; |
41 |
| |
42 |
| private ResourceLocalizer _localizer; |
43 |
| |
44 |
8
| public void setWebContext(WebContext webContext)
|
45 |
| { |
46 |
8
| _webContext = webContext;
|
47 |
| } |
48 |
| |
49 |
4
| public IAsset createAsset(Resource baseResource, String path, Locale locale, Location location)
|
50 |
| { |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
4
| Resource assetResource = baseResource.getRelativeResource(path);
|
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
4
| if (assetResource.getResourceURL() == null && path.startsWith("/"))
|
63 |
2
| return _classpathAssetFactory.createAbsoluteAsset(path, locale, location);
|
64 |
| |
65 |
2
| Resource localized = _localizer.findLocalization(assetResource, locale);
|
66 |
| |
67 |
2
| if (localized == null)
|
68 |
1
| throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource),
|
69 |
| location, null); |
70 |
| |
71 |
1
| return createAsset(localized, location);
|
72 |
| } |
73 |
| |
74 |
8
| public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
|
75 |
| { |
76 |
8
| Resource base = new WebContextResource(_webContext, path);
|
77 |
8
| Resource localized = _localizer.findLocalization(base, locale);
|
78 |
| |
79 |
8
| if (localized == null)
|
80 |
1
| throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path),
|
81 |
| location, null); |
82 |
| |
83 |
7
| return createAsset(localized, location);
|
84 |
| } |
85 |
| |
86 |
8
| public IAsset createAsset(Resource resource, Location location)
|
87 |
| { |
88 |
8
| return new ContextAsset(_contextPath, resource, location);
|
89 |
| } |
90 |
| |
91 |
11
| public void setContextPath(String contextPath)
|
92 |
| { |
93 |
11
| _contextPath = contextPath;
|
94 |
| } |
95 |
| |
96 |
7
| public void setClasspathAssetFactory(AssetFactory classpathAssetFactory)
|
97 |
| { |
98 |
7
| _classpathAssetFactory = classpathAssetFactory;
|
99 |
| } |
100 |
| |
101 |
10
| public void setLocalizer(ResourceLocalizer localizer)
|
102 |
| { |
103 |
10
| _localizer = localizer;
|
104 |
| } |
105 |
| } |