%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.ContentURI |
|
|
1 | package org.apache.turbine.util; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 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; |
|
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 | 0 | super(data); |
49 | 0 | init(data); |
50 | 0 | } |
51 | ||
52 | /** |
|
53 | * Default constructor |
|
54 | */ |
|
55 | public ContentURI() |
|
56 | 0 | { |
57 | 0 | } |
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 | // we blithely cast to RunData as the runtime error thrown |
|
68 | // if data is null or another type is appropriate. |
|
69 | 0 | init((RunData) data); |
70 | 0 | } |
71 | ||
72 | /** |
|
73 | * Refresh method - does nothing |
|
74 | */ |
|
75 | public void refresh() |
|
76 | { |
|
77 | // empty |
|
78 | 0 | } |
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 | 0 | super.init(data); |
88 | try |
|
89 | { |
|
90 | 0 | Class runDataClass = RunData.class; |
91 | 0 | Method meth = runDataClass.getDeclaredMethod("getContextPath", null); |
92 | 0 | contextPath = (String) meth.invoke(data, null); |
93 | } |
|
94 | 0 | catch (Exception 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 | 0 | contextPath = ""; |
102 | 0 | } |
103 | 0 | } |
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 | 0 | StringBuffer sb = new StringBuffer(); |
114 | 0 | sb.append(getServerScheme()); //http |
115 | 0 | sb.append("://"); |
116 | 0 | sb.append(getServerName()); //www.foo.com |
117 | 0 | sb.append(":"); |
118 | 0 | sb.append(getServerPort()); //port webserver running on (8080 for TDK) |
119 | //the context for tomcat adds a / so no need to add another |
|
120 | 0 | sb.append(contextPath); //the tomcat context |
121 | 0 | sb.append("/"); |
122 | 0 | sb.append(pathToContent); |
123 | 0 | return (sb.toString()); |
124 | } |
|
125 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |