KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.mmbase.bridge.jsp.taglib.util.*;
12 import javax.servlet.jsp.JspTagException JavaDoc;
13 import javax.servlet.http.*;
14
15 import org.mmbase.util.transformers.CharTransformer;
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 import java.util.*;
20
21 /**
22  * The importtag puts things in the context. It can find them from the
23  * environment or from its body.
24  *
25  * @author Michiel Meeuwissen
26  * @see ContextTag
27  * @version $Id: ImportTag.java,v 1.58 2005/05/18 08:04:16 michiel Exp $
28  */

29
30 public class ImportTag extends ContextReferrerTag {
31     private static final Logger log = Logging.getLoggerInstance(ImportTag.class);
32
33     protected Attribute required = Attribute.NULL;
34     protected Attribute from = Attribute.NULL;
35
36     protected Attribute externid = Attribute.NULL;
37     private Attribute reset = Attribute.NULL;
38
39     private boolean found = false;
40     private String JavaDoc useId = null;
41
42     /**
43      * The extern id it the identifier in some external source.
44      */

45
46     public void setExternid(String JavaDoc e) throws JspTagException JavaDoc {
47         externid = getAttribute(e);
48     }
49
50     /**
51      * If 'required' then the variable must be available in the
52      * external source, otherwise exception.
53      *
54      */

55     public void setRequired(String JavaDoc b) throws JspTagException JavaDoc {
56         required = getAttribute(b);
57     }
58
59
60     /**
61      * If 'required' then the variable must be available in the
62      * external source, otherwise exception.
63      *
64      */

65     public void setReset(String JavaDoc b) throws JspTagException JavaDoc {
66         reset = getAttribute(b);
67     }
68
69     /**
70      * From which external source
71      */

72
73     public void setFrom(String JavaDoc s) throws JspTagException JavaDoc {
74         from = getAttribute(s);
75     }
76
77
78     public int doStartTag() throws JspTagException JavaDoc {
79         Object JavaDoc value = null;
80         helper.overrideWrite(false);
81         log.trace("dostarttag of import");
82         
83         if (getId() == null) {
84             log.trace("No id was given, using externid ");
85             useId = (String JavaDoc) externid.getValue(this);
86         } else {
87             useId = getId();
88             if (log.isDebugEnabled()) log.trace("An id was given (" + id + ")");
89         }
90
91         
92         if (externid != Attribute.NULL) {
93
94             boolean res = reset.getBoolean(this, false);
95             if (log.isDebugEnabled()) {
96                 log.trace("Externid was given " + externid.getString(this));
97             }
98             if (from.getString(this).equals("")) {
99                 found = (getContextProvider().getContextContainer().findAndRegister(pageContext, externid.getString(this), useId, ! res) != null);
100             } else {
101                 List fromsList = from.getList(this);
102                 boolean searchThis = fromsList.contains("this") || fromsList.contains("THIS");
103
104                 if (searchThis) {
105                     res = true;
106                 }
107                 Iterator froms = fromsList.iterator();
108                 ContextContainer cc = getContextProvider().getContextContainer();
109                 while (froms.hasNext() && ! found) {
110                     int from = ContextContainer.stringToLocation((String JavaDoc) froms.next());
111                     Object JavaDoc result = cc.find(pageContext, from, externid.getString(this));
112                     if (from == ContextContainer.LOCATION_THIS && result == null) {
113                         result = cc.find(pageContext, from, useId);
114                     }
115                     if (result != null) {
116                         if (! (from == ContextContainer.LOCATION_PARAMETERS || from == ContextContainer.LOCATION_MULTIPART)) {
117                             helper.overrideNoImplicitList();
118                         }
119
120                         cc.register(useId, result, ! res);
121                         found = true;
122                         break;
123                     }
124                 }
125             }
126
127             if (! found && required.getBoolean(this, false)) {
128                 String JavaDoc fromString = from.getString(this).toLowerCase();
129                 if (fromString.equals("session") && ((HttpServletRequest) pageContext.getRequest()).getSession(false) == null) {
130                     throw new JspTagException JavaDoc("Required parameter '" + externid.getString(this) + "' not found in session, because there is no session");
131                 }
132                 throw new JspTagException JavaDoc("Required parameter '" + externid.getString(this) + "' not found " + (fromString.equals("") ? "anywhere" : ("in " + fromString)));
133             }
134             if (found) {
135                 value = getObject(useId);
136                 if (log.isDebugEnabled()) {
137                     log.debug("found value for " + useId + " '" + value + "'");
138                 }
139             }
140         }
141         if (found) {
142             setValue(value, WriterHelper.NOIMPLICITLIST);
143             if (useId != null) {
144                 ContextContainer cc = getContextProvider().getContextContainer();
145                 cc.reregister(useId, helper.getValue());
146             }
147             return SKIP_BODY;
148         } else {
149             setValue(null);
150             return EVAL_BODY_BUFFERED;
151         }
152
153     }
154
155     /**
156      * Retrieves the value from the writer-helper, but escapes if necessary (using 'escape' attribute)
157      * @since MMBase-1.7.2
158      */

159     protected void setValue(Object JavaDoc value, boolean noImplicitList) throws JspTagException JavaDoc {
160         value = getEscapedValue(value);
161         if (log.isDebugEnabled()) {
162             log.debug("Setting " + value + " " + (value == null ? "NULL" : "" + value.getClass()));
163         }
164         helper.setValue(value, noImplicitList);
165     }
166     /**
167      * @since MMBase-1.7.2
168      */

169     protected void setValue(Object JavaDoc value) throws JspTagException JavaDoc {
170         setValue(value, WriterHelper.IMPLICITLIST);
171     }
172
173
174     public int doEndTag() throws JspTagException JavaDoc {
175         if (log.isDebugEnabled()) {
176             log.debug("endtag of import with id:" + id + " externid: " + externid.getString(this));
177         }
178         if (externid != Attribute.NULL) {
179             if (! found ) {
180                 if (log.isDebugEnabled()) log.debug("External Id " + externid.getString(this) + " not found");
181                 // try to find a default value in the body.
182
Object JavaDoc body = bodyContent != null ? bodyContent.getString() : "";
183                 if (! "".equals(body)) { // hey, there is a body content!
184
if (log.isDebugEnabled()) {
185                         log.debug("Found a default in the body (" + body + ")");
186                     }
187                     setValue(body);
188                     getContextProvider().getContextContainer().reregister(useId, helper.getValue());
189                 } else {
190                     // might be vartype="list" or so, still need to set
191
setValue(null);
192                     getContextProvider().getContextContainer().reregister(useId, helper.getValue());
193                 }
194             }
195         } else { // get value from the body of the tag.
196
setValue(bodyContent != null ? bodyContent.getString() : "");
197             if (useId != null) {
198                 if (log.isDebugEnabled()) {
199                     log.debug("Setting " + useId + " to " + helper.getValue());
200                 }
201                 boolean res = reset.getBoolean(this, false);
202                 // should this be more general? Also in other contextwriters?
203

204                 getContextProvider().getContextContainer().register(useId, helper, !res);
205
206             } else {
207                 if (helper.getJspvar() == null) {
208                     found = false; // for use next time
209
useId = null;
210                     throw new JspTagException JavaDoc("Attributes externid, id and jspvar cannot be all missing");
211                 }
212             }
213         }
214         found = false; // for use next time
215
useId = null;
216         bodyContent = null;
217         helper.doEndTag();
218         log.debug("end of importag");
219         super.doEndTag();
220         return EVAL_PAGE;
221     }
222
223
224 }
225
Popular Tags