KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > ContentURI


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.lang.reflect.Method JavaDoc;
20
21 import org.apache.turbine.services.pull.ApplicationTool;
22
23 /**
24  * Utility class to allow the easy inclusion of
25  * images in templates: <img SRC="$content.getURI("image.jpg")">
26  *
27  * @author <a HREF="mailto:criley@ekmail.com">Cameron Riley</a>
28  * @author <a HREF="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
29  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
30  * @version $Id: ContentURI.java,v 1.4.2.2 2004/05/20 03:16:38 seade Exp $
31  * @deprecated Use {@link org.apache.turbine.services.pull.tools.ContentTool} for tool usage
32  * and {@link org.apache.turbine.util.uri.DataURI} for code usage instead.
33  */

34 public class ContentURI
35         extends DynamicURI
36         implements ApplicationTool
37 {
38     /** stores the context path for servlet 2.1+ compliant containers */
39     private String JavaDoc contextPath;
40
41     /**
42      * Constructor
43      *
44      * @param data a RunData instance
45      */

46     public ContentURI(RunData data)
47     {
48         super(data);
49         init(data);
50     }
51
52     /**
53      * Default constructor
54      */

55     public ContentURI()
56     {
57     }
58
59     /**
60      * Initialize this object using the data given (ApplicationTool
61      * method).
62      *
63      * @param data assumed to be a RunData instance
64      */

65     public void init(Object JavaDoc data)
66     {
67         // we blithely cast to RunData as the runtime error thrown
68
// if data is null or another type is appropriate.
69
init((RunData) data);
70     }
71
72     /**
73      * Refresh method - does nothing
74      */

75     public void refresh()
76     {
77         // empty
78
}
79
80     /**
81      * Initialize this object using the given RunData object
82      *
83      * @param data a RunData instance
84      */

85     public void init(RunData data)
86     {
87         super.init(data);
88         try
89         {
90             Class JavaDoc runDataClass = RunData.class;
91             Method JavaDoc meth = runDataClass.getDeclaredMethod("getContextPath", null);
92             contextPath = (String JavaDoc) meth.invoke(data, null);
93         }
94         catch (Exception JavaDoc e)
95         {
96             /*
97              * Ignore a NoSuchMethodException because it means we are
98              * using Servlet API 2.0. Make sure scriptName is not
99              * null.
100              */

101             contextPath = "";
102         }
103     }
104
105     /**
106      * Returns a URI pointing to the given content (where content is a
107      * path relative to the webapp root.
108      *
109      * @param pathToContent a path relative to the webapp root
110      */

111     public String JavaDoc getURI(String JavaDoc pathToContent)
112     {
113         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114         sb.append(getServerScheme()); //http
115
sb.append("://");
116         sb.append(getServerName()); //www.foo.com
117
sb.append(":");
118         sb.append(getServerPort()); //port webserver running on (8080 for TDK)
119
//the context for tomcat adds a / so no need to add another
120
sb.append(contextPath); //the tomcat context
121
sb.append("/");
122         sb.append(pathToContent);
123         return (sb.toString());
124     }
125 }
126
Popular Tags