1
2
3
4
5
6 package org.repoweb.tag;
7 import java.io.IOException;
8 import java.util.Iterator;
9 import javax.servlet.jsp.JspException;
10 import javax.servlet.jsp.JspTagException;
11 import javax.servlet.jsp.JspWriter;
12 import javax.servlet.jsp.tagext.TagSupport;
13 import org.repoweb.model.Artifact;
14 import org.repoweb.pom.Dependency;
15 import org.repoweb.pom.Project;
16 import org.repoweb.pom.Property;
17 /***
18 * Tag to display dependency list.
19 */
20 public class DependencyTag extends TagSupport {
21 private static final String DEPTH = " ";
22
23 public int doStartTag() throws JspException {
24 try {
25 final Object obj = pageContext.getAttribute(getId());
26 if (obj instanceof Project) {
27 Project pom = (Project)obj;
28 pageContext.getOut().write("<dependencies><br>");
29 for (Iterator iter = pom.getDependencies().iterator(); iter.hasNext();) {
30 display((Dependency)iter.next());
31 }
32 pageContext.getOut().write("</dependencies><br>");
33 }
34 else {
35 Artifact art = (Artifact)obj;
36 Dependency wrapper =
37 new Dependency(art.getGroupId(), art.getArtifactId(), art.getVersion());
38 if (art.isOverridden()) {
39 wrapper.setJar(art.getFileName());
40 }
41 wrapper.setType(art.getType());
42 display(wrapper);
43 }
44 }
45 catch (IOException e) {
46 throw new JspTagException("An IOException occurred.");
47 }
48 return SKIP_BODY;
49 }
50
51
52 private void display(Dependency dep) throws IOException {
53 addSpace(1);
54 pageContext.getOut().write("<dependency><br>");
55
56 if (dep.getGroupId().equals(dep.getArtifactId())) {
57 writeTag("id", dep.getArtifactId(), LinkTag.detailUrl(pageContext, dep), 2);
58 }
59 else {
60 writeTag("groupId", dep.getGroupId(), 2);
61 writeTag("artifactId", dep.getArtifactId(),
62 LinkTag.detailUrl(pageContext, dep), 2);
63 }
64 writeTag("version", dep.getVersion(), 2);
65 writeTag("jar", dep.getJar(), 2);
66 if (!dep.isDefaultType()) {
67 writeTag("type", dep.getType(), 2);
68 }
69 writeTag("url", dep.getUrl(), dep.getUrl(), 2);
70
71 if (hasProperties(dep)) {
72 addSpace(2);
73 pageContext.getOut().write("<properties><br>");
74 for (Iterator props = dep.getProperties().iterator(); props.hasNext();) {
75 Property property = (Property)props.next();
76 writeTag(property.getId(), property.getValue(), 3);
77 }
78 addSpace(2);
79 pageContext.getOut().write("</properties><br>");
80 }
81
82 addSpace(1);
83 pageContext.getOut().write("</dependency><br>");
84 }
85
86
87 private boolean hasProperties(Dependency dep) {
88 return dep.getProperties() != null && dep.getProperties().size() > 0;
89 }
90
91
92 private void writeTag(String name, String value, int depth)
93 throws IOException {
94 writeTag(name, value, null, depth);
95 }
96
97
98 private void writeTag(String name, String value, String uri, int depth)
99 throws IOException {
100 if (null == value) {
101 return;
102 }
103
104 addSpace(depth);
105
106 JspWriter out = pageContext.getOut();
107 out.write("<");
108 out.write(name);
109 out.write(">");
110 if (uri != null) {
111 out.write("<a href=\"");
112 out.write(uri);
113 out.write("\">");
114 }
115 out.write(value);
116 if (uri != null) {
117 out.write("</a>");
118 }
119 out.write("</");
120 out.write(name);
121 out.write("><br>");
122 }
123
124
125 private void addSpace(int depth) throws IOException {
126 for (int current = 0; current < depth; current++) {
127 pageContext.getOut().write(DEPTH);
128 }
129 }
130 }