01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import static java.lang.String.format;
18:
19: import java.util.Locale;
20:
21: import org.apache.tapestry.internal.InternalConstants;
22: import org.apache.tapestry.ioc.Resource;
23: import org.apache.tapestry.ioc.internal.util.InternalUtils;
24: import org.apache.tapestry.model.ComponentModel;
25: import org.apache.tapestry.services.ComponentClassResolver;
26:
27: public class PageTemplateLocatorImpl implements PageTemplateLocator {
28: private final Resource _contextRoot;
29:
30: private final ComponentClassResolver _resolver;
31:
32: public PageTemplateLocatorImpl(Resource contextRoot,
33: ComponentClassResolver resolver) {
34: _contextRoot = contextRoot;
35: _resolver = resolver;
36: }
37:
38: public Resource findPageTemplateResource(ComponentModel model,
39: Locale locale) {
40: String className = model.getComponentClassName();
41:
42: // A bit of a hack, but should work.
43:
44: if (!className.contains(".pages."))
45: return null;
46:
47: String logicalName = _resolver
48: .resolvePageClassNameToPageName(className);
49:
50: int slashx = logicalName.lastIndexOf('/');
51:
52: if (slashx > 0) {
53: // However, the logical name isn't quite what we want. It may have been somewhat
54: // trimmed.
55:
56: String simpleClassName = InternalUtils.lastTerm(className);
57:
58: logicalName = logicalName.substring(0, slashx + 1)
59: + simpleClassName;
60: }
61:
62: String path = format("WEB-INF/%s.%s", logicalName,
63: InternalConstants.TEMPLATE_EXTENSION);
64:
65: return _contextRoot.forFile(path).forLocale(locale);
66: }
67:
68: }
|