1 10 11 package org.mmbase.bridge.jsp.taglib.pageflow; 12 13 import java.io.File ; 14 import java.util.StringTokenizer ; 15 import java.util.Stack ; 16 import javax.servlet.jsp.JspTagException ; 17 import javax.servlet.http.HttpSession ; 18 import org.mmbase.bridge.*; 19 import org.mmbase.util.logging.Logger; 20 import org.mmbase.util.logging.Logging; 21 import org.mmbase.module.core.MMBaseContext; 22 23 34 35 public class TreeHelper { 36 51 52 private Cloud cloud; 53 private static final Logger log = Logging.getLoggerInstance(TreeHelper.class); 54 private static String htmlroot = MMBaseContext.getHtmlRoot(); 55 56 public void setCloud(Cloud cl) { 57 cloud = cl; 58 } 59 60 66 protected String findLeafFile(String includePage, String objectlist, HttpSession session) throws JspTagException { 67 if ("".equals(objectlist)) { 68 return encodedPath(includePage); 69 } 70 String lf = getLeafFile("/", objectlist, includePage, true, session); 71 log.debug("findLeafFile = [" + lf + "]"); 72 return encodedPath(lf); 73 } 74 75 85 protected String getLeafFile(String prefix, String objectlist, String includePage, boolean maySmartpath, HttpSession session) throws JspTagException { 86 if (objectlist.equals("")) { 87 String nudePage = includePage; 88 if (nudePage.indexOf('?') != -1) { 89 nudePage = nudePage.substring(0, nudePage.indexOf('?')); 90 } 91 92 String filename = concatpath(prefix, nudePage); 93 log.debug("Check file: " + filename + " in root " + htmlroot); 94 95 if ((new File (concatpath(htmlroot, filename))).exists()) { 96 return concatpath(prefix, includePage); 98 } else { 99 return ""; 100 } 101 } 102 103 int firstComma = objectlist.indexOf(','); 104 String firstObject = null; 105 String otherObjects = null; 106 107 if (firstComma > 0) { 108 firstObject = objectlist.substring(0, firstComma); 109 otherObjects = objectlist.substring(firstComma + 1, objectlist.length()); 110 log.debug("Splitting '" + objectlist + "' into '" + firstObject + "' and '" + otherObjects + "'"); 111 } else { 112 firstObject = objectlist; 113 otherObjects = ""; 114 log.debug("Only one object left: '" + firstObject + "'"); 115 } 116 117 String finalfile = null; 118 119 try { 124 cloud.getNode(firstObject); 125 } catch (org.mmbase.bridge.NotFoundException e) { 126 log.debug("'" + firstObject + "' is not an object; seeing it as a path)"); 127 return getLeafFile (concatpath(prefix, firstObject), otherObjects, includePage, maySmartpath, session); 128 } 129 130 134 if (maySmartpath) { 135 String newprefix = prefix; 136 String smartpath = getSmartPath(firstObject, newprefix, session); 137 log.debug("getSmartPath(" + firstObject + "," + newprefix + "," + session + ") = " + smartpath); 138 if (!(smartpath == null || smartpath.equals(""))) { 139 newprefix = smartpath; 140 finalfile = getLeafFile(newprefix, otherObjects, includePage, true, session); 141 } 142 } 143 144 150 if (finalfile == null || "".equals(finalfile)) { 151 NodeManager nm = cloud.getNode(firstObject).getNodeManager(); 152 while (nm != null) { 153 finalfile = getLeafFile(concatpath(prefix, nm.getName()) + File.separator, otherObjects, includePage, false, session); 154 if (!(finalfile == null || "".equals(finalfile))) 155 return finalfile; 156 try { 157 nm = nm.getParent(); 158 } catch (NotFoundException e) { 159 nm = null; 160 } 161 } 162 } else { 163 return finalfile; 164 } 165 return ""; 166 } 167 168 175 protected String findTreeFile(String includePage, String objectlist, HttpSession session) throws JspTagException { 176 if (cloud == null) { 177 throw new JspTagException ("Cloud was not defined"); 178 } 179 180 String nudePage; 182 if (includePage.indexOf('?') != -1) 183 nudePage = includePage.substring(0, includePage.indexOf('?')); 184 else 185 nudePage = includePage; 186 187 StringTokenizer st = new StringTokenizer (objectlist, ","); 189 int numberTokens = st.countTokens(); 190 Stack objectPaths = new Stack (); 191 192 int objectNumbers[] = new int[numberTokens]; 193 String pathNow = "/"; 194 195 for (int i = 0; i < numberTokens; i++) { 197 objectNumbers[i] = Integer.parseInt(st.nextToken()); 198 } 199 200 for (int i = 0; i < numberTokens; i++) { 202 int objectNo = objectNumbers[i]; 203 String field = getSmartPath("" + objectNo, pathNow, session); 204 205 if (field == null || field.equals("")) { 206 break; 207 } 208 209 pathNow = field; 210 objectPaths.push(pathNow); 211 } 212 213 217 pathNow = ""; 218 while (!objectPaths.empty()) { 219 String path = (String )objectPaths.pop(); 220 221 String pathTest = concatpath(concatpath(htmlroot, path), nudePage); 222 223 log.debug("Check file: " + pathTest); 224 if ((new File (pathTest)).isFile()) { 225 return encodedPath(concatpath(path, includePage)); 228 } 229 } 230 231 log.debug("Check file: " + concatpath(htmlroot, nudePage)); 233 if (new File (concatpath(htmlroot, nudePage)).isFile()) { 234 return includePage; 235 } else { 236 return ""; 237 } 238 } 239 240 248 private String getVersion(String objectnumber, HttpSession session) throws JspTagException { 249 if (session == null) { 250 return ""; 252 } 253 String versionnumber = (String )session.getAttribute(getBuilderName(objectnumber) + "version"); 254 if (versionnumber == null) { 255 return ""; 257 } 258 return versionnumber; 259 } 260 261 266 private String getBuilderName(String objectnumber) throws JspTagException { 267 return cloud.getNode(objectnumber).getNodeManager().getName(); 268 } 269 270 276 private String getSmartPath(String objectnumber, String middle, HttpSession session) throws JspTagException { 277 return (String )cloud.getNode(objectnumber).getValue("smartpath(" + htmlroot + "," + middle + "," + getVersion(objectnumber, session) + ")"); 278 } 279 280 289 private String encodedPath(String fileSystemPath) { 290 String fp = fileSystemPath; 291 if (fp == null) { 292 fp = ""; 293 } 294 File f = new File (fp); 295 String result = f.getName(); 296 f = f.getParentFile(); 297 while (f != null) { 298 String thisPart = f.getName(); 299 result = org.mmbase.util.Encode.encode("ESCAPE_URL", thisPart) + "/" + result; 300 f = f.getParentFile(); 301 } 302 return result; 303 } 304 305 309 private String concatpath(String path1, String path2) { 310 if (path1 == null && path2 == null) { 311 return ""; 312 } else if (path1 == null) { 313 return path2; 314 } else if (path2 == null) { 315 return path1; 316 } 317 if (path1.endsWith(File.separator) && path2.startsWith(File.separator)) { 318 return path1 + path2.substring(File.separator.length()); 320 } else if (!path1.endsWith(File.separator) && !path2.startsWith(File.separator)) { 321 return path1 + File.separator + path2; 322 } else { 323 return path1 + path2; 324 } 325 } 326 327 public void doFinally() { 328 cloud = null; 329 } 330 } 331 | Popular Tags |