1 package org.andromda.taglibs.breadcrumbs;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.jsp.JspException;
5 import javax.servlet.jsp.PageContext;
6 import javax.servlet.jsp.tagext.Tag;
7 import javax.servlet.jsp.tagext.TagSupport;
8
9
10
11
12 public class AddTag extends TagSupport
13 {
14 private static final long serialVersionUID = 34L;
15 private String value = null;
16
17
18
19
20 public String getValue()
21 {
22 return value;
23 }
24
25
26
27
28 public void setValue(String value)
29 {
30 this.value = value;
31 }
32
33
34
35
36 public int doStartTag() throws JspException
37 {
38 Object breadCrumbsObject = this.pageContext.getAttribute(BreadCrumbs.SESSION_KEY, PageContext.SESSION_SCOPE);
39
40 if (breadCrumbsObject == null)
41 {
42 BreadCrumbs breadCrumbs = new BreadCrumbs();
43 this.pageContext.setAttribute(BreadCrumbs.SESSION_KEY, breadCrumbs, PageContext.SESSION_SCOPE);
44 addToBreadCrumbs(breadCrumbs);
45 }
46 else if (breadCrumbsObject instanceof BreadCrumbs)
47 {
48 addToBreadCrumbs((BreadCrumbs) breadCrumbsObject);
49 }
50
51 return Tag.SKIP_BODY;
52 }
53
54 private void addToBreadCrumbs(BreadCrumbs breadCrumbs)
55 {
56 HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
57
58 BreadCrumb newCrumb = new BreadCrumb(request.getRequestURL().toString(), value);
59
60 if (breadCrumbs.isEmpty() == false)
61 {
62 BreadCrumb lastCrumb = (BreadCrumb) breadCrumbs.getLast();
63 if (lastCrumb.hasSimilarProperties(newCrumb))
64 {
65 breadCrumbs.set(breadCrumbs.size() - 1, newCrumb);
66 return;
67 }
68 }
69 breadCrumbs.add(newCrumb);
70 }
71 }