1 18 19 package org.apache.struts.util; 20 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.PageContext ; 23 24 import org.apache.struts.taglib.TagUtils; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.commons.logging.Log; 27 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.net.URLEncoder ; 31 32 38 public class ResponseUtils { 39 40 41 43 44 47 protected static MessageResources messages = 48 MessageResources.getMessageResources 49 ("org.apache.struts.util.LocalStrings"); 50 51 52 55 private static Method encode = null; 56 57 58 61 private static final Log log = LogFactory.getLog(ResponseUtils.class); 62 63 64 68 static { 69 70 try { 71 Class [] args = new Class []{String .class, String .class}; 73 encode = URLEncoder .class.getMethod("encode", args); 74 } catch (NoSuchMethodException e) { 75 log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); 76 } 77 } 78 79 80 81 83 84 91 public static String filter(String value) { 92 93 if (value == null || value.length() == 0) { 94 return value; 95 } 96 97 StringBuffer result = null; 98 String filtered = null; 99 for (int i = 0; i < value.length(); i++) { 100 filtered = null; 101 switch (value.charAt(i)) { 102 case '<': 103 filtered = "<"; 104 break; 105 case '>': 106 filtered = ">"; 107 break; 108 case '&': 109 filtered = "&"; 110 break; 111 case '"': 112 filtered = """; 113 break; 114 case '\'': 115 filtered = "'"; 116 break; 117 } 118 119 if (result == null) { 120 if (filtered != null) { 121 result = new StringBuffer (value.length() + 50); 122 if (i > 0) { 123 result.append(value.substring(0, i)); 124 } 125 result.append(filtered); 126 } 127 } else { 128 if (filtered == null) { 129 result.append(value.charAt(i)); 130 } else { 131 result.append(filtered); 132 } 133 } 134 } 135 136 return result == null ? value : result.toString(); 137 } 138 139 140 141 142 148 public static String encodeURL(String url) { 149 return encodeURL(url, "UTF-8"); 150 } 151 152 153 161 public static String encodeURL(String url, String enc) { 162 try { 163 164 if(enc==null || enc.length()==0){ 165 enc = "UTF-8"; 166 } 167 168 if (encode != null) { 170 return (String ) encode.invoke(null, new Object []{url, enc}); 171 } 172 173 } catch (IllegalAccessException e) { 174 log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); 175 } catch (InvocationTargetException e) { 176 log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); 177 } 178 179 return URLEncoder.encode(url); 180 } 181 182 183 197 public static void write(PageContext pageContext, String text) 198 throws JspException { 199 200 TagUtils.getInstance().write(pageContext, text); 201 202 } 203 204 205 216 public static void writePrevious(PageContext pageContext, String text) 217 throws JspException { 218 219 TagUtils.getInstance().writePrevious(pageContext, text); 220 221 } 222 223 224 } 225 | Popular Tags |