1 49 50 package org.opencms.configuration; 51 52 import org.opencms.file.CmsObject; 53 import org.opencms.main.CmsLog; 54 55 import org.apache.commons.beanutils.ConvertUtils; 56 import org.apache.commons.beanutils.MethodUtils; 57 import org.apache.commons.digester.Digester; 58 import org.apache.commons.digester.Rule; 59 import org.apache.commons.logging.Log; 60 61 import org.xml.sax.Attributes ; 62 63 91 92 public class CmsSetNextRule extends Rule { 93 94 95 private static final Log LOG = CmsLog.getLog(CmsSetNextRule.class); 96 99 protected String m_bodyText = null; 100 101 104 protected String m_methodName = null; 105 106 111 protected int m_paramCount = 0; 112 113 116 protected Class [] m_paramTypes = null; 117 118 121 protected boolean m_useExactMatch = false; 122 123 127 private String [] m_paramClassNames = null; 128 129 134 private int m_targetOffset = 0; 135 136 152 public CmsSetNextRule(String methodName, Class clazz) { 153 154 this(methodName, new Class [] {clazz}); 155 } 156 157 176 public CmsSetNextRule(String methodName, Class [] clazzes) { 177 178 m_targetOffset = 0; 179 m_methodName = methodName; 180 m_paramCount = clazzes.length + 1; 181 m_paramTypes = new Class [m_paramCount]; 182 m_paramTypes[0] = CmsObject.class; 183 System.arraycopy(clazzes, 0, m_paramTypes, 1, clazzes.length); 184 } 185 186 195 public void begin(java.lang.String namespace, java.lang.String name, Attributes attributes) throws Exception { 196 197 200 if (m_paramCount > 0) { 202 Object [] parameters = new Object [m_paramCount]; 203 for (int i = 0; i < parameters.length; i++) { 204 parameters[i] = null; 205 } 206 digester.pushParams(parameters); 207 } 208 } 209 210 219 public void body(java.lang.String namespace, java.lang.String name, String bodyText) throws Exception { 220 221 if (m_paramCount == 0) { 222 m_bodyText = bodyText.trim(); 223 } 224 } 225 226 234 public void end(java.lang.String namespace, java.lang.String name) throws Exception { 235 236 Object parent = digester.peek(1); 238 Object child = digester.peek(0); 239 240 Object [] parameters = null; 242 if (m_paramCount > 0) { 243 parameters = (Object [])digester.popParams(); 244 if (LOG.isTraceEnabled()) { 245 for (int i = 0, size = parameters.length; i < size; i++) { 246 LOG.trace("[SetNextRuleWithParams](" + i + ")" + parameters[i]); 247 } 248 } 249 250 if (m_paramCount == 1 && parameters[0] == null) { 261 return; 262 } 263 264 } else if (m_paramTypes != null && m_paramTypes.length != 0) { 265 269 if (m_bodyText == null) { 272 return; 273 } 274 275 parameters = new Object [1]; 276 parameters[0] = m_bodyText; 277 if (m_paramTypes.length == 0) { 278 m_paramTypes = new Class [1]; 279 m_paramTypes[0] = String .class; 280 } 281 282 } else { 283 } 287 288 Object [] paramValues = new Object [m_paramTypes.length]; 292 293 Class propertyClass = child.getClass(); 294 for (int i = 0; i < m_paramTypes.length; i++) { 295 if (m_paramTypes[i] == propertyClass) { 296 paramValues[i] = child; 298 } else if (parameters[i] == null 299 || (parameters[i] instanceof String && !String .class.isAssignableFrom(m_paramTypes[i]))) { 300 paramValues[i] = ConvertUtils.convert((String )parameters[i], m_paramTypes[i]); 303 } else { 304 paramValues[i] = parameters[i]; 305 } 306 } 307 308 if (parent == null) { 309 StringBuffer sb = new StringBuffer (); 310 sb.append("[SetNextRuleWithParams]{"); 311 sb.append(digester.getMatch()); 312 sb.append("} Call target is null ("); 313 sb.append("targetOffset="); 314 sb.append(m_targetOffset); 315 sb.append(",stackdepth="); 316 sb.append(digester.getCount()); 317 sb.append(")"); 318 throw new org.xml.sax.SAXException (sb.toString()); 319 } 320 321 if (LOG.isDebugEnabled()) { 323 StringBuffer sb = new StringBuffer ("[SetNextRuleWithParams]{"); 324 sb.append(digester.getMatch()); 325 sb.append("} Call "); 326 sb.append(parent.getClass().getName()); 327 sb.append("."); 328 sb.append(m_methodName); 329 sb.append("("); 330 for (int i = 0; i < paramValues.length; i++) { 331 if (i > 0) { 332 sb.append(","); 333 } 334 if (paramValues[i] == null) { 335 sb.append("null"); 336 } else { 337 sb.append(paramValues[i].toString()); 338 } 339 sb.append("/"); 340 if (m_paramTypes[i] == null) { 341 sb.append("null"); 342 } else { 343 sb.append(m_paramTypes[i].getName()); 344 } 345 } 346 sb.append(")"); 347 LOG.debug(sb.toString()); 348 } 349 350 Object result = null; 351 if (m_useExactMatch) { 352 result = MethodUtils.invokeExactMethod(parent, m_methodName, paramValues, m_paramTypes); 354 355 } else { 356 result = MethodUtils.invokeMethod(parent, m_methodName, paramValues, m_paramTypes); 358 } 359 360 processMethodCallResult(result); 361 } 362 363 371 public void finish(String namespace, String name) throws Exception { 372 373 String dummy = name; 374 dummy = namespace; 375 dummy = null; 376 m_bodyText = dummy; 377 } 378 379 386 public boolean getUseExactMatch() { 387 388 return m_useExactMatch; 389 } 390 391 400 public void setDigester(Digester aDigester) { 401 402 aDigester.setLogger(CmsLog.getLog(aDigester.getClass())); 403 super.setDigester(aDigester); 405 if (m_paramClassNames != null) { 407 m_paramTypes = new Class [m_paramClassNames.length]; 408 for (int i = 0; i < m_paramClassNames.length; i++) { 409 try { 410 m_paramTypes[i] = aDigester.getClassLoader().loadClass(m_paramClassNames[i]); 411 } catch (ClassNotFoundException e) { 412 LOG.error(Messages.get().getBundle().key(Messages.ERR_LOAD_CLASS_1, m_paramClassNames[i]), e); 414 m_paramTypes[i] = null; } 416 } 417 } 418 } 419 420 427 public void setUseExactMatch(boolean useExactMatch) { 428 429 m_useExactMatch = useExactMatch; 430 } 431 432 437 public String toString() { 438 439 StringBuffer sb = new StringBuffer ("CallMethodRule["); 440 sb.append("methodName="); 441 sb.append(m_methodName); 442 sb.append(", paramCount="); 443 sb.append(m_paramCount); 444 sb.append(", paramTypes={"); 445 if (m_paramTypes != null) { 446 for (int i = 0; i < m_paramTypes.length; i++) { 447 if (i > 0) { 448 sb.append(", "); 449 } 450 sb.append(m_paramTypes[i].getName()); 451 } 452 } 453 sb.append("}"); 454 sb.append("]"); 455 return (sb.toString()); 456 457 } 458 459 465 protected void processMethodCallResult(Object result) { 466 467 if (result != null) { 469 } 471 } 472 } | Popular Tags |