KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > RemoveTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11 import javax.servlet.jsp.JspTagException JavaDoc;
12 import java.util.Collection JavaDoc;
13 import javax.servlet.jsp.PageContext JavaDoc;
14 import javax.servlet.http.*;
15 import org.mmbase.bridge.jsp.taglib.functions.Functions;
16 import org.mmbase.bridge.jsp.taglib.util.*;
17
18 /**
19  * Remove an object from the Context.
20  *
21  * @author Michiel Meeuwissen
22  * @version $Id: RemoveTag.java,v 1.12 2006/06/22 18:15:44 johannes Exp $
23  */

24
25 public class RemoveTag extends ContextReferrerTag {
26
27     private Attribute value = Attribute.NULL;
28     private Attribute from = Attribute.NULL;
29
30     /**
31      * @since MMBase-1.8
32      */

33     public void setValue(String JavaDoc v) throws JspTagException JavaDoc {
34         value = getAttribute(v);
35     }
36
37     /**
38      * @since MMBase-1.8
39      */

40     public void setFrom(String JavaDoc f) throws JspTagException JavaDoc {
41         from = getAttribute(f);
42     }
43
44     /**
45      * @since MMBase-1.8
46      */

47     protected void remove(ContextContainer cc, String JavaDoc referid) throws JspTagException JavaDoc {
48         if (value.getString(this).equals("")) {
49             cc.unRegister(referid);
50         } else {
51             Collection JavaDoc col = (Collection JavaDoc) cc.get(referid);
52             Functions.remove(col, value.getValue(this));
53         }
54     }
55
56     public int doEndTag() throws JspTagException JavaDoc {
57         super.doEndTag();
58         String JavaDoc fromString = from.getString(this);
59         if (! "".equals(fromString)) {
60             boolean useCollection = ! value.getString(this).equals("");
61             Collection JavaDoc col = null;
62             int from = ContextContainer.stringToLocation(fromString);
63             switch(from) {
64             case ContextContainer.LOCATION_PARENT:
65                 if (useCollection) {
66                     col = (Collection JavaDoc) getContextProvider().getContextContainer().getParent().get(getReferid());
67                     break;
68                 } else {
69                     remove(getContextProvider().getContextContainer().getParent(), getReferid());
70                     return EVAL_PAGE;
71                 }
72             case ContextContainer.LOCATION_PAGE:
73                 if (useCollection) {
74                     col = (Collection JavaDoc) pageContext.getAttribute(getReferid());
75                     break;
76                 } else {
77                     pageContext.removeAttribute(getReferid());
78                     return EVAL_PAGE;
79                 }
80             case ContextContainer.LOCATION_REQUEST:
81                 if (useCollection) {
82                     col = (Collection JavaDoc) pageContext.getAttribute(getReferid(), PageContext.REQUEST_SCOPE);
83                     break;
84                 } else {
85                     pageContext.removeAttribute(getReferid(), PageContext.REQUEST_SCOPE);
86                     return EVAL_PAGE;
87                 }
88             case ContextContainer.LOCATION_APPLICATION:
89                 if (useCollection) {
90                     col = (Collection JavaDoc) pageContext.getAttribute(getReferid(), PageContext.APPLICATION_SCOPE);
91                     break;
92                 } else {
93                     pageContext.removeAttribute(getReferid(), PageContext.APPLICATION_SCOPE);
94                     return EVAL_PAGE;
95                 }
96             case ContextContainer.LOCATION_SESSION:
97                 HttpSession session = ((HttpServletRequest) pageContext.getRequest()).getSession(false);
98                 if (session != null) {
99                     if (useCollection) {
100                         col = (Collection JavaDoc) session.getAttribute(getReferid());
101                         break;
102                     } else {
103                         session.removeAttribute(getReferid());
104                     }
105                 }
106                 return EVAL_PAGE;
107             case ContextContainer.LOCATION_THIS:
108                 break;
109             default: {
110                 throw new JspTagException JavaDoc("Unknown value for 'from' attribute " + fromString);
111             }
112             }
113             if (col != null) {
114                 Functions.remove(col, value.getValue(this));
115                 return EVAL_PAGE;
116             }
117         }
118
119         remove(getContextProvider().getContextContainer(), getReferid());
120         return EVAL_PAGE;
121     }
122
123 }
124
Popular Tags