KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > BaseTagWriter


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 public class BaseTagWriter implements IRender
36 {
37
38     public void render(IMarkupWriter writer, IRequestCycle cycle)
39     {
40         IPage page = cycle.getPage();
41
42         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
43         sb.append("/");
44
45         if (page.getNamespace().getId() == null)
46         {
47             String JavaDoc name = page.getPageName();
48             int slashx = name.lastIndexOf('/');
49
50             // Include the directory and trailing slash.
51
if (slashx > 0)
52                 sb.append(name.substring(0, slashx + 1));
53         }
54
55         String JavaDoc url = cycle.getAbsoluteURL(sb.toString());
56
57         writer.beginEmpty("base");
58         writer.attribute("href", url);
59
60         writer.println();
61     }
62
63 }
Popular Tags