1 18 package org.apache.struts.taglib.nested; 19 20 import java.util.*; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.jsp.tagext.Tag ; 23 24 import org.apache.struts.taglib.html.Constants; 25 import org.apache.struts.taglib.html.FormTag; 26 27 46 public class NestedPropertyHelper { 47 48 49 public static final String NESTED_INCLUDES_KEY = "<nested-includes-key/>"; 50 51 52 57 public static final String getCurrentProperty(HttpServletRequest request) { 58 NestedReference nr = (NestedReference) request.getAttribute(NESTED_INCLUDES_KEY); 60 return (nr == null) ? null : nr.getNestedProperty(); 62 } 63 64 65 75 public static final String getCurrentName(HttpServletRequest request, 76 NestedNameSupport nested) { 77 NestedReference nr = (NestedReference) request.getAttribute(NESTED_INCLUDES_KEY); 79 if (nr != null) { 81 return nr.getBeanName(); 82 83 } else { 84 Tag tag = (Tag ) nested; 86 Tag formTag = null; 87 88 do { 90 tag = tag.getParent(); 91 if (tag != null && tag instanceof FormTag) { 92 formTag = tag; 93 } 94 } while (formTag == null && tag != null); 95 96 if (formTag == null) { 97 return ""; 98 } 99 return ((FormTag) formTag).getBeanName(); 101 } 102 } 103 104 112 public static final String getAdjustedProperty(HttpServletRequest request, 113 String property) { 114 String parent = getCurrentProperty(request); 116 return calculateRelativeProperty(property, parent); 117 } 118 119 125 public static final void setProperty(HttpServletRequest request, 126 String property) { 127 NestedReference nr = referenceInstance(request); 129 nr.setNestedProperty(property); 130 } 131 132 138 public static final void setName(HttpServletRequest request, String name) { 139 NestedReference nr = referenceInstance(request); 141 nr.setBeanName(name); 142 } 143 144 148 public static final void deleteReference(HttpServletRequest request) { 149 request.removeAttribute(NESTED_INCLUDES_KEY); 151 } 152 153 159 public static void setNestedProperties(HttpServletRequest request, 160 NestedPropertySupport tag) { 161 boolean adjustProperty = true; 162 163 if (tag instanceof NestedNameSupport) { 164 NestedNameSupport nameTag = (NestedNameSupport)tag; 165 if (nameTag.getName() == null|| Constants.BEAN_KEY.equals(nameTag.getName())) { 166 nameTag.setName(getCurrentName(request, (NestedNameSupport) tag)); 167 } else { 168 adjustProperty = false; 169 } 170 } 171 172 173 String property = tag.getProperty(); 174 if (adjustProperty) { 175 property = getAdjustedProperty(request, property); 176 } 177 tag.setProperty(property); 178 } 179 180 181 187 private static final NestedReference referenceInstance(HttpServletRequest request) { 188 189 NestedReference nr = (NestedReference) request.getAttribute(NESTED_INCLUDES_KEY); 190 if (nr == null) { 192 nr = new NestedReference(); 193 request.setAttribute(NESTED_INCLUDES_KEY, nr); 194 } 195 return nr; 197 } 198 199 207 private static String calculateRelativeProperty(String property, 208 String parent) { 209 if (parent == null) { parent = ""; } 210 if (property == null) { property = ""; } 211 212 214 if ("./".equals(property) || "this/".equals(property)) { 215 return parent; 216 } 217 218 219 String stepping; 220 221 222 if (property.endsWith("/")) { 223 stepping = property; 224 property = ""; 225 } else { 226 stepping = property.substring(0, property.lastIndexOf('/') + 1); 227 228 property = property.substring(property.lastIndexOf('/') + 1, property.length()); 229 } 230 231 if (stepping.startsWith("/")) { 232 233 return property; 234 } else { 235 236 StringTokenizer proT = new StringTokenizer(parent, "."); 237 int propCount = proT.countTokens(); 238 239 240 StringTokenizer strT = new StringTokenizer(stepping, "/"); 241 int count = strT.countTokens(); 242 243 if (count >= propCount) { 244 245 return property; 246 247 } else { 248 249 count = propCount - count; 250 StringBuffer result = new StringBuffer (); 251 for (int i = 0; i < count; i++) { 252 result.append(proT.nextToken()); 253 result.append('.'); 254 } 255 result.append(property); 256 257 258 if (result.charAt(result.length()-1) == '.') { 259 return result.substring(0,result.length()-1); 260 } else { 261 return result.toString(); 262 } 263 } 264 } 265 } 266 } 267 | Popular Tags |