1
2
3
4
5
6 package org.repoweb.tag;
7 import java.util.Iterator;
8 import javax.servlet.jsp.JspException;
9 import javax.servlet.jsp.tagext.TagSupport;
10 import org.repoweb.model.ArtifactList;
11 import org.repoweb.model.ArtifactListFilter;
12 /***
13 * Tag to iterate on artifact list.
14 */
15 public class IterateTag extends TagSupport {
16 private String _collection;
17 private String _property;
18 private Iterator _iter;
19 private boolean _reverse = false;
20 private String _forGroupId;
21 private String _forType;
22 private String _forArtifactId;
23 private String _forVersion;
24
25 public int doStartTag() throws JspException {
26 final ArtifactList list = getArtifactList();
27 if (list != null) {
28 ArtifactListFilter filter = newFilter(list);
29 fillFilterValue(filter);
30
31 _iter = filter.applyFilter();
32 if (_iter.hasNext()) {
33 pageContext.setAttribute(getId(), _iter.next());
34 return EVAL_BODY_AGAIN;
35 }
36 }
37 return SKIP_BODY;
38 }
39
40
41 private void fillFilterValue(ArtifactListFilter filter) {
42 if (_forGroupId != null) {
43 filter.setGroupId(getFilter(_forGroupId));
44 }
45 if (_forVersion != null) {
46 filter.setVersion(getFilter(_forVersion));
47 }
48 if (_forType != null) {
49 filter.setType(getFilter(_forType));
50 }
51 if (_forArtifactId != null) {
52 filter.setArtifactId(getFilter(_forArtifactId));
53 }
54 if (_reverse) {
55 filter.setReverse(true);
56 }
57 }
58
59
60 private ArtifactListFilter newFilter(final ArtifactList list) {
61 if (_property == null) {
62 return list.newArtifactFilter();
63 }
64 else {
65 return list.newPropertyFilter(_property);
66 }
67 }
68
69
70 public int doAfterBody() throws JspException {
71 if (_iter.hasNext()) {
72 pageContext.setAttribute(getId(), _iter.next());
73 return EVAL_BODY_AGAIN;
74 }
75 else {
76 return SKIP_BODY;
77 }
78 }
79
80
81 public void release() {
82 super.release();
83 _iter = null;
84 }
85
86
87 private ArtifactList getArtifactList() {
88 return (ArtifactList)pageContext.getAttribute(_collection);
89 }
90
91
92 private String getFilter(String key) {
93 return (String)pageContext.getAttribute(key);
94 }
95
96
97 public void setCollection(String collection) {
98 this._collection = collection;
99 }
100
101
102 public void setProperty(String property) {
103 this._property = property;
104 }
105
106
107 public void setReverse(boolean reverse) {
108 _reverse = reverse;
109 }
110
111
112 public void setForGroupId(String forGroupId) {
113 _forGroupId = forGroupId;
114 }
115
116
117 public void setForType(String forType) {
118 _forType = forType;
119 }
120
121
122 public void setForArtifactId(String forArtifactId) {
123 _forArtifactId = forArtifactId;
124 }
125
126
127 public void setForVersion(String forVersion) {
128 _forVersion = forVersion;
129 }
130
131
132 public String toString() {
133 return "IterateTag{" + "_property='" + _property + "'" + ", _version='"
134 + _forVersion + "'" + ", _artifactId='" + _forArtifactId + "'" + ", _type='"
135 + _forType + "'" + ", _groupId='" + _forGroupId + "'" + ", _reverse=" + _reverse
136 + ", _iter=" + _iter + "}";
137 }
138 }