1 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 ; 25 import javax.servlet.jsp.tagext.JspTag ; 26 import javax.servlet.jsp.tagext.SimpleTagSupport ; 27 28 30 32 34 96 public class FormatString extends FormatTag 97 { 98 protected boolean truncate = false; 100 103 public String getTagName() 104 { 105 return "FormatString"; 106 } 107 108 117 public void setTruncate(boolean truncate) 118 { 119 this.truncate = truncate; 120 } 121 122 126 public void doTag() 127 throws JspException 128 { 129 JspTag parentTag = SimpleTagSupport.findAncestorWithClass(this, IFormattable.class); 130 131 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 s = Bundle.getString("Tags_FormattableParentRequired"); 150 registerTagError(s, null); 151 reportErrors(); 152 } 153 } 154 155 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 format(Object dataToFormat) throws JspException 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 unformattedString = dataToFormat.toString(); 181 182 int length = unformattedString.length(); 183 int patternLength = getPattern().length(); 184 185 int ignoreNumbers = 0; 186 187 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 } 227 else { 228 formattedString.append(unformattedString.charAt(index)); 229 } 230 patternIndex++; 231 } 232 233 if (patternIndex < patternLength) { 234 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 |