1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.asset; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.Location; |
19 |
|
import org.apache.hivemind.Resource; |
20 |
|
import org.apache.tapestry.IAsset; |
21 |
|
import org.apache.tapestry.IRequestCycle; |
22 |
|
import org.apache.tapestry.l10n.ResourceLocalizer; |
23 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
24 |
|
import org.apache.tapestry.web.WebContext; |
25 |
|
import org.apache.tapestry.web.WebContextResource; |
26 |
|
|
27 |
|
import java.util.Locale; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
6 |
public class ContextAssetFactory implements AssetFactory |
37 |
|
{ |
38 |
|
private String _contextPath; |
39 |
|
|
40 |
|
private WebContext _webContext; |
41 |
|
|
42 |
|
private ResourceLocalizer _localizer; |
43 |
|
|
44 |
|
private IRequestCycle _requestCycle; |
45 |
|
|
46 |
|
public void setWebContext(WebContext webContext) |
47 |
|
{ |
48 |
4 |
_webContext = webContext; |
49 |
4 |
} |
50 |
|
|
51 |
|
public boolean assetExists(IComponentSpecification spec, Resource baseResource, String path, Locale locale) |
52 |
|
{ |
53 |
1 |
return findAsset(spec, baseResource, path, locale) != null; |
54 |
|
} |
55 |
|
|
56 |
|
Resource findAsset(IComponentSpecification spec, Resource baseResource, String path, Locale locale) |
57 |
|
{ |
58 |
3 |
Resource assetResource = baseResource.getRelativeResource("/").getRelativeResource(path); |
59 |
3 |
Resource localized = _localizer.findLocalization(assetResource, locale); |
60 |
|
|
61 |
3 |
if (localized == null) { |
62 |
|
|
63 |
1 |
assetResource = baseResource.getRelativeResource(path); |
64 |
1 |
localized = _localizer.findLocalization(assetResource, locale); |
65 |
|
} |
66 |
|
|
67 |
3 |
if (localized == null && spec != null && spec.getLocation().getResource() != null) { |
68 |
|
|
69 |
|
|
70 |
0 |
assetResource = spec.getLocation().getResource().getRelativeResource("/").getRelativeResource(path); |
71 |
|
|
72 |
0 |
localized = _localizer.findLocalization(assetResource, locale); |
73 |
|
} |
74 |
|
|
75 |
3 |
if (localized == null || localized.getResourceURL() == null) { |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
1 |
if (path != null && !path.startsWith("/")) |
82 |
1 |
path = "/" + path; |
83 |
|
|
84 |
1 |
Resource base = new WebContextResource(_webContext, path); |
85 |
1 |
localized = _localizer.findLocalization(base, locale); |
86 |
|
} |
87 |
|
|
88 |
3 |
return localized; |
89 |
|
} |
90 |
|
|
91 |
|
public IAsset createAsset(Resource baseResource, IComponentSpecification spec, String path, Locale locale, Location location) |
92 |
|
{ |
93 |
2 |
Resource localized = findAsset(spec, baseResource, path, locale); |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
2 |
if ( (localized == null || localized.getResourceURL() == null) |
100 |
|
&& path.startsWith("/")) { |
101 |
|
|
102 |
0 |
return createAbsoluteAsset(path, locale, location); |
103 |
|
} |
104 |
|
|
105 |
2 |
if (localized == null) |
106 |
1 |
throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource), location, null); |
107 |
|
|
108 |
1 |
return createAsset(localized, location); |
109 |
|
} |
110 |
|
|
111 |
|
public IAsset createAbsoluteAsset(String path, Locale locale, Location location) |
112 |
|
{ |
113 |
3 |
Resource base = new WebContextResource(_webContext, path); |
114 |
3 |
Resource localized = _localizer.findLocalization(base, locale); |
115 |
|
|
116 |
3 |
if (localized == null) |
117 |
1 |
throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path), location, null); |
118 |
|
|
119 |
2 |
return createAsset(localized, location); |
120 |
|
} |
121 |
|
|
122 |
|
public IAsset createAsset(Resource resource, Location location) |
123 |
|
{ |
124 |
3 |
return new ContextAsset(_contextPath, resource, location, _requestCycle); |
125 |
|
} |
126 |
|
|
127 |
|
public void setContextPath(String contextPath) |
128 |
|
{ |
129 |
6 |
_contextPath = contextPath; |
130 |
6 |
} |
131 |
|
|
132 |
|
public void setLocalizer(ResourceLocalizer localizer) |
133 |
|
{ |
134 |
6 |
_localizer = localizer; |
135 |
6 |
} |
136 |
|
|
137 |
|
public void setRequestCycle(IRequestCycle cycle) |
138 |
|
{ |
139 |
1 |
_requestCycle = cycle; |
140 |
1 |
} |
141 |
|
} |