1 25 package org.ofbiz.webapp.taglib; 26 27 import java.io.IOException ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import javax.servlet.jsp.JspTagException ; 31 import javax.servlet.jsp.JspWriter ; 32 import javax.servlet.jsp.tagext.BodyContent ; 33 import javax.servlet.jsp.tagext.BodyTagSupport ; 34 35 43 public class IterateNextTag extends BodyTagSupport { 44 45 protected String name = null; 46 protected Class type = null; 47 protected Object element = null; 48 protected boolean expandMap = false; 49 50 public void setName(String name) { 51 this.name = name; 52 } 53 54 public void setType(String type) throws ClassNotFoundException { 55 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 56 this.type = loader.loadClass(type); 57 } 58 59 public void setExpandMap(String expMap) { 60 expandMap = "true".equals(expMap); 62 } 63 64 public String getName() { 65 return name; 66 } 67 68 public String getExpandMap() { 69 return expandMap ? "true" : "false"; 70 } 71 72 public Object getElement() { 73 return element; 74 } 75 76 public int doStartTag() throws JspTagException { 77 IteratorTag iteratorTag = 78 (IteratorTag) findAncestorWithClass(this, IteratorTag.class); 79 80 if (iteratorTag == null) 81 throw new JspTagException ("IterateNextTag not inside IteratorTag."); 82 83 Iterator iterator = iteratorTag.getIterator(); 84 85 if (iterator == null || !iterator.hasNext()) 86 return SKIP_BODY; 87 88 if (name == null) 89 name = "next"; 90 91 Object element = iterator.next(); 93 94 pageContext.setAttribute(name, element); 95 96 if (expandMap) { 98 Map tempMap = (Map ) element; 99 Iterator mapEntries = tempMap.entrySet().iterator(); 100 101 while (mapEntries.hasNext()) { 102 Map.Entry entry = (Map.Entry ) mapEntries.next(); 103 Object value = entry.getValue(); 104 105 if (value == null) value = new String (); 106 pageContext.setAttribute((String ) entry.getKey(), value); 107 } 108 } 109 110 iteratorTag.setIterator(iterator); 112 113 return EVAL_BODY_AGAIN; 114 } 115 116 public int doAfterBody() { 117 return SKIP_BODY; 118 } 119 120 public int doEndTag() { 121 try { 122 BodyContent body = getBodyContent(); 123 124 if (body != null) { 125 JspWriter out = body.getEnclosingWriter(); 126 String bodyString = body.getString(); 127 body.clearBody(); 128 out.print(bodyString); 129 } 130 } catch (IOException e) { 131 System.out.println("IterateNext Tag error: " + e); 132 } 133 return EVAL_PAGE; 134 } 135 } 136 137 | Popular Tags |