KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > functions > Functions


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.functions;
11
12
13 import org.mmbase.bridge.jsp.taglib.ContentTag;
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.mmbase.bridge.Node;
18 import org.mmbase.bridge.NodeList;
19 import org.mmbase.util.Casting;
20 import org.mmbase.util.transformers.CharTransformer;
21 import org.mmbase.util.logging.Logger;
22 import org.mmbase.util.logging.Logging;
23
24 /**
25  * Functions for EL variables, and XSL.
26  * Like this:
27
28 <mm:import id="nodelist" vartype="list">1,2,123</mm:import>
29 <mm:cloud>
30   <mm:node number="124" id="node" />
31   <c:choose>
32     <c:when test="${mm:contains(nodelist, node)}">
33       YES!
34     </c:when>
35     <c:otherwise>
36       NO!
37     </c:otherwise>
38   </c:choose>
39 </mm:cloud>
40  * @author Michiel Meeuwissen
41  * @since MMBase-1.8
42  * @version $Id: Functions.java,v 1.15 2006/08/30 18:02:35 michiel Exp $
43  * @todo EXPERIMENTAL
44  */

45 public class Functions {
46     private static final Logger log = Logging.getLoggerInstance(Functions.class);
47
48     /**
49      * MMBase specific 'contains' (for Collections). For strings use fn:contains.
50      */

51     public static boolean contains(Collection JavaDoc col, Object JavaDoc obj) {
52         if (col == null) return false;
53         if (obj instanceof Node) {
54             if (col instanceof NodeList) {
55                 if (col.contains(obj)) return true;
56             } else {
57                 obj = new Integer JavaDoc(((Node) obj).getNumber());
58             }
59         }
60         if (col.contains(obj)) return true;
61         return col.contains(Casting.toString(obj));
62     }
63
64     /**
65      * MMBase specific 'remove' (for Collections).
66      */

67     public static void remove(Collection JavaDoc col, Object JavaDoc obj) {
68         if (col == null) return;
69         if (obj instanceof Collection JavaDoc) { // like removeAll
70
Iterator JavaDoc i = ((Collection JavaDoc) obj).iterator();
71             while (i.hasNext()) {
72                 remove(col, i.next());
73             }
74         } else {
75             if (obj instanceof Node) {
76                 col.remove(new Integer JavaDoc(((Node) obj).getNumber()));
77             }
78             col.remove(Casting.toString(obj));
79             col.remove(obj);
80         }
81     }
82
83
84     /**
85      * Provides the 'escape' functionality to the XSLT itself. (using taglib:escape('p', mytag))
86      *
87      */

88     public static String JavaDoc escape(String JavaDoc escaper, String JavaDoc string) {
89         try {
90             CharTransformer ct = ContentTag.getCharTransformer(escaper, null);
91             return ct == null ? "" + Casting.unWrap(string) : ct.transform("" + Casting.unWrap(string));
92         } catch (Exception JavaDoc e) {
93             String JavaDoc mes = "Could not escape " + string + " with escape " + escaper + " : " + e.getMessage();
94             log.debug(mes, e);
95             return mes;
96         }
97     }
98
99     public static String JavaDoc directory(String JavaDoc file) {
100         if (file.endsWith("/")) return file;
101         return org.mmbase.util.ResourceLoader.getDirectory(file);
102     }
103
104     /**
105      * @since MMBase-1.8.2
106      */

107     public static String JavaDoc url(String JavaDoc page, javax.servlet.jsp.PageContext JavaDoc pageContext) {
108         javax.servlet.http.HttpServletRequest JavaDoc req = (javax.servlet.http.HttpServletRequest JavaDoc) pageContext.getRequest();
109         StringBuffer JavaDoc show = new StringBuffer JavaDoc();
110         if (page.equals("")) { // means _this_ page
111
String JavaDoc requestURI = req.getRequestURI();
112             if (requestURI.endsWith("/")) {
113                 page = ".";
114             } else {
115                 page = new java.io.File JavaDoc(requestURI).getName();
116             }
117         }
118         if (page.charAt(0) == '/') { // absolute on servletcontex
119
show.append(req.getContextPath());
120         }
121         show.append(page);
122         return show.toString();
123
124     }
125
126
127
128 }
129
Popular Tags