1 7 package com.inversoft.verge.mvc.controller; 8 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.NoSuchElementException ; 14 import java.util.StringTokenizer ; 15 16 import org.apache.log4j.Logger; 17 18 import com.inversoft.verge.mvc.MVCException; 19 import com.inversoft.verge.mvc.MVCRegistry; 20 21 22 47 public class ControllerMVCInfo { 48 49 52 private static final Logger logger = Logger.getLogger(ControllerMVCInfo.class); 53 54 57 public static final String INPUT_DELIMITER = "/"; 58 59 60 private String controllerHandler; 61 private ControllerHandler handler; 62 private List urlValues = new ArrayList (5); 63 64 65 69 protected ControllerMVCInfo() { 70 } 71 72 78 public ControllerMVCInfo(String encodedValue) throws MVCException { 79 decode(encodedValue); 80 } 81 82 86 public ControllerMVCInfo(String controllerHandler, List urlValues) { 87 assert (controllerHandler != null) : "controllerHandler == null"; 88 89 this.controllerHandler = controllerHandler; 90 this.urlValues = urlValues; 91 } 92 93 94 99 public String getControllerHandler() { 100 return controllerHandler; 101 } 102 103 109 public ControllerHandler getHandler() throws MVCException { 110 if (handler == null) { 111 handler = MVCRegistry.lookupControllerHandler(controllerHandler); 112 if (handler == null) { 113 throw new MVCException("ControllerHandler named: " + 114 controllerHandler + " does not exist"); 115 } 116 117 } 118 return handler; 119 } 120 121 127 public List getURLValues() { 128 return urlValues; 129 } 130 131 136 public String encode() { 137 StringBuffer buf = new StringBuffer (128); 138 buf.append(ControllerMVCInfo.INPUT_DELIMITER); 139 buf.append(controllerHandler); 140 141 Iterator iter = urlValues.iterator(); 142 while (iter.hasNext()) { 143 buf.append(ControllerMVCInfo.INPUT_DELIMITER).append(iter.next()); 144 } 145 146 return buf.toString(); 147 } 148 149 155 public void decode(String encodedValue) throws MVCException { 156 if (encodedValue == null) { 157 logger.error("Verge framework encountered an invalid URL [" + 158 encodedValue + "]"); 159 throw new MVCException("Verge framework encountered an invalid URL [" + 160 encodedValue + "]"); 161 } 162 163 StringTokenizer st = new StringTokenizer (encodedValue, 164 ControllerMVCInfo.INPUT_DELIMITER); 165 try { 166 controllerHandler = st.nextToken(); 167 } catch (NoSuchElementException nsee) { 168 logger.error("Verge framework encountered an invalid URL [" + 169 encodedValue + "]"); 170 throw new MVCException("Verge framework encountered an invalid URL [" + 171 encodedValue + "]"); 172 } 173 174 while (st.hasMoreTokens()) { 175 urlValues.add(st.nextToken()); 176 } 177 } 178 } | Popular Tags |