1 |
|
package org.apache.tapestry.resolver; |
2 |
|
|
3 |
|
import org.apache.hivemind.Resource; |
4 |
|
import org.apache.hivemind.util.ContextResource; |
5 |
|
import org.apache.tapestry.IComponent; |
6 |
|
import org.apache.tapestry.IRequestCycle; |
7 |
|
import org.apache.tapestry.TapestryUtils; |
8 |
|
import org.apache.tapestry.asset.AssetFactory; |
9 |
|
import org.apache.tapestry.web.WebContextResource; |
10 |
|
|
11 |
|
import java.util.Locale; |
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
27 |
public class ComponentResourceResolverImpl implements IComponentResourceResolver { |
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
private static final String WEB_INF = "WEB-INF/"; |
22 |
|
|
23 |
|
private AssetFactory _classpathAssetFactory; |
24 |
|
private AssetFactory _contextAssetFactory; |
25 |
|
|
26 |
|
|
27 |
|
private String _applicationId; |
28 |
|
|
29 |
|
|
30 |
|
private Resource _contextRoot; |
31 |
|
|
32 |
|
private Resource _webInfLocation; |
33 |
|
|
34 |
|
private Resource _webInfAppLocation; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public void initializeService() |
40 |
|
{ |
41 |
4 |
_webInfLocation = _contextRoot.getRelativeResource(WEB_INF); |
42 |
|
|
43 |
4 |
_webInfAppLocation = _webInfLocation.getRelativeResource(_applicationId + "/"); |
44 |
4 |
} |
45 |
|
|
46 |
|
public Resource findComponentResource(IComponent component, IRequestCycle cycle, String path, String extension, Locale locale) |
47 |
|
{ |
48 |
69 |
Resource base = component.getSpecification().getSpecificationLocation(); |
49 |
69 |
String baseName = path == null ? extractBaseName(base) : path; |
50 |
69 |
Resource resource = null; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
71 |
if (WebContextResource.class.isInstance(base) || ContextResource.class.isInstance(base)) |
58 |
|
{ |
59 |
2 |
resource = base.getRelativeResource(baseName + extension); |
60 |
|
|
61 |
2 |
if (resource != null) |
62 |
2 |
return localizeResource(resource, locale); |
63 |
|
} |
64 |
|
|
65 |
67 |
resource = findComponentClassResource(component, cycle, baseName, extension, locale); |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
67 |
if (resource == null) |
70 |
|
{ |
71 |
63 |
resource = base.getRelativeResource(baseName + extension); |
72 |
|
|
73 |
63 |
if (resource != null) |
74 |
63 |
resource = localizeResource(resource, locale); |
75 |
|
} |
76 |
|
|
77 |
67 |
return resource; |
78 |
|
} |
79 |
|
|
80 |
|
String extractBaseName(Resource baseResourceLocation) |
81 |
|
{ |
82 |
6 |
String fileName = baseResourceLocation.getName(); |
83 |
6 |
int dotx = fileName.lastIndexOf('.'); |
84 |
|
|
85 |
6 |
return dotx > -1 ? fileName.substring(0, dotx) : fileName; |
86 |
|
} |
87 |
|
|
88 |
|
Resource localizeResource(Resource resource, Locale locale) |
89 |
|
{ |
90 |
65 |
if (locale == null) |
91 |
64 |
return resource; |
92 |
|
|
93 |
1 |
Resource localized = resource.getLocalization(locale); |
94 |
1 |
if (localized != null && localized.getResourceURL() != null) |
95 |
1 |
return localized; |
96 |
|
|
97 |
0 |
return resource; |
98 |
|
} |
99 |
|
|
100 |
|
Resource findComponentClassResource(IComponent component, IRequestCycle cycle, String baseName, String extension, Locale locale) |
101 |
|
{ |
102 |
67 |
Resource base = component.getSpecification().getSpecificationLocation(); |
103 |
67 |
String componentPackages = component.getNamespace().getPropertyValue("org.apache.tapestry.component-class-packages"); |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
67 |
if (componentPackages == null) |
108 |
63 |
return null; |
109 |
|
|
110 |
4 |
String className = component.getSpecification().getComponentClassName(); |
111 |
4 |
if (className == null) |
112 |
0 |
return null; |
113 |
|
|
114 |
4 |
String[] packages = TapestryUtils.split(componentPackages); |
115 |
4 |
for (int i=0; i < packages.length; i++) |
116 |
|
{ |
117 |
|
|
118 |
4 |
int index = className.lastIndexOf(packages[i]); |
119 |
4 |
if (index < 0) |
120 |
0 |
continue; |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
4 |
String templateName = className.substring((index + packages[i].length()) + 1, className.length()).replaceAll("\\.", "/"); |
125 |
4 |
templateName = templateName + extension; |
126 |
|
|
127 |
4 |
if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfAppLocation, templateName, locale)) |
128 |
|
{ |
129 |
1 |
return _contextAssetFactory.createAsset(_webInfAppLocation, component.getSpecification(), templateName, locale, component.getLocation()).getResourceLocation(); |
130 |
3 |
} else if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfLocation, templateName, locale)) |
131 |
|
{ |
132 |
1 |
return _contextAssetFactory.createAsset(_webInfLocation, component.getSpecification(), templateName, locale, component.getLocation()).getResourceLocation(); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
2 |
String resourceName = baseName + extension; |
138 |
|
|
139 |
2 |
if (_classpathAssetFactory.assetExists(component.getSpecification(), base, resourceName, locale)) |
140 |
|
{ |
141 |
1 |
return _classpathAssetFactory.createAsset(base, component.getSpecification(), resourceName, locale, component.getLocation()).getResourceLocation(); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
1 |
String[] packageSegments = packages[i].split("\\."); |
147 |
|
|
148 |
1 |
if (packageSegments != null && packageSegments.length > 0) |
149 |
|
{ |
150 |
|
|
151 |
1 |
String packagePath = ""; |
152 |
|
|
153 |
1 |
for (int s=packageSegments.length - 1; s > -1; s--) |
154 |
|
{ |
155 |
1 |
packagePath += packageSegments[s] + "/"; |
156 |
|
|
157 |
1 |
String templatePath = packagePath + templateName; |
158 |
|
|
159 |
1 |
if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfAppLocation, templatePath, locale)) |
160 |
|
{ |
161 |
0 |
return _contextAssetFactory.createAsset(_webInfAppLocation, component.getSpecification(), templatePath, locale, component.getLocation()).getResourceLocation(); |
162 |
1 |
} else if (_contextAssetFactory.assetExists(component.getSpecification(), _webInfLocation, templatePath, locale)) |
163 |
|
{ |
164 |
1 |
return _contextAssetFactory.createAsset(_webInfLocation, component.getSpecification(), templatePath, locale, component.getLocation()).getResourceLocation(); |
165 |
|
} |
166 |
|
} |
167 |
|
} |
168 |
|
} |
169 |
|
|
170 |
0 |
return null; |
171 |
|
} |
172 |
|
|
173 |
|
public void setContextRoot(Resource contextRoot) |
174 |
|
{ |
175 |
4 |
_contextRoot = contextRoot; |
176 |
4 |
} |
177 |
|
|
178 |
|
public void setClasspathAssetFactory(AssetFactory classpathAssetFactory) |
179 |
|
{ |
180 |
4 |
_classpathAssetFactory = classpathAssetFactory; |
181 |
4 |
} |
182 |
|
|
183 |
|
public void setContextAssetFactory(AssetFactory contextAssetFactory) |
184 |
|
{ |
185 |
4 |
_contextAssetFactory = contextAssetFactory; |
186 |
4 |
} |
187 |
|
|
188 |
|
public void setApplicationId(String applicationId) |
189 |
|
{ |
190 |
4 |
_applicationId = applicationId; |
191 |
4 |
} |
192 |
|
} |