1 25 package org.ofbiz.webapp.taglib; 26 27 import java.io.IOException ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import javax.servlet.jsp.JspTagException ; 35 import javax.servlet.jsp.JspWriter ; 36 import javax.servlet.jsp.PageContext ; 37 import javax.servlet.jsp.tagext.BodyContent ; 38 import javax.servlet.jsp.tagext.BodyTagSupport ; 39 40 import org.ofbiz.base.util.Debug; 41 import org.ofbiz.base.util.ObjectType; 42 43 44 52 public class IteratorTag extends BodyTagSupport { 53 54 public static final String module = IteratorTag.class.getName(); 55 56 protected Iterator iterator = null; 57 protected String name = null; 58 protected String property = null; 59 protected Object element = null; 60 protected Class type = null; 61 protected int limit = 0; 62 protected int offset = 0; 63 protected boolean expandMap = false; 64 65 public void setName(String name) { 66 this.name = name; 67 } 68 69 public void setProperty(String property) { 70 this.property = property; 71 } 72 73 public void setType(String type) throws ClassNotFoundException { 74 this.type = ObjectType.loadClass(type); 75 } 76 77 public void setLimit(int limit) { 78 this.limit = limit; 79 } 80 81 public void setOffset(int offset) { 82 this.offset = offset; 83 } 84 85 public void setExpandMap(String expMap) { 86 expandMap = "true".equals(expMap); 88 } 89 90 public void setIterator(Iterator iterator) { 91 this.iterator = iterator; 92 } 93 94 public String getName() { 95 return name; 96 } 97 98 public String getProperty() { 99 return property; 100 } 101 102 public Object getElement() { 103 return element; 104 } 105 106 public Iterator getIterator() { 107 return this.iterator; 108 } 109 110 public String getType() { 111 return type.getName(); 112 } 113 114 public int getLimit() { 115 return this.limit; 116 } 117 118 public int getOffset() { 119 return this.offset; 120 } 121 122 public String getExpandMap() { 123 return expandMap ? "true" : "false"; 124 } 125 126 public int doStartTag() throws JspTagException { 127 Debug.logVerbose("Starting Iterator Tag...", module); 128 129 if (!defineIterator()) 130 return SKIP_BODY; 131 132 Debug.logVerbose("We now have an iterator.", module); 133 134 if (defineElement()) 135 return EVAL_BODY_AGAIN; 136 else 137 return SKIP_BODY; 138 } 139 140 public int doAfterBody() { 141 if (defineElement()) { 142 return EVAL_BODY_AGAIN; 143 } else { 144 return SKIP_BODY; 145 } 146 } 147 148 public int doEndTag() { 149 try { 150 BodyContent body = getBodyContent(); 151 152 if (body != null) { 153 JspWriter out = body.getEnclosingWriter(); 154 String bodyString = body.getString(); 155 body.clearBody(); 156 out.print(bodyString); 157 } 158 } catch (IOException e) { 159 Debug.logInfo("IteratorTag IO Error", module); 160 Debug.logInfo(e, module); 161 } 162 return EVAL_PAGE; 163 } 164 165 private boolean defineIterator() { 166 Iterator newIterator = null; 168 Collection thisCollection = null; 169 170 if (property != null) { 171 if (Debug.verboseOn()) Debug.logVerbose("Getting iterator from property: " + property, module); 172 Object propertyObject = pageContext.findAttribute(property); 173 174 if (propertyObject instanceof Iterator ) { 175 newIterator = (Iterator ) propertyObject; 176 } else { 177 thisCollection = (Collection ) propertyObject; 179 } 180 } else { 181 ObjectTag objectTag = 183 (ObjectTag) findAncestorWithClass(this, ObjectTag.class); 184 185 if (objectTag == null) 186 return false; 187 if (objectTag.getType().equals("java.util.Collection")) { 188 thisCollection = (Collection ) objectTag.getObject(); 189 } else { 190 try { 191 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 192 Method [] m = loader.loadClass(objectTag.getType()).getDeclaredMethods(); 193 194 for (int i = 0; i < m.length; i++) { 195 if (m[i].getName().equals("iterator")) { 196 Debug.logVerbose("Found iterator method. Using it.", module); 197 newIterator = (Iterator ) m[i].invoke( 198 objectTag.getObject(), (Object []) null); 199 break; 200 } 201 } 202 } catch (Exception e) { 203 return false; 204 } 205 } 206 } 207 208 if (newIterator == null) { 209 if (thisCollection == null || thisCollection.size() < 1) 210 return false; 211 212 if (limit > 0 || offset > 0) { 213 ArrayList colList = new ArrayList (thisCollection); 214 int startIndex = offset > 0 ? offset : 0; 215 int endIndex = limit > 0 ? offset + limit : colList.size(); 216 217 endIndex = endIndex > colList.size() ? colList.size() : endIndex; 218 List subList = colList.subList(startIndex, endIndex); 219 220 newIterator = subList.iterator(); 221 } else { 222 newIterator = thisCollection.iterator(); 223 } 224 225 Debug.logVerbose("Got iterator.", module); 226 } else { Debug.logVerbose("iterator already set.", module); 228 } 229 this.iterator = newIterator; 230 return true; 231 } 232 233 private boolean defineElement() { 234 element = null; 235 pageContext.removeAttribute(name, PageContext.PAGE_SCOPE); 236 boolean verboseOn = Debug.verboseOn(); 237 238 if (this.iterator.hasNext()) { 239 element = this.iterator.next(); 240 if (verboseOn) Debug.logVerbose("iterator has another object: " + element, module); 241 } else { 242 if (verboseOn) Debug.logVerbose("iterator has no more objects", module); 243 } 244 if (element != null) { 245 if (verboseOn) Debug.logVerbose("set attribute " + name + " to be " + element + " as next value from iterator", module); 246 pageContext.setAttribute(name, element); 247 248 if (expandMap) { 250 Map tempMap = (Map ) element; 251 Iterator mapEntries = tempMap.entrySet().iterator(); 252 253 while (mapEntries.hasNext()) { 254 Map.Entry entry = (Map.Entry ) mapEntries.next(); 255 Object value = entry.getValue(); 256 257 if (value == null) value = new String (); 258 pageContext.setAttribute((String ) entry.getKey(), value); 259 } 260 } 261 262 return true; 263 } 264 if (verboseOn) Debug.logVerbose("no more iterations; element = " + element, module); 265 return false; 266 } 267 } 268 269 | Popular Tags |