1 13 package info.magnolia.cms.taglibs; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ContentHandler; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.util.Resource; 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import javax.jcr.RepositoryException; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.jsp.PageContext ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 import org.apache.log4j.Logger; 29 30 31 36 public class ContentNodeIterator extends TagSupport { 37 38 public static final String CONTENT_NODE_COLLECTION_NAME = "contentNodeCollectionName"; 40 protected static final String CURRENT_INDEX = "currentIndex"; 42 protected static final String SIZE = "size"; 44 47 private static final long serialVersionUID = 222L; 48 49 52 private static Logger log = Logger.getLogger(ContentNodeIterator.class); 53 54 private String contentNodeCollectionName; 55 56 private int beginIndex; 57 58 private int endIndex; 59 60 private int step = 1; 61 62 private int size; 63 64 private int currentIndex; 65 66 private Iterator contentNodeIterator; 67 68 72 public void setContainerListName(String name) { 73 this.setContentNodeCollectionName(name); 74 } 75 76 79 public void setContentNodeCollectionName(String name) { 80 this.contentNodeCollectionName = name; 81 } 82 83 86 public void setBegin(String index) { 87 this.beginIndex = (new Integer (index)).intValue(); 88 } 89 90 93 public void setEnd(String index) { 94 this.endIndex = (new Integer (index)).intValue(); 95 } 96 97 100 public void setStep(String step) { 101 this.step = (new Integer (step)).intValue(); 102 } 103 104 107 private int getEnd() { 108 if (this.endIndex == 0) { 109 return this.size; 110 } 111 return this.endIndex; 112 } 113 114 117 public int doStartTag() { 118 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 119 Content page = Resource.getCurrentActivePage(request); 120 try { 121 Collection children = page.getContent(this.contentNodeCollectionName).getChildren( 122 ItemType.CONTENTNODE, 123 ContentHandler.SORT_BY_SEQUENCE); 124 this.size = children.size(); 125 if (this.size == 0) { 126 return SKIP_BODY; 127 } 128 pageContext.setAttribute(ContentNodeIterator.SIZE, new Integer (this.getEnd()), PageContext.REQUEST_SCOPE); 129 pageContext.setAttribute( 130 ContentNodeIterator.CURRENT_INDEX, 131 new Integer (this.currentIndex), 132 PageContext.REQUEST_SCOPE); 133 pageContext.setAttribute( 134 ContentNodeIterator.CONTENT_NODE_COLLECTION_NAME, 135 this.contentNodeCollectionName, 136 PageContext.REQUEST_SCOPE); 137 this.contentNodeIterator = children.iterator(); 138 Resource.setLocalContentNodeCollectionName(request, this.contentNodeCollectionName); 139 for (; this.beginIndex > -1; --this.beginIndex) { 140 Resource.setLocalContentNode(request, (Content) this.contentNodeIterator.next()); 141 } 142 } 143 catch (RepositoryException re) { 144 log.debug(re.getMessage()); 145 return SKIP_BODY; 146 } 147 return EVAL_BODY_INCLUDE; 148 } 149 150 153 public int doAfterBody() { 154 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 155 if (this.contentNodeIterator.hasNext() && (this.currentIndex < this.getEnd())) { 156 this.currentIndex++; 157 pageContext.setAttribute( 158 ContentNodeIterator.CURRENT_INDEX, 159 new Integer (this.currentIndex), 160 PageContext.REQUEST_SCOPE); 161 for (int i = 0; i < this.step; i++) { 162 Resource.setLocalContentNode(request, (Content) this.contentNodeIterator.next()); 163 } 164 return EVAL_BODY_AGAIN; 165 } 166 return SKIP_BODY; 167 } 168 169 172 public int doEndTag() { 173 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 174 Resource.removeLocalContentNode(request); 175 Resource.removeLocalContentNodeCollectionName(request); 176 pageContext.removeAttribute(ContentNodeIterator.CURRENT_INDEX); 177 pageContext.removeAttribute(ContentNodeIterator.SIZE); 178 pageContext.removeAttribute(ContentNodeIterator.CONTENT_NODE_COLLECTION_NAME); 179 this.beginIndex = 0; 180 this.endIndex = 0; 181 this.step = 1; 182 this.size = 0; 183 this.currentIndex = 0; 184 return EVAL_PAGE; 185 } 186 187 190 public void release() { 191 this.contentNodeCollectionName = null; 192 this.contentNodeIterator = null; 193 this.beginIndex = 0; 194 this.endIndex = 0; 195 this.step = 1; 196 this.size = 0; 197 this.currentIndex = 0; 198 super.release(); 199 } 200 201 } 202 | Popular Tags |