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