1 16 17 package org.pentaho.plugin.misc; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.dom4j.Node; 29 import org.pentaho.messages.Messages; 30 import org.pentaho.plugin.ComponentBase; 31 32 45 public class UtilityComponent extends ComponentBase { 46 47 50 private static final long serialVersionUID = -3257037449482351540L; 51 52 List commandList = new ArrayList (); 53 54 HashMap tmpOutputs = new HashMap (); 55 56 61 62 public Log getLogger() { 63 return LogFactory.getLog(UtilityComponent.class); 64 } 65 66 protected boolean validateSystemSettings() { 67 return true; 69 } 70 71 protected boolean validateAction() { 72 boolean hasError = false; 73 commandList = new ArrayList (); 74 75 Node componentNode = getComponentDefinition(); 76 List commandNodes = componentNode.selectNodes("*"); 78 for (Iterator it = commandNodes.iterator(); it.hasNext();) { 79 Node commandNode = (Node) it.next(); 80 UtilityBase command = utilityFactory(commandNode); 81 if ((command != null) && command.validate(commandNode)) { 82 commandList.add(command); 83 } else { 84 hasError = true; 85 } 86 } 87 88 return (!hasError); 89 } 90 91 96 public void done() { 97 98 } 99 100 105 protected boolean executeAction() { 106 tmpOutputs = new HashMap (); try { 109 for (Iterator it = commandList.iterator(); it.hasNext();) { 110 UtilityBase ud = (UtilityBase) it.next(); 111 if (debug) 112 debug("Executing Utility - " + ud.getCommandName()); if (!ud.execute()) { 114 return (false); 115 } 116 } 117 118 Set outNames = getOutputNames(); 119 for (Iterator it = outNames.iterator(); it.hasNext();) { 120 String name = (String ) it.next(); 121 Object value = tmpOutputs.get(name); 122 if (value != null) { 123 setOutputValue(name, value); 124 } 125 } 126 127 return (true); 128 } finally { 129 tmpOutputs = null; } 131 } 132 133 198 199 204 public boolean init() { 205 if (debug) 206 debug(Messages.getString("TestComponent.DEBUG_INITIALIZING_TEST")); return true; 208 } 209 210 protected Object getActionParameterValue(String name) { 211 try { 212 return (getInputValue(name)); 213 } catch (Exception e) { 214 } 216 return (null); 217 } 218 219 243 private UtilityBase utilityFactory(Node commandNode) { 244 245 String commandName = commandNode.getName(); 246 if ((commandName == null) || (commandName.length() < 1)) { 247 error(Messages.getErrorString("UtilityComponent.ERROR_0006_NO_COMMAND")); return (null); 249 } 250 251 if ("format".equalsIgnoreCase(commandName)) { return (new FormatUtil()); 253 } 254 255 if ("print".equalsIgnoreCase(commandName)) { return (new PrintUtil()); 257 } 258 259 if ("copy".equalsIgnoreCase(commandName)) { return (new CopyUtil()); 261 } 262 263 if ("getmapvalues".equalsIgnoreCase(commandName)) { return (new GetMapValues()); 265 } 266 267 error(Messages.getErrorString("UtilityComponent.ERROR_0007_UNKNOWN_COMMAND", commandName)); return (null); 269 } 270 271 abstract class UtilityBase { 272 String commandName; 273 274 HashMap parameterMap; 275 276 HashMap parameterListMap; 277 278 public UtilityBase(String commandName) { 279 this.commandName = commandName; 280 parameterMap = new HashMap (); 281 parameterListMap = new HashMap (); 282 } 283 284 290 boolean addParameter(String paramName, Node cmdNode, boolean required) { 291 Node paramNode = cmdNode.selectSingleNode(paramName); 292 if (paramNode == null) { 293 if (required) { 294 error(Messages.getErrorString("TestComponent.ERROR_0002_PARAMETER_MISSING", paramName)); return (false); 296 } 297 return (true); 298 } 299 300 String paramValue = paramNode.getText(); 301 302 if (paramValue.startsWith("\"") && paramValue.endsWith("\"")) { parameterMap.put(paramName, paramValue); 304 } else { 305 parameterMap.put(paramName, paramValue); 306 } 307 return (true); 308 } 309 310 int addParameterList(String paramName, Node cmdNode, int minReqd) { 311 List paramList = cmdNode.selectNodes(paramName); 312 if (paramList.size() < minReqd) { 313 error(Messages.getErrorString("TestComponent.ERROR_0003_PARAMETER_MISSING", paramName, String.valueOf(minReqd), String.valueOf(paramList.size()))); } 315 316 ArrayList al = new ArrayList (); 317 for (Iterator it = paramList.iterator(); it.hasNext();) { 318 al.add(((Node) it.next()).getText()); 319 } 320 parameterListMap.put(paramName, al); 321 322 return (al.size()); 323 } 324 325 String getCommandName() { 326 return (commandName); 327 } 328 329 Object getParameter(String name) { 330 String paramValue = (String ) parameterMap.get(name); 331 return (getValueOf(paramValue)); 332 } 333 334 boolean addOutputParameter(String name, Object value) { 335 try { 336 tmpOutputs.put(name, value); 337 return (true); 338 } catch (Exception e) { 339 } 341 342 return (false); 343 } 344 345 boolean setOutputParameter(String name, Object value) { 346 try { 347 String paramValue = (String ) parameterMap.get(name); 348 tmpOutputs.put(paramValue, value); 349 return (true); 350 } catch (Exception e) { 351 } 353 354 return (false); 355 } 356 357 List getParameterList(String name) { 358 ArrayList rtnList = new ArrayList (); 359 List paramList = (List ) parameterListMap.get(name); 360 if (paramList == null) { 361 return (rtnList); 362 } 363 364 for (Iterator it = paramList.iterator(); it.hasNext();) { 365 rtnList.add(getValueOf((String ) it.next())); 366 } 367 return (rtnList); 368 } 369 370 List getParameterListNames(String name) { 371 ArrayList rtnList = new ArrayList (); 372 List paramList = (List ) parameterListMap.get(name); 373 if (paramList == null) { 374 return (rtnList); 375 } 376 377 for (Iterator it = paramList.iterator(); it.hasNext();) { 378 rtnList.add(it.next()); 379 } 380 381 return (rtnList); 382 } 383 384 Object getValueOf(String paramValue) { 385 if (paramValue == null) { 386 return (null); 387 } 388 389 if (paramValue.startsWith("\"") && paramValue.endsWith("\"")) { if (paramValue.length() < 3) { 391 return (""); } 393 return (paramValue.substring(1, paramValue.length() - 1)); 394 } 395 396 Object obj = tmpOutputs.get(paramValue); 397 if (obj != null) { 398 return (obj); 399 } 400 401 return (getInputValue(paramValue)); 402 } 403 404 abstract boolean validate(Node commandNode); 405 406 abstract boolean execute(); 407 } 408 409 class FormatUtil extends UtilityBase { 410 411 FormatUtil() { 412 super("format"); } 414 415 boolean validate(Node commandNode) { 416 if (!addParameter("format-string", commandNode, true)) { return (false); 418 } 419 420 if (!addParameter("return", commandNode, false)) { return (false); 422 } 423 424 addParameterList("arg", commandNode, 0); return (true); 426 } 427 428 boolean execute() { 429 try { 430 MessageFormat mf = new MessageFormat (getParameter("format-string").toString()); String theResult = mf.format(getParameterList("arg").toArray()); setOutputParameter("return", theResult); return (true); 434 } catch (Exception e) { 435 error(Messages.getString("UtilityComponent.ERROR_0001_FORMAT_ERROR")); } 437 return (false); 438 } 439 } 440 441 class PrintUtil extends UtilityBase { 442 443 PrintUtil() { 444 super("print"); } 446 447 boolean validate(Node commandNode) { 448 if (!addParameter("delimiter", commandNode, false)) { return (false); 450 } 451 452 addParameterList("arg", commandNode, 0); 454 return (true); 455 } 456 457 boolean execute() { 458 try { 459 StringBuffer sb = new StringBuffer ("\n***************************************************************\n"); 461 Object o = getParameter("delimiter"); String delim = (o != null) ? o.toString() : ""; 464 List args = getParameterList("arg"); for (Iterator it = args.iterator(); it.hasNext();) { 466 sb.append(it.next()).append(delim); 467 } 468 469 sb.append("\n***************************************************************\n"); 471 info(sb.toString()); 472 473 return (true); 474 } catch (Exception e) { 475 error(Messages.getString("UtilityComponent.ERROR_0002_MESSAGE_LOG_ERROR")); } 477 return (false); 478 } 479 } 480 481 class CopyUtil extends UtilityBase { 482 483 CopyUtil() { 484 super("copy"); } 486 487 boolean validate(Node commandNode) { 488 if (!addParameter("from", commandNode, false)) { return (false); 490 } 491 492 if (!addParameter("return", commandNode, false)) { return (false); 494 } 495 496 return (true); 497 } 498 499 boolean execute() { 500 try { 501 setOutputParameter("return", getParameter("from")); return (true); 503 } catch (Exception e) { 504 error(Messages.getString("UtilityComponent.ERROR_0003_ERROR_COPYING_PARAMETER")); } 506 return (false); 507 } 508 } 509 510 class GetMapValues extends UtilityBase { 511 512 GetMapValues() { 513 super("getmapvalues"); } 515 516 boolean validate(Node commandNode) { 517 if (!addParameter("property-map", commandNode, true)) { return (false); 519 } 520 addParameterList("arg", commandNode, 1); 522 return (true); 523 } 524 525 boolean execute() { 526 try { 527 Object mapObj = getParameter("property-map"); 529 if (!(mapObj instanceof Map )) { 530 error(Messages.getErrorString("UtilityComponent.ERROR_0004_PARAMETER_NOT_MAP", "property-map")); return (false); 532 } 533 534 Map srcMap = (Map ) mapObj; 535 for (Iterator it = getParameterListNames("arg").iterator(); it.hasNext();) { String key = it.next().toString(); 537 addOutputParameter(key, srcMap.get(key)); 538 } 539 540 return (true); 541 } catch (Exception e) { 542 error(Messages.getString(Messages.getString("UtilityComponent.ERROR_0005_GET_MAP_VALUES_ERROR"))); } 544 return (false); 545 } 546 } 547 548 } | Popular Tags |