1 10 package org.mmbase.bridge.jsp.taglib.functions; 11 12 13 import org.mmbase.bridge.jsp.taglib.ContentTag; 14 import java.util.Collection ; 15 import java.util.Iterator ; 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 45 public class Functions { 46 private static final Logger log = Logging.getLoggerInstance(Functions.class); 47 48 51 public static boolean contains(Collection col, Object 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 (((Node) obj).getNumber()); 58 } 59 } 60 if (col.contains(obj)) return true; 61 return col.contains(Casting.toString(obj)); 62 } 63 64 67 public static void remove(Collection col, Object obj) { 68 if (col == null) return; 69 if (obj instanceof Collection ) { Iterator i = ((Collection ) obj).iterator(); 71 while (i.hasNext()) { 72 remove(col, i.next()); 73 } 74 } else { 75 if (obj instanceof Node) { 76 col.remove(new Integer (((Node) obj).getNumber())); 77 } 78 col.remove(Casting.toString(obj)); 79 col.remove(obj); 80 } 81 } 82 83 84 88 public static String escape(String escaper, String 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 e) { 93 String 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 directory(String file) { 100 if (file.endsWith("/")) return file; 101 return org.mmbase.util.ResourceLoader.getDirectory(file); 102 } 103 104 107 public static String url(String page, javax.servlet.jsp.PageContext pageContext) { 108 javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest ) pageContext.getRequest(); 109 StringBuffer show = new StringBuffer (); 110 if (page.equals("")) { String requestURI = req.getRequestURI(); 112 if (requestURI.endsWith("/")) { 113 page = "."; 114 } else { 115 page = new java.io.File (requestURI).getName(); 116 } 117 } 118 if (page.charAt(0) == '/') { show.append(req.getContextPath()); 120 } 121 show.append(page); 122 return show.toString(); 123 124 } 125 126 127 128 } 129 | Popular Tags |