Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.taglib.ji18n; | |
34 | ||
35 | import java.io.IOException; | |
36 | import java.util.Collection; | |
37 | import java.util.LinkedList; | |
38 | ||
39 | import javax.servlet.ServletException; | |
40 | import javax.servlet.http.HttpServletRequest; | |
41 | ||
42 | import org.chwf.i18n.Message; | |
43 | import org.chwf.servlet.view.PageConfig; | |
44 | import org.chwf.taglib.base.LifeCycleTagSupport; | |
45 | ||
46 | /** | |
47 | * Tag handler for the <code><message></code> tag. | |
48 | * | |
49 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
50 | */ | |
51 | 34 | public class MessageTag extends LifeCycleTagSupport { |
52 | ||
53 | /** Message name. */ | |
54 | private String name; | |
55 | ||
56 | /** Message resource. */ | |
57 | private String resource; | |
58 | ||
59 | /** Message arguments. */ | |
60 | 17 | private Collection arguments = new LinkedList(); |
61 | ||
62 | /** | |
63 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
64 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
65 | * method: <code>super.cleanup()</code> | |
66 | */ | |
67 | public void cleanup() { | |
68 | 1 | super.cleanup(); |
69 | 1 | this.name = null; |
70 | 1 | this.resource = null; |
71 | 1 | arguments.clear(); |
72 | 1 | } |
73 | ||
74 | /** | |
75 | * The message key. | |
76 | * | |
77 | * @return The message key. | |
78 | * @deprecated | |
79 | */ | |
80 | public String getKey() { | |
81 | 2 | return this.name; |
82 | } | |
83 | ||
84 | /** | |
85 | * The message key. | |
86 | * | |
87 | * @param key The message key. | |
88 | * @deprecated | |
89 | */ | |
90 | public void setKey(String key) { | |
91 | 2 | this.name = key; |
92 | 2 | } |
93 | ||
94 | /** | |
95 | * The message name. | |
96 | * | |
97 | * @return The message name. | |
98 | */ | |
99 | public String getName() { | |
100 | 7 | return this.name; |
101 | } | |
102 | ||
103 | /** | |
104 | * The message name. | |
105 | * | |
106 | * @param name The message name. | |
107 | */ | |
108 | public void setName(String name) { | |
109 | 7 | this.name = name; |
110 | 7 | } |
111 | ||
112 | /** | |
113 | * The resource name. Defaults to resource derived from page location. | |
114 | * | |
115 | * @return The resource name. | |
116 | */ | |
117 | public String getResource() { | |
118 | 9 | if (this.resource == null) { |
119 | 3 | this.resource = |
120 | (String) this.pageContext.getAttribute(ResourceTag.RESOURCE_NAME); | |
121 | } | |
122 | 9 | if (this.resource == null) { |
123 | 2 | HttpServletRequest request = |
124 | (HttpServletRequest) this.pageContext.getRequest(); | |
125 | 2 | this.resource = PageConfig.getPageConfig(request).getResource(); |
126 | } | |
127 | 9 | return this.resource; |
128 | } | |
129 | ||
130 | /** | |
131 | * The resource name. | |
132 | * | |
133 | * @param resource The resource name. | |
134 | */ | |
135 | public void setResource(String resource) { | |
136 | 7 | this.resource = rewritePath(resource); |
137 | 7 | } |
138 | ||
139 | /** | |
140 | * Add a tag attribute. This method is used to manipulate generic or | |
141 | * pass-through attributes. | |
142 | * | |
143 | * @param name The attribute name. | |
144 | * @param value The attribute value. | |
145 | */ | |
146 | public void addParameter(String name, String value) { | |
147 | 2 | super.addParameter(name, value); |
148 | 2 | arguments.add(value); |
149 | 2 | } |
150 | ||
151 | /** | |
152 | * Write out the message. | |
153 | * | |
154 | * @return EVAL_PAGE | |
155 | * @throws ServletException For servlet errors. | |
156 | * @throws IOException For write errors. | |
157 | */ | |
158 | public int doEnd() throws ServletException, IOException { | |
159 | 3 | Message message = |
160 | Message.getMessage(getName(), arguments.toArray(), getResource()); | |
161 | 3 | pageContext.getOut().print(message.toString()); |
162 | 3 | return EVAL_PAGE; |
163 | } | |
164 | ||
165 | /** | |
166 | * Rewrite the resource path. | |
167 | * | |
168 | * @param path The original path. | |
169 | * @return The rewritten path. | |
170 | */ | |
171 | static String rewritePath(String path) { | |
172 | 9 | if (path.charAt(0) == '/') { |
173 | 1 | path = path.substring(1); |
174 | } | |
175 | 9 | return path.replace('/', '.'); |
176 | } | |
177 | } | |
178 |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |