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 | 0 | 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 | 0 | _webContext = webContext; |
49 | 0 | } |
50 | |
|
51 | |
public boolean assetExists(IComponentSpecification spec, Resource baseResource, String path, Locale locale) |
52 | |
{ |
53 | 0 | return findAsset(spec, baseResource, path, locale) != null; |
54 | |
} |
55 | |
|
56 | |
Resource findAsset(IComponentSpecification spec, Resource baseResource, String path, Locale locale) |
57 | |
{ |
58 | 0 | Resource assetResource = baseResource.getRelativeResource("/").getRelativeResource(path); |
59 | 0 | Resource localized = _localizer.findLocalization(assetResource, locale); |
60 | |
|
61 | 0 | if (localized == null) { |
62 | |
|
63 | 0 | assetResource = baseResource.getRelativeResource(path); |
64 | 0 | localized = _localizer.findLocalization(assetResource, locale); |
65 | |
} |
66 | |
|
67 | 0 | 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 | 0 | if (localized == null || localized.getResourceURL() == null) { |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | if (path != null && !path.startsWith("/")) |
82 | 0 | path = "/" + path; |
83 | |
|
84 | 0 | Resource base = new WebContextResource(_webContext, path); |
85 | 0 | localized = _localizer.findLocalization(base, locale); |
86 | |
} |
87 | |
|
88 | 0 | return localized; |
89 | |
} |
90 | |
|
91 | |
public IAsset createAsset(Resource baseResource, IComponentSpecification spec, String path, Locale locale, Location location) |
92 | |
{ |
93 | 0 | Resource localized = findAsset(spec, baseResource, path, locale); |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | 0 | if ( (localized == null || localized.getResourceURL() == null) |
100 | |
&& path.startsWith("/")) { |
101 | |
|
102 | 0 | return createAbsoluteAsset(path, locale, location); |
103 | |
} |
104 | |
|
105 | 0 | if (localized == null) |
106 | 0 | throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, baseResource), location, null); |
107 | |
|
108 | 0 | return createAsset(localized, location); |
109 | |
} |
110 | |
|
111 | |
public IAsset createAbsoluteAsset(String path, Locale locale, Location location) |
112 | |
{ |
113 | 0 | Resource base = new WebContextResource(_webContext, path); |
114 | 0 | Resource localized = _localizer.findLocalization(base, locale); |
115 | |
|
116 | 0 | if (localized == null) |
117 | 0 | throw new ApplicationRuntimeException(AssetMessages.missingContextResource(path), location, null); |
118 | |
|
119 | 0 | return createAsset(localized, location); |
120 | |
} |
121 | |
|
122 | |
public IAsset createAsset(Resource resource, Location location) |
123 | |
{ |
124 | 0 | return new ContextAsset(_contextPath, resource, location, _requestCycle); |
125 | |
} |
126 | |
|
127 | |
public void setContextPath(String contextPath) |
128 | |
{ |
129 | 0 | _contextPath = contextPath; |
130 | 0 | } |
131 | |
|
132 | |
public void setLocalizer(ResourceLocalizer localizer) |
133 | |
{ |
134 | 0 | _localizer = localizer; |
135 | 0 | } |
136 | |
|
137 | |
public void setRequestCycle(IRequestCycle cycle) |
138 | |
{ |
139 | 0 | _requestCycle = cycle; |
140 | 0 | } |
141 | |
} |