KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > util > ContextContainer


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.util;
11
12 import org.mmbase.bridge.jsp.taglib.*;
13 import javax.servlet.jsp.JspTagException JavaDoc;
14 import javax.servlet.jsp.PageContext JavaDoc;
15 import javax.servlet.http.*;
16
17 import java.util.*;
18 import java.lang.reflect.*;
19
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22
23 /**
24  * This is a HashMap, but the keys can contain 'dots', in which case
25  * there is searched for HashMaps in the HashMap.
26  *
27  * @author Michiel Meeuwissen
28  * @version $Id: ContextContainer.java,v 1.53.2.2 2006/11/21 20:39:56 michiel Exp $
29  **/

30
31 public abstract class ContextContainer extends AbstractMap implements Map {
32     private static final Logger log = Logging.getLoggerInstance(ContextContainer.class);
33
34     public static final int LOCATION_NOTSET = -10;
35     public static final int LOCATION_PAGE = 0;
36     public static final int LOCATION_PARENT = 5; // uses parent Contex, if there is one.
37
public static final int LOCATION_PARAMETERS = 10;
38     public static final int LOCATION_MULTIPART = 20;
39     public static final int LOCATION_MULTIPART_OPT = 21;
40     public static final int LOCATION_SESSION = 30;
41     public static final int LOCATION_COOKIE = 40;
42     public static final int LOCATION_ATTRIBUTES = 50;
43     public static final int LOCATION_REQUEST = 50;
44     public static final int LOCATION_APPLICATION = 55;
45     public static final int LOCATION_THIS = 60; // current value, if there is one
46

47
48     public static int stringToLocation(String JavaDoc s) throws JspTagException JavaDoc {
49         int location;
50         s = s.toLowerCase();
51         if ("parent".equals(s)) {
52             location = LOCATION_PARENT;
53         } else if ("page".equals(s)) {
54             location = LOCATION_PAGE;
55         } else if ("session".equals(s)) {
56             location = LOCATION_SESSION;
57         } else if ("parameters".equals(s)) {
58             location = LOCATION_PARAMETERS;
59         } else if ("parameter".equals(s)) {
60             location = LOCATION_PARAMETERS;
61         } else if ("postparameters".equals(s)) { // backward compatible
62
location = LOCATION_MULTIPART;
63         } else if ("multipart".equals(s)) {
64             location = LOCATION_MULTIPART;
65         } else if ("multipart?".equals(s)) {
66             location = LOCATION_MULTIPART_OPT;
67         } else if ("cookie".equals(s)) {
68             location = LOCATION_COOKIE;
69         } else if ("attributes".equals(s)) {
70             location = LOCATION_REQUEST;
71         } else if ("request".equals(s)) {
72             location = LOCATION_REQUEST;
73         } else if ("application".equals(s)) {
74             location = LOCATION_APPLICATION;
75         } else if ("this".equals(s)) {
76             location = LOCATION_THIS;
77         } else {
78             throw new JspTagException JavaDoc("Unknown location '" + s + "'");
79         }
80         return location;
81     }
82
83     public static String JavaDoc locationToString(int i) {
84         switch(i) {
85         case LOCATION_PARENT: return "parent";
86         case LOCATION_PARAMETERS: return "parameters";
87         case LOCATION_SESSION: return "session";
88         case LOCATION_PAGE: return "page";
89         case LOCATION_MULTIPART: return "multipart";
90         case LOCATION_MULTIPART_OPT: return "multipart?";
91         case LOCATION_COOKIE: return "cookie";
92         case LOCATION_REQUEST: return "request";
93         case LOCATION_APPLICATION: return "application";
94         case LOCATION_THIS: return "this";
95         default: return "<>";
96         }
97     }
98
99
100
101     private static boolean isContextVarNameChar(char c) {
102         return Character.isLetter(c) || Character.isDigit(c) || c == '_';
103     }
104     static boolean isContextIdentifierChar(char c) {
105         return isContextVarNameChar(c) || c == '.';
106         // || c =='/'; // / for forward compatibility?
107
}
108
109
110     /**
111      * Returns the Map which will is used for actually storing stuff.
112      *
113      * @since MMBase-1.8
114      */

115     protected abstract Backing getBacking();
116
117
118     public void release(PageContext JavaDoc pc, ContextContainer p) {
119         getBacking().pullPageContext(pc);
120         // restore also the parent.xb
121
parent = p;
122     }
123
124
125     public Set entrySet() {
126         return getBacking().entrySet();
127     }
128
129
130     private final String JavaDoc id;
131     protected ContextContainer parent;
132
133
134     /**
135      * Since a ContextContainer can contain other ContextContainer, it
136      * has to know which ContextContainer contains this. And it also
137      * has an id.
138      */

139
140     public ContextContainer(String JavaDoc i, ContextContainer p) {
141         id = i;
142         parent = p;
143     }
144
145     public String JavaDoc getId() {
146         return id;
147     }
148
149     /**
150      * @since MMBase-1.8
151      */

152     public void setParent(PageContext JavaDoc pc, ContextContainer p) {
153         getBacking().pushPageContext(pc);
154         parent = p;
155     }
156
157     /**
158      * @since MMBase-1.7
159      */

160     public ContextContainer getParent() {
161         return parent;
162     }
163
164     /**
165      * @since MMBase-1.8.3
166      */

167     public PageContext JavaDoc getPageContext() {
168         return getBacking().getPageContext();
169     }
170
171
172     /**
173      * Keys must be Strings, so put(Object, ..) is forbidden in this HashMap!
174      */

175
176     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
177         if (key instanceof String JavaDoc) {
178             try {
179                 return put((String JavaDoc) key, value);
180             } catch (JspTagException JavaDoc e) {
181                 throw new RuntimeException JavaDoc(e);
182             }
183         } else {
184             throw new RuntimeException JavaDoc("Error, key should be string in ContextContainers! (Tried " + key.getClass().getName() + ")");
185         }
186     }
187
188     /**
189      * Not all Strings can be allowed as keys. Keys are like variable names.
190      */

191
192     public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) throws JspTagException JavaDoc {
193         if (key.indexOf('.') != -1) {
194             throw new JspTagException JavaDoc("Key may not contain dots (" + key + ")");
195         }
196         return getBacking().put(key, value);
197     }
198     public boolean containsKey(Object JavaDoc key) {
199         throw new RuntimeException JavaDoc("Error, key should be string in ContextContainers!");
200     }
201
202     /**
203      * This function takes a key, which can contain dots. It returns
204      * a structure with a new ContextContainer and the rest of the
205      * key. It also returns if it had to 'go down' to find this,
206      * because if it has done this, the next time you may not go up
207      * again (check the parent).
208      *
209      */

210
211     protected Pair getPair(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
212         // checking if key contains a 'dot'.
213
int dotPos = -1;
214         {
215             int pos = 0;
216             int keyLength = key.length();
217             while (pos < keyLength) {
218                 char c = key.charAt(pos);
219                 if (c == '.') {
220                     dotPos = pos;
221                     break;
222                 }
223                 if (! isContextVarNameChar(c)) return null;
224                 pos++;
225             }
226         }
227         if (dotPos > 0) {
228             String JavaDoc contextKey = key.substring(0, dotPos);
229             // find the Context:
230
boolean wentDown = true;
231             Object JavaDoc c = simpleGet(contextKey, checkParent);
232             if(c == null && checkParent && parent != null) {
233                 c = parent.get(key, true);
234                 wentDown = false;
235             }
236
237             if(c == null) {
238                 throw new JspTagException JavaDoc("Context '" + contextKey+ "' could not be found.");
239             }
240             String JavaDoc newKey = key.substring(dotPos + 1);
241             // and search with that one:
242
if (c instanceof ContextContainer) {
243                 return new ContextContainerPair((ContextContainer)c, newKey, wentDown);
244             } else if (c instanceof Map) {
245                 return new MapPair((Map)c, newKey, wentDown);
246             } else {
247                 return new BeanPair(c, newKey, wentDown);
248             }
249         } else {
250             return null;
251         }
252
253     }
254     /**
255      * Like containsKey but doesn't check for dots.
256      */

257     private boolean simpleContainsKey(String JavaDoc key, boolean checkParent) {
258         boolean result = getBacking().containsKey(key);
259         if (!result && checkParent && parent != null) {
260             result = parent.simpleContainsKey(key, true);
261         }
262         return result;
263     }
264
265     /**
266      * Like the containsKey of HashMap.
267      *
268      * @param key The key to search
269      * @param checkParent If this is false, it will only look in the current Container (and below).
270      */

271
272     public boolean containsKey(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
273         Pair p = getPair(key, checkParent);
274         if (p == null) {
275             return simpleContainsKey(key, checkParent);
276         } else {
277             return p.containsKey(p.restKey, ! p.wentDown);
278         }
279     }
280
281     public boolean containsKey(String JavaDoc key) throws JspTagException JavaDoc {
282         return containsKey(key, true);
283     }
284
285     /**
286      * Like get, but does not try to search dots, because you know already that there aren't.
287      */

288     protected Object JavaDoc simpleGet(String JavaDoc key, boolean checkParent) { // already sure that there is no dot.
289
Object JavaDoc result = getBacking().getOriginal(key);
290         if (result == null && checkParent && parent != null) {
291             return parent.simpleGet(key, true);
292         }
293         return result;
294     }
295
296     /**
297      * Like get, but you can explicity indicate if to search 'parent' Contextes as well.
298      */

299
300     public Object JavaDoc get(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
301         Pair p = getPair(key, checkParent);
302         if (p == null) {
303             return simpleGet(key, checkParent);
304         } else {
305             return p.get(p.restKey, ! p.wentDown);
306         }
307     }
308
309     /**
310      *
311      */

312
313     public Object JavaDoc get(String JavaDoc key) throws JspTagException JavaDoc {
314         return get(key, true);
315     }
316
317     public Object JavaDoc get(Object JavaDoc key) {
318         if (!(key instanceof String JavaDoc)) {
319             return null;
320         }
321         try {
322             return get((String JavaDoc)key, true);
323         } catch (JspTagException JavaDoc e) {
324             log.warn("Exception when trying to get value '" + key + "': " + e);
325         }
326         return null;
327     }
328
329     public Set keySet() {
330         HashSet result = new HashSet(getBacking().keySet());
331         if (parent != null) {
332             result.addAll(parent.keySet());
333         }
334         return result;
335     }
336
337     /**
338      * @since MMBase-1.7
339      */

340     Set keySet(boolean checkParent) {
341         if (checkParent) {
342             return keySet();
343         } else {
344             return getBacking().keySet();
345         }
346     }
347
348     // utilities, since MMBase-1.7 moved from ContextTag to here.
349

350     /**
351      * @since MMBase-1.7 (here)
352      */

353     public Object JavaDoc getObject(String JavaDoc key) throws JspTagException JavaDoc {
354         if (! containsKey(key, true)) { // do check parent.
355
throw new JspTagException JavaDoc("Object '" + key + "' is not registered. Registered are " + keySet());
356         }
357         if (log.isDebugEnabled()) {
358             log.debug("Getting '" + key + "' from container " + this);
359         }
360         return get(key);
361     }
362
363     /**
364      * @since MMBase-1.7 (here)
365      */

366     public void register(String JavaDoc newId, Object JavaDoc n, boolean check) throws JspTagException JavaDoc {
367         register(newId, n, check, true);
368     }
369
370     /**
371      * @since MMBase-1.8
372      */

373     public void register(String JavaDoc newId, WriterHelper helper, boolean check) throws JspTagException JavaDoc {
374         register(newId, helper.getValue(), check, true);
375     }
376
377     /**
378      * @since MMBase-1.7
379      */

380     protected void register(String JavaDoc newId, Object JavaDoc n, boolean check, boolean checkParent) throws JspTagException JavaDoc {
381         if (log.isDebugEnabled()) {
382             log.trace("registering " + n + " a (" + (n != null ? n.getClass().getName() :"")+ ") under " + newId + " with context " + id);
383         }
384         // first check if current context is specified
385
Pair pair = getPair(newId, checkParent);
386
387         if (pair != null) {
388             pair.register(pair.restKey, n, check, ! pair.wentDown);
389
390         } else {
391
392             // Check if the id is a valid identifier
393
// A valid id must begin with a letter or underscore, followed
394
// by letters, underscores and digits.
395
boolean valid = true;
396             char chars[] = newId.toCharArray();
397             if (chars.length < 1) {
398                 log.debug("Id must be longer then 0");
399                 valid = false;
400             } else {
401                 if (Character.isLetter(chars[0]) || chars[0] == '_') {
402                     if (log.isDebugEnabled()) log.debug("First character is valid, checking the rest of " + newId);
403                     for (int i = 1; i < chars.length; ++i) {
404                         if (! isContextVarNameChar(chars[i])) {
405                             valid = false;
406                             break;
407                         }
408                     }
409                 } else {
410                     if (log.isDebugEnabled()) log.debug("First character is not valid: " + chars[0]);
411                     valid = false;
412                 }
413             }
414             if (! valid) {
415                 JspTagException JavaDoc exception = new TaglibException ("'" + newId + "' is not a valid Context identifier", new Throwable JavaDoc());
416                 log.info(Logging.stackTrace(exception));
417                 throw exception;
418             }
419
420             log.debug("Valid");
421             //pageContext.setAttribute(id, n);
422
if ((! newId.equals("_")) && check && isRegistered(newId)) {
423                 JspTagException JavaDoc e = new JspTagException JavaDoc("Object with id " + newId + " was already registered in " + this);
424                 if (log.isDebugEnabled()) {
425                     log.debug(Logging.stackTrace(e));
426                 }
427                 throw e;
428             }
429             if (log.isDebugEnabled()) {
430                 log.debug("putting '" + newId + "'/'" + n + "' in " + this);
431             }
432             put(newId, n);
433         }
434     }
435
436     /**
437      * @since MMBase-1.7 (here)
438      */

439     public void register(String JavaDoc newId, Object JavaDoc n) throws JspTagException JavaDoc {
440         register(newId, n, true);
441     }
442
443     /**
444      * @since MMBase-1.7
445      */

446     void registerAll(Map map) throws JspTagException JavaDoc {
447         if (map == null) return;
448         Iterator i = map.entrySet().iterator();
449         while (i.hasNext()) {
450             Map.Entry entry = (Map.Entry) i.next();
451             String JavaDoc key = (String JavaDoc) entry.getKey();
452             register(key, entry.getValue());
453         }
454
455     }
456     /**
457      * @since MMBase-1.7
458      */

459     void unRegisterAll(Set set) throws JspTagException JavaDoc {
460         if (set == null) return;
461         Iterator i = set.iterator();
462         while (i.hasNext()) {
463             String JavaDoc key = (String JavaDoc) i.next();
464             unRegister(key);
465         }
466
467     }
468     /**
469      * @since MMBase-1.7 (here)
470      */

471     public void registerNode(String JavaDoc newId, org.mmbase.bridge.Node n) throws JspTagException JavaDoc {
472         register(newId, n);
473     }
474     /**
475      * @since MMBase-1.7 (here)
476      */

477     public boolean isRegistered(String JavaDoc key) throws JspTagException JavaDoc {
478         return getBacking().containsOwnKey(key);
479     }
480
481     /**
482      * @since MMBase-1.7 (here)
483      */

484     public void unRegister(String JavaDoc key) throws JspTagException JavaDoc {
485         unRegister(key, true);
486     }
487
488     /**
489      * @since MMBase-1.7
490      */

491     protected void unRegister(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
492         if (log.isDebugEnabled()) {
493             log.debug("removing object '" + key + "' from Context '" + id + "'");
494         }
495         Pair pair = getPair(key, checkParent);
496         if (pair != null) {
497             pair.unRegister(pair.restKey, ! pair.wentDown);
498         } else {
499             remove(key);
500         }
501     }
502     /**
503      * @since MMBase-1.7 (here)
504     */

505     public void reregister(String JavaDoc id, Object JavaDoc n) throws JspTagException JavaDoc {
506         unRegister(id);
507         register(id, n);
508     }
509
510     /**
511      * Java Servlet Specification Version 2.3 SRV.4.9
512      * says that a servlet engine should read a request
513      * as ISO-8859-1 if request.getCharacterEncoding()
514      * returns null. We override this behaviour because
515      * the browser propably sends the request in the
516      * same encoding as the html was send to te browser
517      * And it is likely that the html was send to the
518      * browser in the same encoding as the MMBase
519      * encoding property.
520      * @since MMBase-1.8
521      */

522     protected static Object JavaDoc fixEncoding(Object JavaDoc value, String JavaDoc encoding) throws TaglibException {
523         if(value instanceof String JavaDoc) {
524             try {
525                 value = new String JavaDoc(((String JavaDoc)value).getBytes("ISO-8859-1"), encoding);
526             } catch (java.io.UnsupportedEncodingException JavaDoc e) {
527                 throw new TaglibException("Unsupported Encoding ", e);
528             }
529         } else if (value instanceof List) {
530             ListIterator i = ((List)value).listIterator();
531             while(i.hasNext()) {
532                 i.set(fixEncoding(i.next(), encoding));
533             }
534         }
535         return value;
536
537     }
538     /**
539      * @since MMBase-1.8
540      */

541     public static Object JavaDoc fixEncoding(Object JavaDoc value, PageContext JavaDoc pageContext) throws TaglibException {
542         HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
543         String JavaDoc enc = req.getCharacterEncoding();
544         if (enc == null) {
545             enc = getDefaultCharacterEncoding(pageContext);
546         } else {
547             // I think this happens seldom, if ever.
548
log.debug("form encoding specified: " + enc);
549         }
550
551         if (enc.equalsIgnoreCase("ISO-8859-1")) {
552             enc = "CP1252";
553         }
554         log.debug("Fixing char encoding of " + value + " to " + enc);
555         return fixEncoding(value, enc);
556     }
557
558     /**
559      * @since MMBase-1.7
560      */

561
562     public Object JavaDoc find(PageContext JavaDoc pageContext, int from, String JavaDoc referId) throws JspTagException JavaDoc {
563         Object JavaDoc result = null;
564         switch (from) {
565         case LOCATION_COOKIE:
566             javax.servlet.http.Cookie JavaDoc[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies();
567             if (cookies == null) {
568                 log.debug("Cannot use cookies");
569             } else {
570                 log.debug("Found cookies");
571                 for (int i=0; i< cookies.length; i++) {
572                     if (log.isDebugEnabled()) {
573                         log.debug(cookies[i].getName() + "/" + cookies[i].getValue());
574                     }
575                     if (cookies[i].getName().equals(referId)) {
576                         // simply return the first value found.
577
// this is probably a little to simple...
578
// since a cookie can e.g. also have another path.
579
result = cookies[i].getValue();
580                         break;
581                     }
582                 }
583             }
584             break;
585         case LOCATION_SESSION:
586             if (((HttpServletRequest) pageContext.getRequest()).getSession(false) == null) {
587                 log.debug("Cannot use session if session is disabled");
588             } else {
589                 result = ((HttpServletRequest) pageContext.getRequest()).getSession(false).getAttribute(referId);
590             }
591             break;
592         case LOCATION_MULTIPART_OPT:
593         case LOCATION_MULTIPART:
594             if (MultiPart.isMultipart(pageContext)) {
595                 if (log.isDebugEnabled()) {
596                     log.debug("searching " + referId + " in multipart post");
597                 }
598                 MultiPart.MMultipartRequest mp = MultiPart.getMultipartRequest(pageContext);
599                 if (mp.isFile(referId)) {
600                     result = mp.getFileItem(referId);
601                 } else {
602                     result = fixEncoding(mp.getParameterValues(referId), pageContext);
603                 }
604                 //result = MultiPart.getMultipartRequest(pageContext).getParameterValues(referId);
605
} else {
606                 if (from == LOCATION_MULTIPART) {
607                     throw new JspTagException JavaDoc("Trying to read from multipart post, while request was not a multipart post");
608                 }
609                 }
610             break;
611         case LOCATION_PARAMETERS: {
612             if (log.isDebugEnabled()) {
613                 log.debug("searching parameter " + referId);
614             }
615             HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
616             String JavaDoc[] resultvec = req.getParameterValues(referId);
617             if (resultvec != null) {
618                 if (log.isDebugEnabled()) log.debug("Found: " + resultvec);
619                 if (resultvec.length > 1) {
620                     result = (List) fixEncoding(java.util.Arrays.asList(resultvec), pageContext);
621                 } else {
622                     result = fixEncoding(resultvec[0], pageContext);
623                 }
624             }
625         }
626         break;
627         case LOCATION_PARENT:
628             if (parent != null) {
629                 if (parent.containsKey(referId, true)) {
630                     result = parent.get(referId);
631                     if (result == this) { // don't find this tag itself...
632
result = null;
633                     }
634                 }
635             }
636             break;
637         case LOCATION_PAGE:
638             result = pageContext.getAttribute(referId);
639             break;
640         case LOCATION_REQUEST:
641             result = pageContext.getAttribute(referId, PageContext.REQUEST_SCOPE);
642             break;
643         case LOCATION_APPLICATION:
644             result = pageContext.getAttribute(referId, PageContext.APPLICATION_SCOPE);
645             break;
646         case LOCATION_THIS:
647             result = simpleGet(referId, false);
648             break;
649         default:
650             result = null;
651         }
652         return result;
653
654     }
655
656     /**
657      * @since MMBase-1.7
658      */

659
660     public Object JavaDoc find(PageContext JavaDoc pageContext, String JavaDoc externid) throws JspTagException JavaDoc {
661         log.debug("searching in parent");
662         Object JavaDoc result;
663         result = find(pageContext, LOCATION_PARENT, externid);
664         if (result != null) return result;
665         log.debug("searching in attributes");
666         result = find(pageContext, LOCATION_ATTRIBUTES, externid);
667         if (result != null) return result;
668         log.debug("searching in parameters");
669         result = find(pageContext, LOCATION_PARAMETERS, externid);
670         if (result != null) return result;
671         if (MultiPart.isMultipart(pageContext)) {
672             log.debug("searching in multipart post");
673             result = find(pageContext, LOCATION_MULTIPART, externid);
674             if (result != null) return result;
675         }
676         if (((HttpServletRequest) pageContext.getRequest()).getSession(false) != null) {
677             log.debug("searching in session");
678             result = find(pageContext, LOCATION_SESSION, externid);
679         }
680         return result;
681     }
682
683     /**
684      * Searches a key in request, postparameters, session, parent
685      * context and registers it in this one.
686      *
687      * Returns null if it could not be found.
688      */

689     public Object JavaDoc findAndRegister(PageContext JavaDoc pageContext, int from, String JavaDoc referId, String JavaDoc newId) throws JspTagException JavaDoc {
690         return findAndRegister(pageContext, from, referId, newId, true);
691     }
692
693     public Object JavaDoc findAndRegister(PageContext JavaDoc pageContext, int from, String JavaDoc referId, String JavaDoc newId, boolean check) throws JspTagException JavaDoc {
694         if (newId == null) {
695             throw new JspTagException JavaDoc("Cannot register with id is null");
696         }
697         if (referId == null) {
698             throw new JspTagException JavaDoc("Cannot refer with id is null");
699         }
700         Object JavaDoc result = find(pageContext, from, referId);
701         // if it cannot be found, then 'null' will be put in the hashmap ('not present')
702

703         register(newId, result, check);
704         if (log.isDebugEnabled()) {
705             log.debug("found " + newId + " (" + result + ")");
706         }
707         return result;
708     }
709
710     public Object JavaDoc findAndRegister(PageContext JavaDoc pageContext, String JavaDoc externid, String JavaDoc newId) throws JspTagException JavaDoc {
711         return findAndRegister(pageContext, externid, newId, true);
712     }
713     public Object JavaDoc findAndRegister(PageContext JavaDoc pageContext, String JavaDoc externid, String JavaDoc newId, boolean check) throws JspTagException JavaDoc {
714         if (log.isDebugEnabled()) {
715             log.debug("searching to register object " + externid + " in context " + getId() + " check: " + check);
716         }
717         if (check && isRegistered(newId)) {
718             throw new JspTagException JavaDoc("Object with id " + newId + " was already registered in " + toString());
719         }
720         // if (findAndRegister(LOCATION_PAGE, referId, id)) return true;
721
Object JavaDoc result = find(pageContext, externid);
722         register(newId, result, check);
723         return result;
724     }
725
726     public Object JavaDoc findAndRegister(PageContext JavaDoc pageContext, String JavaDoc id) throws JspTagException JavaDoc {
727         return findAndRegister(pageContext, id, id);
728     }
729     public String JavaDoc findAndRegisterString(PageContext JavaDoc pageContext, String JavaDoc id) throws JspTagException JavaDoc {
730         return findAndRegisterString(pageContext, id, true);
731     }
732
733     public String JavaDoc findAndRegisterString(PageContext JavaDoc pageContext, String JavaDoc id, boolean check) throws JspTagException JavaDoc {
734         return (String JavaDoc) findAndRegister(pageContext, id, id, check);
735     }
736
737     public boolean isPresent(String JavaDoc key) throws JspTagException JavaDoc {
738         //if (! isRegisteredSomewhere(key)) {
739
// log.warn("Checking presence of unregistered context variable " + key + " in context " + getId());
740
// return false;
741
//}
742
Object JavaDoc value = get(key);
743
744         if (value == null) {
745             return false;
746         } else {
747             if (value instanceof List) {
748                 if (((List) value).size() == 0) return false;
749             }
750             return true;
751         }
752     }
753
754     /**
755      * @since MMBase-1.8
756      */

757     public void setJspVar(PageContext JavaDoc pageContext, String JavaDoc jspvar, int type, Object JavaDoc value) {
758         getBacking().setJspVar(pageContext, jspvar, type, value);
759     }
760
761     static String JavaDoc getDefaultCharacterEncoding(PageContext JavaDoc pageContext) {
762        String JavaDoc charEnc = pageContext.getResponse().getCharacterEncoding();
763        if(charEnc != null) {
764            return charEnc;
765        }
766        log.error("page encoding not specified, using iso-8859-1");
767        return "iso-8859-1";
768     }
769
770
771     public String JavaDoc toString() {
772         if (id == null) {
773             return "the context without id " + getBacking().toString();
774         } else {
775             return "context '" + id + "'" + getBacking().toString();
776         }
777     }
778 }
779
780 /**
781  * Container class, to store results of 'getPair' function, which is a 'parent' container plus
782  * a key.
783  *
784  * Since MMBase-1.8 three different implementations are available, to work like EL as much as possible.
785  */

786
787 abstract class Pair {
788     public final String JavaDoc restKey;
789     public final boolean wentDown;
790     Pair(String JavaDoc k, boolean w) {
791         restKey = k; wentDown = w;
792     }
793     abstract boolean containsKey(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc;
794     abstract Object JavaDoc get(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc;
795     abstract void register(String JavaDoc newId, Object JavaDoc n, boolean check, boolean checkParent) throws JspTagException JavaDoc;
796     abstract void unRegister(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc;
797 }
798
799 /**
800  * @since MMBase-1.8
801  */

802 class ContextContainerPair extends Pair {
803     private ContextContainer context;
804     ContextContainerPair(ContextContainer c, String JavaDoc k, boolean w) {
805         super(k, w);
806         context = c;
807     }
808     final boolean containsKey(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
809         return context.containsKey(key, checkParent);
810     }
811     final Object JavaDoc get(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
812         return context.get(key, checkParent);
813     }
814     final void register(String JavaDoc newId, Object JavaDoc n, boolean check, boolean checkParent) throws JspTagException JavaDoc {
815         context.register(newId, n, check, checkParent);
816     }
817     final void unRegister(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
818         context.unRegister(key, checkParent);
819     }
820
821
822 }
823 /**
824  * @since MMBase-1.8
825  */

826 class MapPair extends Pair {
827     private Map map;
828     MapPair(Map c, String JavaDoc k, boolean w) {
829         super(k, w);
830         map = c;
831     }
832     final boolean containsKey(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
833         if (checkParent) throw new JspTagException JavaDoc("Cannot check parent of Map");
834         return map.containsKey(key);
835     }
836     final Object JavaDoc get(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
837         if (checkParent) throw new JspTagException JavaDoc("Cannot check parent of Map");
838         return map.get(key);
839     }
840     final void register(String JavaDoc newId, Object JavaDoc n, boolean check, boolean checkParent) throws JspTagException JavaDoc {
841         if (checkParent) throw new JspTagException JavaDoc("Cannot check parent of Map");
842         map.put(newId, n);
843     }
844     final void unRegister(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
845         if (checkParent) throw new JspTagException JavaDoc("Cannot check parent of Map");
846         map.remove(key);
847     }
848 }
849
850 /**
851  * @since MMBase-1.8
852  */

853 class BeanPair extends Pair {
854     private Object JavaDoc bean;
855     private Class JavaDoc clazz;
856     BeanPair(Object JavaDoc c, String JavaDoc k, boolean w) {
857         super(k, w);
858         bean = c;
859         clazz = bean.getClass();
860     }
861
862     private Method getMethod(String JavaDoc key) {
863         String JavaDoc methodKey = Character.toUpperCase(key.charAt(0)) + key.substring(1);
864         Method method;
865         try {
866             method = clazz.getMethod("get" + methodKey, null);
867         } catch (Exception JavaDoc e) {
868             try {
869                 method = clazz.getMethod("is" + methodKey, null);
870             } catch (Exception JavaDoc f) {
871                 return null;
872             }
873         }
874         return method;
875     }
876     final boolean containsKey(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
877         if (checkParent) throw new JspTagException JavaDoc("Cannot check parent of Bean");
878         return getMethod(key) != null;
879     }
880     final Object JavaDoc get(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
881         if (checkParent) throw new JspTagException JavaDoc("Cannot check parent of Bean");
882         try {
883             Method method = getMethod(key);
884             if (method == null) return null;
885             return method.invoke(bean, null);
886         } catch (Exception JavaDoc iae) {
887             throw new TaglibException(iae.getMessage(), iae);
888         }
889     }
890     final void register(String JavaDoc newId, Object JavaDoc n, boolean check, boolean checkParent) throws JspTagException JavaDoc {
891         throw new UnsupportedOperationException JavaDoc("Cannot register in a bean");
892     }
893     final void unRegister(String JavaDoc key, boolean checkParent) throws JspTagException JavaDoc {
894         throw new UnsupportedOperationException JavaDoc("Cannot unregister in a bean");
895     }
896
897 }
898
Popular Tags