1 16 17 package org.apache.taglibs.standard.tag.common.core; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.OutputStreamWriter ; 22 import java.text.DateFormat ; 23 import java.util.Enumeration ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.PageContext ; 28 29 import org.apache.taglibs.standard.resources.Resources; 30 31 36 public class Util { 37 38 private static final String REQUEST = "request"; 39 private static final String SESSION = "session"; 40 private static final String APPLICATION = "application"; 41 private static final String DEFAULT = "default"; 42 private static final String SHORT = "short"; 43 private static final String MEDIUM = "medium"; 44 private static final String LONG = "long"; 45 private static final String FULL = "full"; 46 47 public static final int HIGHEST_SPECIAL = '>'; 48 public static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][]; 49 static { 50 specialCharactersRepresentation['&'] = "&".toCharArray(); 51 specialCharactersRepresentation['<'] = "<".toCharArray(); 52 specialCharactersRepresentation['>'] = ">".toCharArray(); 53 specialCharactersRepresentation['"'] = """.toCharArray(); 54 specialCharactersRepresentation['\''] = "'".toCharArray(); 55 } 56 57 68 public static int getScope(String scope) { 69 int ret = PageContext.PAGE_SCOPE; 71 if (REQUEST.equalsIgnoreCase(scope)) 72 ret = PageContext.REQUEST_SCOPE; 73 else if (SESSION.equalsIgnoreCase(scope)) 74 ret = PageContext.SESSION_SCOPE; 75 else if (APPLICATION.equalsIgnoreCase(scope)) 76 ret = PageContext.APPLICATION_SCOPE; 77 78 return ret; 79 } 80 81 92 public static int getStyle(String style, String errCode) 93 throws JspException { 94 int ret = DateFormat.DEFAULT; 95 96 if (style != null) { 97 if (DEFAULT.equalsIgnoreCase(style)) { 98 ret = DateFormat.DEFAULT; 99 } else if (SHORT.equalsIgnoreCase(style)) { 100 ret = DateFormat.SHORT; 101 } else if (MEDIUM.equalsIgnoreCase(style)) { 102 ret = DateFormat.MEDIUM; 103 } else if (LONG.equalsIgnoreCase(style)) { 104 ret = DateFormat.LONG; 105 } else if (FULL.equalsIgnoreCase(style)) { 106 ret = DateFormat.FULL; 107 } else { 108 throw new JspException (Resources.getMessage(errCode, style)); 109 } 110 } 111 112 return ret; 113 } 114 115 116 117 129 public static String escapeXml(String buffer) { 130 int start = 0; 131 int length = buffer.length(); 132 char[] arrayBuffer = buffer.toCharArray(); 133 StringBuffer escapedBuffer = null; 134 135 for (int i = 0; i < length; i++) { 136 char c = arrayBuffer[i]; 137 if (c <= HIGHEST_SPECIAL) { 138 char[] escaped = specialCharactersRepresentation[c]; 139 if (escaped != null) { 140 if (start == 0) { 142 escapedBuffer = new StringBuffer (length + 5); 143 } 144 if (start < i) { 146 escapedBuffer.append(arrayBuffer,start,i-start); 147 } 148 start = i + 1; 149 escapedBuffer.append(escaped); 151 } 152 } 153 } 154 if (start == 0) { 156 return buffer; 157 } 158 if (start < length) { 160 escapedBuffer.append(arrayBuffer,start,length-start); 161 } 162 return escapedBuffer.toString(); 163 } 164 165 169 public static String getContentTypeAttribute(String input, String name) { 170 int begin; 171 int end; 172 int index = input.toUpperCase().indexOf(name.toUpperCase()); 173 if (index == -1) return null; 174 index = index + name.length(); index = input.indexOf('=', index); if (index == -1) return null; 177 index += 1; input = input.substring(index).trim(); 179 180 if (input.charAt(0) == '"') { 181 begin = 1; 183 end = input.indexOf('"', begin); 184 if (end == -1) return null; 185 } else { 186 begin = 0; 187 end = input.indexOf(';'); 188 if (end == -1) end = input.indexOf(' '); 189 if (end == -1) end = input.length(); 190 } 191 return input.substring(begin, end).trim(); 192 } 193 194 205 public static String URLEncode(String s, String enc) { 206 207 if (s == null) { 208 return "null"; 209 } 210 211 if (enc == null) { 212 enc = "UTF-8"; } 214 215 StringBuffer out = new StringBuffer (s.length()); 216 ByteArrayOutputStream buf = new ByteArrayOutputStream (); 217 OutputStreamWriter writer = null; 218 try { 219 writer = new OutputStreamWriter (buf, enc); 220 } catch (java.io.UnsupportedEncodingException ex) { 221 writer = new OutputStreamWriter (buf); 223 } 224 225 for (int i = 0; i < s.length(); i++) { 226 int c = s.charAt(i); 227 if (c == ' ') { 228 out.append('+'); 229 } else if (isSafeChar(c)) { 230 out.append((char)c); 231 } else { 232 try { 234 writer.write(c); 235 writer.flush(); 236 } catch(IOException e) { 237 buf.reset(); 238 continue; 239 } 240 byte[] ba = buf.toByteArray(); 241 for (int j = 0; j < ba.length; j++) { 242 out.append('%'); 243 out.append(Character.forDigit((ba[j]>>4) & 0xf, 16)); 245 out.append(Character.forDigit(ba[j] & 0xf, 16)); 246 } 247 buf.reset(); 248 } 249 } 250 return out.toString(); 251 } 252 253 private static boolean isSafeChar(int c) { 254 if (c >= 'a' && c <= 'z') { 255 return true; 256 } 257 if (c >= 'A' && c <= 'Z') { 258 return true; 259 } 260 if (c >= '0' && c <= '9') { 261 return true; 262 } 263 if (c == '-' || c == '_' || c == '.' || c == '!' || 264 c == '~' || c == '*' || c == '\'' || c == '(' || c == ')') { 265 return true; 266 } 267 return false; 268 } 269 270 279 public static Enumeration getRequestLocales(HttpServletRequest request) { 280 Enumeration values = request.getHeaders("accept-language"); 281 if (values.hasMoreElements()) { 282 return request.getLocales(); 286 } else { 287 return values; 291 } 292 } 293 } 294 | Popular Tags |