Coverage details for org.chwf.taglib.jhtml.FieldPatternTag

LineHitsSource
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>&lt;fieldPattern&gt;</code> tag.
46  *
47  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
48  */
4911public 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() {
801    super.cleanup();
811    this.normalPattern = null;
821    this.errorPattern = null;
831  }
84  
85   /**
86    * Set the normal pattern.
87    *
88    * @param normalPattern The normal pattern.
89    */
90   public void setNormal(String normalPattern) {
915    this.normalPattern = normalPattern;
925  }
93  
94   /**
95    * Set the error pattern.
96    *
97    * @param errorPattern The error pattern.
98    */
99   public void setError(String errorPattern) {
1005    this.errorPattern = errorPattern;
1015  }
102  
103   /**
104    * Creates the object.
105    *
106    * @return SKIP_BODY
107    * @throws JspException For IO exceptions.
108    */
109   public int doStart() throws JspException {
1103    ServletUtils.setPageAttribute(
111       pageContext,
112       NORMAL_PATTERN_KEY,
113       getNormalPattern());
1143    ServletUtils.setPageAttribute(
115       pageContext,
116       ERROR_PATTERN_KEY,
117       getErrorPattern());
1183    return SKIP_BODY;
119   }
120  
121   /**
122    * Get the (split) normal pattern.
123    *
124    * @return The normal pattern.
125    */
126   public String[] getNormalPattern() {
1276    return splitPattern(this.normalPattern);
128   }
129  
130   /**
131    * Get the (split) error pattern.
132    *
133    * @return The normal pattern.
134    */
135   public String[] getErrorPattern() {
1366    return splitPattern(this.errorPattern);
137   }
138  
139   /** Map caching split pattern. */
1401  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) {
15113    if (pattern == null) {
1524      return null;
153     }
1549    String[] array = null;
1559    synchronized (PATTERN_MAP) {
1569      array = (String[]) PATTERN_MAP.get(pattern);
1579      if (array == null) {
1584        StringTokenizer tokenizer = new StringTokenizer(pattern, "{}");
1594        array = new String[tokenizer.countTokens()];
1604        int count = 0;
16122        while (tokenizer.hasMoreTokens()) {
16214          String token = tokenizer.nextToken();
16314          if (token.equals(INPUT_KEY)) {
1642            array[count] = INPUT_KEY;
16512          } else if (token.equals(LABEL_KEY)) {
1662            array[count] = LABEL_KEY;
16710          } else if (token.equals(ERROR_KEY)) {
1682            array[count] = ERROR_KEY;
169           } else {
1708            array[count] = token;
171           }
17214          count++;
173         }
1744        PATTERN_MAP.put(pattern, array);
175       }
1769    }
1779    return array;
178   }
179 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.