KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > FormatString


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.util.Bundle;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.JspTag JavaDoc;
26 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
27
28 //java imports
29

30 //internal imports
31

32 //external imports
33

34 /**
35  * A formatter used to format strings. FormatString uses the following pattern syntax:
36  * <p>
37  * The '#' character gets replaced by the next character in the string getting formatted,
38  * while other characters get put in as literals. For example:
39  * <p>
40  * String "5555555555" with pattern "(###)###-####" would result in: (555)555-5555.
41  * <p>
42  * The '*' character will display all characters in the string at that point in the pattern. For example:
43  * <p>
44  * String "123456" with pattern "#-*!" would result in: 1-23456!
45  * <p>
46  * If a result with a '#' pr '*' character showing is desired, the '#' or '*' needs to
47  * be escaped with the '$' character. For example:
48  * <p>
49  * String "ABCD" with pattern "$#-####" would result in: #-ABCD.
50  * <p>
51  * To show a '$' in the result, the '$' character needs to be escaped. For example:
52  * <p>
53  * String "1234" with pattern "$$#,###" would result in: $1,234
54  * <p>
55  * If the truncate attribute is set to "true", characters in the string that exceed the pattern
56  * will be dropped. Otherwise, they will be appended to the end of the formatted string.
57  * @jsptagref.tagdescription A formatter used to format strings.
58  *
59  * <p>The &lt;netui:formatString> tag formats the output of its parent tag. For example:
60  *
61  * <pre> &lt;netui:span value="${pageFlow.phone}">
62  * &lt;netui:formatString pattern="phone number: (###) ###-####"/>
63  * &lt;/netui:span> </pre>
64  *
65  * <p>
66  * &lt;netui:formatString> uses the following pattern syntax:
67  * <p>
68  * The <b>#</b> character is a placeholder for individual characters in the String to be formatted,
69  * while other characters are treated as literals. For example:
70  * <p>
71  * String "5555555555" with pattern "(###)###-####" would result in: (555)555-5555.
72  * <p>
73  * The <b>*</b> character displays all remaining characters in the String. For example:
74  * <p>
75  * String "123456" with pattern "#-*!" would result in: 1-23456!
76  * <p>
77  * If a result with a '#' or '*' character showing is desired, the '#' or '*' needs to
78  * be escaped with the '$' character. For example:
79  * <p>
80  * String "ABCD" with pattern "$#-####" would result in: #-ABCD.
81  * <p>
82  * To show a '$' in the result, the '$' character needs to be escaped. For example:
83  * <p>
84  * String "1234" with pattern "$$#,###" would result in: $1,234
85  * <p>
86  * If the <code>truncate</code> attribute is set to "true", characters in the String that exceed the pattern
87  * will be dropped. Otherwise, they will be appended to the end of the formatted String.
88  * @example In this sample, the String "2125555555" will be formatted to this form: (212)555-5555.
89  *
90  * <pre> &lt;netui:span value="2125555555">
91  * &lt;netui:formatString pattern="phone (###) ###-####"/>
92  * &lt;/netui:span> </pre>
93  * @netui:tag name="formatString" body-content="empty" description="A formatter used to format strings."
94  * @netui:attribute name="pattern" required="true" rtexprvalue="true"
95  */

96 public class FormatString extends FormatTag
97 {
98     protected boolean truncate = false; // Whether or not pattern-exceeding characters should be dropped.
99

100     /**
101      * Return the name of the Tag.
102      */

103     public String JavaDoc getTagName()
104     {
105         return "FormatString";
106     }
107
108     /**
109      * Sets whether or not pattern-exceeding characters should be dropped.
110      * @param truncate "true" or "false"
111      * @jsptagref.attributedescription A boolean specifying whether characters that exceed the pattern's length should be dropped.
112      * @jsptagref.databindable false
113      * @jsptagref.attributesyntaxvalue <i>boolean_truncate</i>
114      * @netui:attribute required="false" rtexprvalue="true"
115      * description="A boolean specifying whether characters that exceed the pattern's length should be dropped."
116      */

117     public void setTruncate(boolean truncate)
118     {
119         this.truncate = truncate;
120     }
121
122     /**
123      * Create the internal Formatter instance and perform the formatting.
124      * @throws JspException if a JSP exception has occurred
125      */

126     public void doTag()
127             throws JspException JavaDoc
128     {
129         JspTag JavaDoc parentTag = SimpleTagSupport.findAncestorWithClass(this, IFormattable.class);
130
131         // if there are errors we need to either add these to the parent AbstractBastTag or report an error.
132
if (hasErrors()) {
133             if (parentTag instanceof IFormattable) {
134                 IFormattable parent = (IFormattable) parentTag;
135                 parent.formatterHasError();
136             }
137             reportErrors();
138             return;
139         }
140
141         if (parentTag instanceof IFormattable) {
142             StringFormatter formatter = new StringFormatter();
143             formatter.setPattern(_pattern);
144             formatter.setTruncate(truncate);
145             IFormattable parent = (IFormattable) parentTag;
146             parent.addFormatter(formatter);
147         }
148         else {
149             String JavaDoc s = Bundle.getString("Tags_FormattableParentRequired");
150             registerTagError(s, null);
151             reportErrors();
152         }
153     }
154
155     /**
156      * Internal FormatTag.Formatter which performs its own string parsing and formatting.
157      */

158     public static class StringFormatter extends FormatTag.Formatter
159     {
160         private boolean truncate;
161
162         public boolean getTruncate()
163         {
164             return truncate;
165         }
166
167         public void setTruncate(boolean truncate)
168         {
169             this.truncate = truncate;
170         }
171
172         public String JavaDoc format(Object JavaDoc dataToFormat) throws JspException JavaDoc
173         {
174             if (dataToFormat == null) {
175                 return null;
176             }
177             InternalStringBuilder formattedString = new InternalStringBuilder(32);
178             int index = 0;
179             int patternIndex = 0;
180             String JavaDoc unformattedString = dataToFormat.toString();
181
182             int length = unformattedString.length();
183             int patternLength = getPattern().length();
184
185             int ignoreNumbers = 0;
186
187             //Cycle through each character in the string
188
for (index = 0; index < length; index++) {
189                 if (patternIndex < patternLength) {
190                     boolean loop = true;
191                     while (loop) {
192                         char thisChar = getPattern().charAt(patternIndex);
193                         if (thisChar == '#')
194                             break;
195                         else if (thisChar == '*') {
196                             ignoreNumbers++;
197                             break;
198                         }
199                         else if (thisChar == '$') {
200                             if ((patternIndex + 1 < patternLength) && (getPattern().charAt(patternIndex + 1) == '$')) {
201                                 patternIndex++;
202                             }
203                             else if ((patternIndex + 1 < patternLength) && (getPattern().charAt(patternIndex + 1) == '#')) {
204                                 patternIndex++;
205                             }
206                             else if ((patternIndex + 1 < patternLength) && (getPattern().charAt(patternIndex + 1) == '*')) {
207                                 patternIndex++;
208                             }
209                         }
210                         formattedString = formattedString.append(getPattern().charAt(patternIndex));
211                         patternIndex++;
212                         if (patternIndex >= patternLength)
213                             loop = false;
214                     }
215                 }
216
217                 if ((patternIndex >= patternLength) && (truncate))
218                     break;
219
220                 if (ignoreNumbers == 1) {
221                     formattedString.append(unformattedString.substring(index));
222                     ignoreNumbers++;
223                 }
224                 else if (ignoreNumbers > 1) {
225                     //Ignore
226
}
227                 else {
228                     formattedString.append(unformattedString.charAt(index));
229                 }
230                 patternIndex++;
231             }
232
233             if (patternIndex < patternLength) {
234                 //Throw in the rest of the pattern
235
while (patternIndex < patternLength) {
236                     char patternChar = getPattern().charAt(patternIndex);
237                     if (patternChar == '#') {
238                         formattedString.append(" ");
239                     }
240                     else {
241                         formattedString.append(patternChar);
242                     }
243                     patternIndex++;
244                 }
245             }
246             return formattedString.toString();
247         }
248     }
249 }
250
Popular Tags