1 package org.jahia.taglibs.container; 2 3 import java.io.IOException ; 4 import java.util.Enumeration ; 5 6 import javax.servlet.ServletRequest ; 7 import javax.servlet.jsp.JspException ; 8 import javax.servlet.jsp.JspTagException ; 9 import javax.servlet.jsp.tagext.BodyTagSupport ; 10 11 import org.jahia.data.JahiaData; 12 import org.jahia.data.beans.ContainerBean; 13 import org.jahia.data.containers.JahiaContainer; 14 import org.jahia.data.containers.JahiaContainerList; 15 16 17 23 public class ContainerTag extends BodyTagSupport { 24 25 private static org.apache.log4j.Logger logger = 26 org.apache.log4j.Logger.getLogger(ContainerTag.class); 27 28 private Enumeration containers; 29 30 private boolean first = true; 31 private boolean last = true; 32 private JahiaContainer container = null; 33 private JahiaContainer firstContainer = null; 34 private int counter = 1; 35 private JahiaData jData = null; 36 37 private boolean initLoop = true; 38 39 private boolean display = true; 40 41 public boolean displayBody() { 42 return display; 43 } 44 45 public boolean isFirst() { 46 return this.first; 47 } 48 49 public boolean isLast() { 50 return this.last; 51 } 52 53 public JahiaContainer getContainer() { 54 return this.container; 55 } 56 57 public ContainerBean getContainerBean() { 58 if (this.container == null) { 59 return null; 60 } 61 return new ContainerBean(this.container, jData.params()); 62 } 63 64 public int getCounter() { 65 return this.counter; 66 } 67 68 public int doStartTag() { 69 initLoop = true; 70 71 ContainerListTag cListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class); 72 JahiaContainerList containerList = cListTag.getContainerList(); 73 if (containerList == null) 74 return SKIP_BODY; 75 76 if (initLoop) { 77 ServletRequest request = pageContext.getRequest(); 78 jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 79 containers = containerList.getContainers(); 82 int ct = 0; 83 if (containers.hasMoreElements()) { 84 this.container = (JahiaContainer)containers.nextElement(); 85 if (getId() != null) { 86 pageContext.setAttribute(getId(), getContainerBean()); 87 } 88 if (ct == 0) { 89 this.firstContainer = this.container; 90 } 91 if (containers.hasMoreElements()) { 92 this.last = false; 93 } 94 } else { 95 this.counter = 0; 96 this.display = false; 97 } 98 containers = containerList.getContainers(); 99 if (containers.hasMoreElements()) { 100 this.container = (JahiaContainer) containers.nextElement(); 101 if (getId() != null) { 102 pageContext.setAttribute(getId(), getContainerBean()); 103 } 104 } else { 105 return SKIP_BODY; 106 } 107 initLoop = false; 108 } 109 return EVAL_BODY_BUFFERED; 110 } 111 112 113 public int doAfterBody() throws JspException { 115 ContainerListTag cListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class); 117 118 if (this.display) { 119 try { 120 getBodyContent().writeOut(getPreviousOut()); 121 getBodyContent().clear(); 122 this.counter++; 123 } catch (IOException ioe) { 124 throw new JspTagException (); 125 } 126 if (containers.hasMoreElements()) { 127 this.container = (JahiaContainer)containers.nextElement(); 129 if (getId() != null) { 130 pageContext.setAttribute(getId(), getContainerBean()); 131 } 132 this.first = false; 134 if (!containers.hasMoreElements()) { 136 this.last = true; 137 } 138 return EVAL_BODY_BUFFERED; 139 } else { 140 this.display = false; 141 } 142 } 143 return SKIP_BODY; 144 } 145 146 public int doEndTag() throws JspException { 147 super.doEndTag(); 150 containers = null; 151 152 first = true; 153 last = true; 154 container = null; 155 firstContainer = null; 156 counter = 1; 157 158 initLoop = true; 159 jData = null; 160 161 display = true; 162 if (getId() != null) { 163 pageContext.removeAttribute(getId()); 164 } 165 return EVAL_PAGE; 166 } 167 168 169 } 170 | Popular Tags |