1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.lang.reflect.Method;
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 264148 2005-08-29 14:21:04Z henning $
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 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 data)
66 {
67
68
69 init((RunData) data);
70 }
71
72 /***
73 * Refresh method - does nothing
74 */
75 public void refresh()
76 {
77
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 runDataClass = RunData.class;
91 Method meth = runDataClass.getDeclaredMethod("getContextPath", null);
92 contextPath = (String) meth.invoke(data, null);
93 }
94 catch (Exception e)
95 {
96
97
98
99
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 getURI(String pathToContent)
112 {
113 StringBuffer sb = new StringBuffer();
114 sb.append(getServerScheme());
115 sb.append("://");
116 sb.append(getServerName());
117 sb.append(":");
118 sb.append(getServerPort());
119
120 sb.append(contextPath);
121 sb.append("/");
122 sb.append(pathToContent);
123 return (sb.toString());
124 }
125 }