Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
BaseTagWriter |
|
| 3.0;3 |
1 | // Copyright 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // 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.services.impl; |
|
16 | ||
17 | import org.apache.tapestry.IMarkupWriter; |
|
18 | import org.apache.tapestry.IPage; |
|
19 | import org.apache.tapestry.IRender; |
|
20 | import org.apache.tapestry.IRequestCycle; |
|
21 | ||
22 | /** |
|
23 | * Contains code needed to render the <base> tag for pages. The <base> tag ensures that |
|
24 | * the base URL for the rendered page matches the location of the page template in the servlet |
|
25 | * context, so that relative URLs to static assets (images, stylesheets, etc.) will be processed |
|
26 | * correctly. This is important starting with release 4.0, where HTML templates are no longer |
|
27 | * restricted to the servlet root. |
|
28 | * <p> |
|
29 | * Note that pages outside of the application namespace (provided by the framework itself, or in a |
|
30 | * library) are "virtually located" in the application root. |
|
31 | * |
|
32 | * @author Howard M. Lewis Ship |
|
33 | * @since 4.0 |
|
34 | */ |
|
35 | 3 | public class BaseTagWriter implements IRender |
36 | { |
|
37 | ||
38 | public void render(IMarkupWriter writer, IRequestCycle cycle) |
|
39 | { |
|
40 | 3 | IPage page = cycle.getPage(); |
41 | ||
42 | 3 | StringBuffer sb = new StringBuffer(); |
43 | 3 | sb.append("/"); |
44 | ||
45 | 3 | if (page.getNamespace().getId() == null) |
46 | { |
|
47 | 1 | String name = page.getPageName(); |
48 | 1 | int slashx = name.lastIndexOf('/'); |
49 | ||
50 | // Include the directory and trailing slash. |
|
51 | 1 | if (slashx > 0) |
52 | 1 | sb.append(name.substring(0, slashx + 1)); |
53 | } |
|
54 | ||
55 | 3 | String url = cycle.getAbsoluteURL(sb.toString()); |
56 | ||
57 | 3 | writer.beginEmpty("base"); |
58 | 3 | writer.attribute("href", url); |
59 | 3 | writer.printRaw("<!--[if IE]></base><![endif]-->"); |
60 | ||
61 | 3 | writer.println(); |
62 | 3 | } |
63 | ||
64 | } |