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.jhtml; | |
34 | ||
35 | import java.util.HashMap; | |
36 | import java.util.Map; | |
37 | import java.util.StringTokenizer; | |
38 | ||
39 | import javax.servlet.jsp.JspException; | |
40 | ||
41 | import org.chwf.servlet.ServletUtils; | |
42 | import org.chwf.taglib.base.LifeCycleTagSupport; | |
43 | ||
44 | /** | |
45 | * Tag handler for the <code><fieldPattern></code> tag. | |
46 | * | |
47 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
48 | */ | |
49 | 11 | public class FieldPatternTag extends LifeCycleTagSupport { |
50 | ||
51 | /** Key for input insert point. */ | |
52 | public static final String INPUT_KEY = "input"; | |
53 | ||
54 | /** Key for label insert point. */ | |
55 | public static final String LABEL_KEY = "label"; | |
56 | ||
57 | /** Key for error insert point. */ | |
58 | public static final String ERROR_KEY = "error"; | |
59 | ||
60 | /** Key used to cache normal pattern in the page context. */ | |
61 | public static final String NORMAL_PATTERN_KEY = | |
62 | "org.chwf.taglib.jhtml.FieldPatternTag.NORMAL_PATTERN_KEY"; | |
63 | ||
64 | /** Key used to cache error pattern in the page context. */ | |
65 | public static final String ERROR_PATTERN_KEY = | |
66 | "org.chwf.taglib.jhtml.FieldPatternTag.ERROR_PATTERN_KEY"; | |
67 | ||
68 | /** Pattern for normal fields. */ | |
69 | private String normalPattern; | |
70 | ||
71 | /** Pattern for fields with errors. */ | |
72 | private String errorPattern; | |
73 | ||
74 | /** | |
75 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
76 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
77 | * method: <code>super.cleanup()</code> | |
78 | */ | |
79 | public void cleanup() { | |
80 | 1 | super.cleanup(); |
81 | 1 | this.normalPattern = null; |
82 | 1 | this.errorPattern = null; |
83 | 1 | } |
84 | ||
85 | /** | |
86 | * Set the normal pattern. | |
87 | * | |
88 | * @param normalPattern The normal pattern. | |
89 | */ | |
90 | public void setNormal(String normalPattern) { | |
91 | 5 | this.normalPattern = normalPattern; |
92 | 5 | } |
93 | ||
94 | /** | |
95 | * Set the error pattern. | |
96 | * | |
97 | * @param errorPattern The error pattern. | |
98 | */ | |
99 | public void setError(String errorPattern) { | |
100 | 5 | this.errorPattern = errorPattern; |
101 | 5 | } |
102 | ||
103 | /** | |
104 | * Creates the object. | |
105 | * | |
106 | * @return SKIP_BODY | |
107 | * @throws JspException For IO exceptions. | |
108 | */ | |
109 | public int doStart() throws JspException { | |
110 | 3 | ServletUtils.setPageAttribute( |
111 | pageContext, | |
112 | NORMAL_PATTERN_KEY, | |
113 | getNormalPattern()); | |
114 | 3 | ServletUtils.setPageAttribute( |
115 | pageContext, | |
116 | ERROR_PATTERN_KEY, | |
117 | getErrorPattern()); | |
118 | 3 | return SKIP_BODY; |
119 | } | |
120 | ||
121 | /** | |
122 | * Get the (split) normal pattern. | |
123 | * | |
124 | * @return The normal pattern. | |
125 | */ | |
126 | public String[] getNormalPattern() { | |
127 | 6 | return splitPattern(this.normalPattern); |
128 | } | |
129 | ||
130 | /** | |
131 | * Get the (split) error pattern. | |
132 | * | |
133 | * @return The normal pattern. | |
134 | */ | |
135 | public String[] getErrorPattern() { | |
136 | 6 | return splitPattern(this.errorPattern); |
137 | } | |
138 | ||
139 | /** Map caching split pattern. */ | |
140 | 1 | private static final Map PATTERN_MAP = new HashMap(); |
141 | ||
142 | /** | |
143 | * Split the specified pattern into its individual elements. Pattern keys | |
144 | * ("input", "label" and "error") are assigned values suitable for "==" | |
145 | * comparison for greater efficiency. | |
146 | * | |
147 | * @param pattern The pattern. | |
148 | * @return The split pattern. | |
149 | */ | |
150 | public static String[] splitPattern(String pattern) { | |
151 | 13 | if (pattern == null) { |
152 | 4 | return null; |
153 | } | |
154 | 9 | String[] array = null; |
155 | 9 | synchronized (PATTERN_MAP) { |
156 | 9 | array = (String[]) PATTERN_MAP.get(pattern); |
157 | 9 | if (array == null) { |
158 | 4 | StringTokenizer tokenizer = new StringTokenizer(pattern, "{}"); |
159 | 4 | array = new String[tokenizer.countTokens()]; |
160 | 4 | int count = 0; |
161 | 22 | while (tokenizer.hasMoreTokens()) { |
162 | 14 | String token = tokenizer.nextToken(); |
163 | 14 | if (token.equals(INPUT_KEY)) { |
164 | 2 | array[count] = INPUT_KEY; |
165 | 12 | } else if (token.equals(LABEL_KEY)) { |
166 | 2 | array[count] = LABEL_KEY; |
167 | 10 | } else if (token.equals(ERROR_KEY)) { |
168 | 2 | array[count] = ERROR_KEY; |
169 | } else { | |
170 | 8 | array[count] = token; |
171 | } | |
172 | 14 | count++; |
173 | } | |
174 | 4 | PATTERN_MAP.put(pattern, array); |
175 | } | |
176 | 9 | } |
177 | 9 | return array; |
178 | } | |
179 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |