1 7 package com.inversoft.verge.mvc.model; 8 9 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 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 20 21 65 public class ModelMVCInfo { 66 67 70 private static final Logger logger = Logger.getLogger(ModelMVCInfo.class); 71 72 75 public static final String INPUT_DELIMITER = "|"; 76 77 80 public static final String EXTRA_PARAM_DELIMITER = "="; 81 82 83 private String inputName; 84 private String modelSystem; 85 private String modelDefinition; 86 private Map extraParams = new HashMap (); 87 88 89 90 95 public ModelMVCInfo() { 96 } 97 98 104 public ModelMVCInfo(String encodedValue) throws MVCException { 105 decode(encodedValue); 106 } 107 108 112 public ModelMVCInfo(String inputName, String modelSystem, String modelDefinition, 113 Map extraParams) { 114 assert (inputName != null) : "inputName == null"; 115 assert (modelSystem != null) : "modelSystem == null"; 116 assert (modelDefinition != null) : "modelDefinition == null"; 117 118 this.inputName = inputName; 119 this.modelSystem = modelSystem; 120 this.modelDefinition = modelDefinition; 121 this.extraParams = extraParams; 122 } 123 124 125 130 public String getInputName() { 131 return inputName; 132 } 133 134 139 public String getModelSystem() { 140 return modelSystem; 141 } 142 143 148 public String getModelDefinition() { 149 return modelDefinition; 150 } 151 152 157 public Map getExtraParams() { 158 return extraParams; 159 } 160 161 166 public void decode(String encodedValue) throws MVCException { 167 StringTokenizer st = new StringTokenizer (encodedValue, INPUT_DELIMITER); 168 try { 169 inputName = st.nextToken(); 170 modelSystem = st.nextToken(); 171 modelDefinition = st.nextToken(); 172 } catch (NoSuchElementException nsee) { 173 logger.error("Invalid input parameter format: " + encodedValue); 174 throw new MVCException("Invalid input parameter format: " + 175 encodedValue); 176 } 177 178 extraParams.clear(); 179 decodeExtraParams(st); 180 } 181 182 187 public String encode() { 188 StringBuffer buf = new StringBuffer (128); 189 buf.append(inputName).append(INPUT_DELIMITER); 190 buf.append(modelSystem).append(INPUT_DELIMITER); 191 buf.append(modelDefinition); 192 193 encodeExtraParams(buf); 194 195 return buf.toString(); 196 } 197 198 207 protected void decodeExtraParams(StringTokenizer st) throws MVCException { 208 209 if (st.hasMoreTokens()) { 211 212 String extraParam; 213 int index; 214 215 do { 216 extraParam = st.nextToken(); 217 index = extraParam.indexOf(EXTRA_PARAM_DELIMITER); 218 if (index == -1 || index == (extraParam.length() - 1)) { 219 logger.error("Invalid extra parameter string: " + extraParam); 220 throw new MVCException("Invalid extra parameter string: " + 221 extraParam); 222 } 223 224 extraParams.put(extraParam.substring(0, index), 225 extraParam.substring(index + 1)); 226 } while (st.hasMoreTokens()); 227 } 228 } 229 230 235 protected void encodeExtraParams(StringBuffer buf) { 236 if (extraParams != null && extraParams.size() > 0) { 237 Iterator iter = extraParams.entrySet().iterator(); 238 Map.Entry entry; 239 while (iter.hasNext()) { 240 entry = (Map.Entry ) iter.next(); 241 buf.append(INPUT_DELIMITER).append(entry.getKey()); 242 buf.append(EXTRA_PARAM_DELIMITER).append(entry.getValue()); 243 } 244 } 245 } 246 } | Popular Tags |