1 23 package org.infoglue.deliver.portal; 24 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.StringTokenizer ; 32 import java.util.Vector ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.pluto.portalImpl.core.PortalControlParameter; 37 38 41 public class PathParser 42 { 43 private static final Log log = LogFactory.getLog(PathParser.class); 44 45 public static final String MULTI_VALUE = "__"; 46 47 private String path; 48 private String action; 49 private Collection parsedPath; 50 private Collection globalNavigation; 51 private Map stateFullParameterMap; 52 private Map stateLessParameterMap; 53 private String pid = null; 54 private String actionControl = null; 55 56 public PathParser(String pPath) 57 { 58 this.path = pPath.substring(0, pPath.lastIndexOf('/')); 59 String tmp = pPath.substring(pPath.lastIndexOf('/') + 1, pPath.length()); 60 StringTokenizer actionTok = new StringTokenizer (tmp, ";"); 61 while (actionTok.hasMoreTokens()) 62 { 63 String token = actionTok.nextToken(); 64 if (token.endsWith(".action")) 65 { 66 this.action = token; 67 } 68 else 69 { 70 } 72 } 73 74 this.parsedPath = new LinkedList (); 75 76 for (StringTokenizer tok = new StringTokenizer (this.path, "/."); tok.hasMoreTokens();) 77 { 78 this.parsedPath.add(tok.nextElement()); 79 } 80 81 this.stateFullParameterMap = new HashMap (); 82 this.stateLessParameterMap = new HashMap (); 83 this.globalNavigation = new LinkedList (); 84 for (Iterator iter = parsedPath.iterator(); iter.hasNext();) 85 { 86 String element = (String ) iter.next(); 87 88 if (PortalControlParameter.isControlParameter(element)) 89 { 90 if (PortalControlParameter.isStateFullParameter(element)) 91 { 92 if (iter.hasNext()) 93 { 94 stateFullParameterMap.put(element, iter.next()); 95 } 96 } 97 else 98 { 99 if (iter.hasNext()) 100 { 101 stateLessParameterMap.put(element, iter.next()); 102 } 103 } 104 105 } 106 else 107 { 108 globalNavigation.add(element); 109 } 110 } 111 112 this.pid = (String ) stateLessParameterMap.get(PortalControlURL.PID); 114 this.actionControl = (String ) stateLessParameterMap.get(PortalControlURL.ACTION); 116 } 117 118 public Map getStateFullParameterMap() { 119 return this.stateFullParameterMap; 120 } 121 122 public Map getStateLessParameterMap() { 123 return this.stateLessParameterMap; 124 } 125 126 public Collection getParsedPath() { 127 return parsedPath; 128 } 129 130 public Collection getGlobalNavigation() { 131 return globalNavigation; 132 } 133 134 public String getPath() { 135 return this.path; 136 } 137 138 public String getAction() { 139 return this.action; 140 } 141 142 148 public static Map copyParameters(Map parameters) 149 { 150 HashMap map = new HashMap (parameters.size()); 151 for (Iterator iter = parameters.keySet().iterator(); iter.hasNext();) 152 { 153 String key = (String ) iter.next(); 154 Object value = parameters.get(key); 155 map.put(key, value); 156 } 157 158 return map; 159 } 160 161 167 public static String getRealPath(String path) { 168 StringBuffer buf = new StringBuffer (); 169 StringTokenizer st = new StringTokenizer (path, "/"); 170 while (st.hasMoreTokens()) { 171 String token = st.nextToken(); 172 if (token.startsWith("_")) { 173 break; 174 } else { 175 buf.append("/"); 176 buf.append(token); 177 } 178 } 179 return buf.toString(); 180 } 181 182 190 public static Map parsePathParameters( 191 String prefix, 192 String encodedParameters, 193 boolean preserveNamespace) { 194 log.debug("Parsing '" + encodedParameters + "' for " + prefix); 195 HashMap map = new HashMap (10); 196 StringTokenizer tok = new StringTokenizer (encodedParameters, "/"); 197 String name = (tok.hasMoreTokens() ? tok.nextToken() : null); 198 while (name != null) { 199 if (name.startsWith(prefix)) { 200 Vector buffer = new Vector (); 201 202 String nextName = decodeValues(tok, buffer); 203 if (buffer.size() > 0) { 204 if (!preserveNamespace) { 205 name = name.substring(prefix.length()); 206 } 207 log.debug("Adding " + name + "=" + buffer); 208 map.put(name, (String []) buffer.toArray(new String [buffer.size()])); 209 } 210 name = nextName; 211 } else { 212 name = (tok.hasMoreTokens() ? tok.nextToken() : null); 214 } 215 } 216 return map; 217 } 218 219 private static String decodeValues(StringTokenizer tok, List buffer) { 220 if (tok.hasMoreTokens()) { 221 String entry = tok.nextToken(); 222 if (entry.startsWith(MULTI_VALUE)) { 223 while (tok.hasMoreTokens() && entry.startsWith(MULTI_VALUE)) { 225 buffer.add(entry.substring(MULTI_VALUE.length())); 226 entry = tok.nextToken().replaceAll("%2F", "/"); 227 } 228 return entry; 229 } else { 230 buffer.add(entry.replaceAll("%2F", "/")); 232 return (tok.hasMoreElements() ? tok.nextToken() : null); 233 } 234 } 235 return null; 236 } 237 238 244 public static String encodeValues(String [] values) { 245 StringBuffer result = new StringBuffer (); 246 if (values.length == 1) { 247 result.append(values[0].replaceAll("/", "%2F")); 248 } else { 249 for (int i = 0; i < values.length; i++) { 250 result.append(MULTI_VALUE); 251 result.append(values[i].replaceAll("/", "%2F")); 252 if (i < values.length - 1) 253 result.append("/"); 254 } 255 } 256 return result.toString(); 257 } 258 259 263 public String getPid() { 264 return pid; 265 } 266 267 271 public String getActionControl() { 272 return actionControl; 273 } 274 275 public boolean isTargetedRequest() { 276 return (this.pid != null || actionControl != null); 277 } 278 } 279 | Popular Tags |