1 16 17 package org.apache.velocity.tools.struts; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.struts.util.MessageResources; 24 import org.apache.struts.action.ActionMessage; 25 import org.apache.struts.action.ActionMessages; 26 27 import org.apache.velocity.app.Velocity; 28 import org.apache.velocity.tools.struts.StrutsUtils; 29 30 56 public class ActionMessagesTool extends MessageResourcesTool 57 { 58 59 60 protected ActionMessages actionMsgs; 61 62 63 66 public ActionMessagesTool() 67 {} 68 69 70 76 public void init(Object obj) 77 { 78 super.init(obj); 80 81 this.actionMsgs = StrutsUtils.getMessages(this.request); 82 } 83 84 85 86 87 91 public boolean exist() 92 { 93 if (actionMsgs == null) 94 { 95 return false; 96 } 97 return !actionMsgs.isEmpty(); 98 } 99 100 101 107 public boolean exist(String property) 108 { 109 if (actionMsgs == null) 110 { 111 return false; 112 } 113 return (actionMsgs.size(property) > 0); 114 } 115 116 117 120 public int getSize() 121 { 122 if (actionMsgs == null) 123 { 124 return 0; 125 } 126 return actionMsgs.size(); 127 } 128 129 130 135 public int getSize(String property) 136 { 137 if (actionMsgs == null) 138 { 139 return 0; 140 } 141 return actionMsgs.size(property); 142 } 143 144 145 161 public List getGlobal() 162 { 163 return get(getGlobalName()); 164 } 165 166 167 174 public List getAll() 175 { 176 return get(null); 177 } 178 179 180 187 public List getAll(String bundle) 188 { 189 return get(null, bundle); 190 } 191 192 193 203 public List get(String property) 204 { 205 return get(property, null); 206 } 207 208 209 220 public List get(String property, String bundle) 221 { 222 if (actionMsgs == null || actionMsgs.isEmpty()) 223 { 224 return null; 225 } 226 227 Iterator msgs; 228 if (property == null) 229 { 230 msgs = actionMsgs.get(); 231 } 232 else 233 { 234 msgs = actionMsgs.get(property); 235 } 236 237 if (!msgs.hasNext()) 238 { 239 return null; 240 } 241 242 MessageResources res = getResources(bundle); 243 List list = new ArrayList (); 244 245 while (msgs.hasNext()) 246 { 247 ActionMessage msg = (ActionMessage)msgs.next(); 248 249 String message = null; 250 if (res != null) 251 { 252 message = 253 res.getMessage(this.locale, msg.getKey(), msg.getValues()); 254 255 if (message == null) 256 { 257 Velocity.warn("ActionMessagesTool: Message for key " + 258 msg.getKey() + 259 " could not be found in message resources."); 260 } 261 } 262 else 263 { 264 message = msg.getKey(); 266 } 267 list.add(message); 268 } 269 return list; 270 } 271 272 273 277 public String getGlobalName() 278 { 279 return ActionMessages.GLOBAL_MESSAGE; 280 } 281 282 } 283 | Popular Tags |