1 5 package xdoclet.tagshandler; 6 7 import java.beans.Introspector ; 8 import java.lang.reflect.Method ; 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Properties ; 12 import java.util.StringTokenizer ; 13 14 import org.apache.commons.logging.Log; 15 16 import xdoclet.ConfigParamIntrospector; 17 import xdoclet.SubTask; 18 import xdoclet.XDocletException; 19 import xdoclet.XDocletTagSupport; 20 import xdoclet.util.LogUtil; 21 22 28 public class ConfigTagsHandler extends XDocletTagSupport 29 { 30 41 private static String currentConfigParam = null; 42 43 51 private static int currentConfigParamIndex = -1; 52 53 58 public static int getCurrentConfigParamIndex() 59 { 60 return currentConfigParamIndex; 61 } 62 63 73 public Object getConfigParameter(String paramName) throws XDocletException 74 { 75 Log log = LogUtil.getLog(ConfigTagsHandler.class, "getConfigParameter"); 76 77 paramName = Introspector.decapitalize(paramName); 78 79 SubTask subtask = getDocletContext().getActiveSubTask(); 80 81 if (log.isDebugEnabled()) { 82 log.debug("subtask=" + subtask.getClass()); 83 log.debug("currentConfigParamIndex=" + currentConfigParamIndex); 84 log.debug("currentConfigParam=" + currentConfigParam); 85 log.debug("paramName=" + paramName); 86 } 87 88 String configName = paramName; 89 Object configValue = null; 90 int index = 0; 91 92 index = configName.indexOf('.'); 93 94 if (index != -1) { 95 97 int index2 = paramName.indexOf('.', index + 1); 98 99 if (configValue == null && index2 == -1) { 101 configName = paramName; 103 configValue = getDocletContext().getConfigParam(configName); 104 105 if (configValue == null) { 107 String elemname = paramName.substring(0, index); 108 String paramname = paramName.substring(index + 1); 109 110 configName = subtask.getSubTaskName() + '.' + elemname; 111 configValue = getDocletContext().getConfigParam(configName); 112 113 if (configValue != null) { 116 if (currentConfigParamIndex != -1) { 118 log.debug("In a forAllConfigParams loop for an ArrayList-based config parameter."); 119 configValue = ((java.util.ArrayList ) configValue).get(currentConfigParamIndex); 121 } 122 123 Method getterMethod = ConfigParamIntrospector.findGetterMethod(configValue, paramname); 124 125 if (getterMethod == null) { 126 configValue = null; 127 } 128 129 try { 130 configValue = getterMethod.invoke(configValue, null); 131 } 132 catch (Exception e) { 133 log.debug("not found", e); 134 } 135 } 136 } 137 } 138 else { 139 141 int lastDotIndex = paramName.lastIndexOf('.'); 143 String paramname = paramName.substring(lastDotIndex + 1); 144 145 configName = paramName.substring(0, lastDotIndex); 146 configValue = getDocletContext().getConfigParam(configName); 147 148 if (configValue != null) { 149 if (currentConfigParamIndex != -1) { 151 log.debug("In a forAllConfigParams loop for an ArrayList-based config parameter."); 152 153 configValue = ((java.util.ArrayList ) configValue).get(currentConfigParamIndex); 155 } 156 157 Method getterMethod = ConfigParamIntrospector.findGetterMethod(configValue, paramname); 158 159 if (getterMethod == null) { 160 configValue = null; 161 } 162 163 try { 164 configValue = getterMethod.invoke(configValue, null); 165 } 166 catch (Exception e) { 167 log.debug("not found", e); 168 } 169 } 170 } 171 } 172 else { 173 175 configName = subtask.getSubTaskName() + '.' + paramName; 177 configValue = getDocletContext().getConfigParam(configName); 178 179 if (configValue == null) { 181 configName = paramName; 182 configValue = getDocletContext().getConfigParam(paramName); 183 } 184 } 185 186 if (configValue == null) { 188 return null; 189 } 190 else { 191 if (log.isDebugEnabled()) { 192 log.debug("Config param found:" + paramName); 193 } 194 } 195 196 if (configValue.equals(ConfigParamIntrospector.NULL)) { 198 configValue = null; 199 } 200 201 log.debug("configValue=" + configValue); 202 203 return configValue; 204 } 205 206 216 public void ifHasConfigParam(String template, Properties attributes) throws XDocletException 217 { 218 if (!configParameterValue(attributes).equals("")) { 219 generate(template); 220 } 221 } 222 223 233 public String configParameterValue(Properties attributes) throws XDocletException 234 { 235 String paramName = attributes.getProperty("paramName"); 236 Object configParam = getConfigParameter(paramName); 237 238 if (configParam instanceof Collection && ((Collection ) configParam).isEmpty()) { 240 return ""; 241 } 242 else if (configParam instanceof Object [] && ((Object []) configParam).length == 0) { 244 return ""; 245 } 246 else if (configParam != null) { 248 return configParam.toString(); 249 } 250 else { 252 return ""; 253 } 254 } 255 256 267 public void forAllConfigParameters(String template, Properties attributes) throws XDocletException 268 { 269 String paramName = attributes.getProperty("paramName"); 270 ArrayList configParams = (java.util.ArrayList ) getConfigParameter(paramName); 271 272 for (int i = 0; i < configParams.size(); i++) { 273 currentConfigParam = paramName; 274 currentConfigParamIndex = i; 275 276 generate(template); 277 } 278 279 currentConfigParam = null; 280 currentConfigParamIndex = -1; 281 } 282 283 294 public void ifConfigParamGreaterOrEquals(String template, Properties attributes) throws XDocletException 295 { 296 if (ifConfigParamGreaterOrEquals_Impl(attributes)) { 297 generate(template); 298 } 299 } 300 301 312 public void ifConfigParamNotGreaterOrEquals(String template, Properties attributes) throws XDocletException 313 { 314 if (!ifConfigParamGreaterOrEquals_Impl(attributes)) { 315 generate(template); 316 } 317 } 318 319 330 public void ifConfigParamEquals(String template, Properties attributes) throws XDocletException 331 { 332 if (ifConfigParamEquals_Impl(attributes)) { 333 generate(template); 334 } 335 } 336 337 348 public void ifConfigParamNotEquals(String template, Properties attributes) throws XDocletException 349 { 350 if (!ifConfigParamEquals_Impl(attributes)) { 351 generate(template); 352 } 353 } 354 355 365 protected boolean ifConfigParamGreaterOrEquals_Impl(Properties attributes) throws XDocletException 366 { 367 Log log = LogUtil.getLog(ConfigTagsHandler.class, "ifConfigParamGreaterOrEquals_Impl"); 368 369 String paramName = attributes.getProperty("paramName"); 370 String paramValue = attributes.getProperty("value"); 371 372 Object configParamValue = getConfigParameter(paramName); 373 374 boolean greaterOrEquals = false; 375 376 if (configParamValue != null) { 377 greaterOrEquals = configParamValue.equals(paramValue); 378 if (!greaterOrEquals) { 379 StringTokenizer configParamTokenizer = new StringTokenizer (configParamValue.toString(), "."); 380 StringTokenizer paramTokenizer = new StringTokenizer (paramValue, "."); 381 382 boolean greater = false; 383 boolean less = false; 384 385 while (!greater && !less && (configParamTokenizer.hasMoreTokens() || 386 paramTokenizer.hasMoreTokens())) { 387 int i = configParamTokenizer.hasMoreTokens() ? Integer.parseInt(configParamTokenizer.nextToken()) : 0; 388 int j = paramTokenizer.hasMoreTokens() ? Integer.parseInt(paramTokenizer.nextToken()) : 0; 389 390 greater = i > j; 391 less = i < j; 392 } 393 394 greaterOrEquals = greater || !less; 395 } 396 } 397 398 return greaterOrEquals; 399 } 400 401 411 protected boolean ifConfigParamEquals_Impl(Properties attributes) throws XDocletException 412 { 413 Log log = LogUtil.getLog(ConfigTagsHandler.class, "ifConfigParamEquals_Impl"); 414 415 String paramName = attributes.getProperty("paramName"); 416 String paramValue = attributes.getProperty("value"); 417 418 Object configParamValue = getConfigParameter(paramName); 419 420 if (log.isDebugEnabled()) 421 log.debug("paramName='" + paramName + 422 "',paramValue='" + paramValue + 423 "',configParamValue='" + configParamValue + '\''); 424 425 if (configParamValue == null) 426 return false; 427 428 if (configParamValue instanceof Boolean ) 429 return configParamValue.equals(Boolean.valueOf(paramValue)); 430 if (configParamValue instanceof Integer ) 431 return configParamValue.equals(Integer.valueOf(paramValue)); 432 433 if (!(configParamValue instanceof String )) 437 log.warn(configParamValue.getClass() + " needs adding to configParamValue types"); 438 return configParamValue.toString().equals(paramValue); 439 } 440 441 448 private boolean isSubConfigParamInSameConfigParam(String paramName) 449 { 450 return paramName.equals(currentConfigParam); 451 } 452 } 453 | Popular Tags |