1 64 65 68 package com.jcorporate.expresso.core.controller; 69 70 import com.jcorporate.expresso.core.misc.StringUtil; 71 import com.jcorporate.expresso.kernel.util.ClassLocator; 72 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 73 import org.w3c.dom.NamedNodeMap ; 74 import org.w3c.dom.Node ; 75 import org.w3c.dom.NodeList ; 76 77 import java.util.Enumeration ; 78 import java.util.HashMap ; 79 import java.util.Iterator ; 80 import java.util.Map ; 81 import java.util.Vector ; 82 83 84 94 public class ControllerElement 95 implements Cloneable , 96 java.io.Serializable { 97 98 101 private String name; 102 103 106 private String label; 107 108 113 private String type; 114 115 118 private String description; 119 120 123 private int lines = 1; 124 125 128 private Vector nested = null; 129 130 131 134 private ControllerElement parent = null; 135 136 139 private int displayLength = 60; 140 141 144 private HashMap attributes = null; 145 146 147 private ControllerResponse myResponse = null; 148 149 153 private Map mappedNested = null; 154 155 156 161 public Vector getContents() { 162 return getNested(); 163 } 164 165 166 169 protected ControllerElement() { 170 } 171 172 177 public void addNested(ControllerElement e) { 178 if (nested == null) { 179 nested = new Vector (3); 180 } 181 182 if (mappedNested == null) { 183 mappedNested = new HashMap (3); 184 } 185 186 187 nested.addElement(e); 188 mappedNested.put(e.getName(), e); 189 190 e.setParent(this); 191 ControllerResponse cr = getControllerResponse(); 192 if (cr != null) { 193 e.setControllerResponse(getControllerResponse()); 194 } 195 } 196 197 198 205 private Vector addNestedFrom(ControllerElement t) { 206 Vector v = new Vector (); 207 208 Vector tVec = t.getNestedOrNull(); 209 if (tVec == null) { 210 return v; 211 } 212 213 ControllerElement oneElement = null; 214 215 for (Enumeration e = tVec.elements(); e.hasMoreElements();) { 216 oneElement = (ControllerElement) e.nextElement(); 217 v.addElement(oneElement); 218 219 for (Enumeration e2 = addNestedFrom(oneElement).elements(); 220 e2.hasMoreElements();) { 221 v.addElement(e2.nextElement()); 222 } 223 } 224 225 226 return v; 227 } 228 229 234 public Vector allNested() { 235 Vector v = new Vector (); 236 237 for (Enumeration e = addNestedFrom(this).elements(); 238 e.hasMoreElements();) { 239 v.addElement(e.nextElement()); 240 } 241 242 return v; 243 } 244 245 251 public Object clone() 252 throws java.lang.CloneNotSupportedException { 253 Object o = null; 254 String className = getClass().getName(); 255 256 try { 257 Class c = ClassLocator.loadClass(className); 258 o = c.newInstance(); 259 } catch (ClassNotFoundException cn) { 260 throw new IllegalArgumentException ("State object '" + className + 261 "' not found"); 262 } catch (InstantiationException ie) { 263 throw new IllegalArgumentException ("State object '" + className + 264 "' cannot be instantiated"); 265 } catch (IllegalArgumentException e) { 266 throw new IllegalArgumentException ("State object '" + className + 267 "' cannot be instantiated (IllegalArgumentException)"); 268 } catch (IllegalAccessException iae) { 269 throw new IllegalArgumentException ("llegal access loading " + 270 "State object '" + className + 271 "'"); 272 } 273 synchronized (this) { 274 ControllerElement ce = (ControllerElement) o; 275 Vector v = this.getNestedOrNull(); 276 if (v != null) { 277 for (Enumeration e = v.elements(); e.hasMoreElements();) { 278 ControllerElement oneNested = (ControllerElement) e.nextElement(); 279 ce.addNested((ControllerElement) oneNested.clone()); 280 } 281 } 282 283 String oneKey = null; 284 HashMap attribs = getAttributes(); 285 286 if (attribs != null) { 287 for (Iterator e2 = attribs.keySet().iterator(); e2.hasNext();) { 288 oneKey = (String ) e2.next(); 289 ce.setAttribute(oneKey, (String ) attribs.get(oneKey)); 290 } 291 } 292 293 ce.description = description; 294 ce.displayLength = displayLength; 295 ce.label = label; 296 ce.lines = lines; 297 ce.name = name; 298 ce.parent = parent; 299 ce.type = type; 300 } 301 302 303 return o; 304 } 305 306 307 313 public static ControllerElement fromXML(Node n) 314 throws ControllerException { 315 ControllerElement ce = new ControllerElement(); 316 317 return fromXML(n, ce); 318 } 319 320 328 protected static ControllerElement fromXML(Node n, ControllerElement ce) 329 throws ControllerException { 330 if (!n.getNodeName().equals("controller-element")) { 331 throw new ControllerException("Failed To Get DOM Node of " + 332 " type 'controller-element' Got " + 333 n.getNodeName() + " instead."); 334 } 335 336 NamedNodeMap nm = n.getAttributes(); 337 338 341 Node attribute; 342 attribute = nm.getNamedItem("name"); 343 344 if (attribute != null) { 345 ce.name = attribute.getNodeValue(); 346 } 347 348 attribute = nm.getNamedItem("description"); 349 350 if (attribute != null) { 351 ce.description = attribute.getNodeValue(); 352 } 353 354 attribute = nm.getNamedItem("display-length"); 355 356 if (attribute != null) { 357 try { 358 ce.displayLength = Integer.parseInt(attribute.getNodeValue()); 359 } catch (NumberFormatException nfe) { 360 } 361 } 362 363 attribute = nm.getNamedItem("label"); 364 365 if (attribute != null) { 366 ce.label = attribute.getNodeValue(); 367 } 368 369 attribute = nm.getNamedItem("lines"); 370 371 if (attribute != null) { 372 try { 373 ce.lines = Integer.parseInt(attribute.getNodeValue()); 374 } catch (NumberFormatException nfe) { 375 } 376 } 377 378 attribute = nm.getNamedItem("name"); 379 380 if (attribute != null) { 381 ce.name = attribute.getNodeValue(); 382 } 383 384 attribute = nm.getNamedItem("type"); 385 386 if (attribute != null) { 387 ce.type = attribute.getNodeValue(); 388 } 389 390 NodeList nl = n.getChildNodes(); 391 392 396 for (int i = 0; i < nl.getLength(); i++) { 397 Node currentNode = nl.item(i); 398 399 if (currentNode.getNodeName().equals("element-attributes")) { 400 NodeList attrnl = currentNode.getChildNodes(); 401 402 for (int j = 0; j < attrnl.getLength(); j++) { 403 Node oneNode = attrnl.item(j); 404 405 if (oneNode.getNodeName().equals("element-attribute")) { 406 NamedNodeMap attrs = oneNode.getAttributes(); 407 String key = attrs.getNamedItem("name").getNodeValue(); 408 String value = attrs.getNamedItem("value").getNodeValue(); 409 410 if (key != null) { 411 if (value == null) { 412 value = (""); 413 } 414 if (ce.attributes == null) { 415 ce.attributes = new HashMap (); 416 } 417 418 ce.attributes.put(key, value); 419 } 420 } 421 } 422 } else if (currentNode.getNodeName().equals("nested-outputs")) { 423 NodeList nestedNodes = currentNode.getChildNodes(); 424 425 for (int k = 0; k < nestedNodes.getLength(); k++) { 426 Node oneNode = nestedNodes.item(k); 427 428 if (oneNode.getNodeName().equals("output")) { 429 ce.addNested(Output.fromXML(nestedNodes.item(k))); 430 } 431 } 432 } else if (currentNode.getNodeName().equals("nested-inputs")) { 433 NodeList nestedNodes = currentNode.getChildNodes(); 434 435 for (int k = 0; k < nestedNodes.getLength(); k++) { 436 Node oneNode = nestedNodes.item(k); 437 438 if (oneNode.getNodeName().equals("input")) { 439 ce.addNested(Input.fromXML(nestedNodes.item(k))); 440 } 441 } 442 } else if (currentNode.getNodeName().equals("nested-blocks")) { 443 NodeList nestedNodes = currentNode.getChildNodes(); 444 445 for (int k = 0; k < nestedNodes.getLength(); k++) { 446 Node oneNode = nestedNodes.item(k); 447 448 if (oneNode.getNodeName().equals("block")) { 449 ce.addNested(Block.fromXML(nestedNodes.item(k))); 450 } 451 } 452 } else if (currentNode.getNodeName().equals("nested-transitions")) { 453 NodeList nestedNodes = currentNode.getChildNodes(); 454 455 for (int k = 0; k < nestedNodes.getLength(); k++) { 456 Node oneNode = nestedNodes.item(k); 457 458 if (oneNode.getNodeName().equals("transition")) { 459 ce.addNested(Transition.fromXML(nestedNodes.item(k))); 460 } 461 } 462 } 463 } 464 465 466 return ce; 467 } 468 469 479 public String getAttribute(String att) { 480 if (attributes == null) { 481 return ""; 482 } 483 return StringUtil.notNull((String ) attributes.get(att)); 484 } 485 486 491 public HashMap getAttributes() { 492 if (attributes == null) { 493 attributes = new HashMap (); 494 } 495 return attributes; 496 } 497 498 503 public HashMap getAttributesOrNull() { 504 return attributes; 505 } 506 507 512 public ControllerElement getContent(String name) { 513 ControllerElement t = null; 514 Vector v = this.getNestedOrNull(); 515 if (v != null) { 516 for (Enumeration e = v.elements(); e.hasMoreElements();) { 517 t = (ControllerElement) e.nextElement(); 518 519 if (t.getName().equals(name)) { 520 return t; 521 } 522 } 523 } 524 525 return null; 526 } 527 528 534 public String getDescription() { 535 return StringUtil.notNull(description); 536 } 537 538 544 public int getDisplayLength() { 545 return displayLength; 546 } 547 548 553 public String getLabel() { 554 return StringUtil.notNull(label); 555 } 556 557 562 public int getLines() { 563 return lines; 564 } 565 566 572 public String getName() { 573 return StringUtil.notNull(name); 574 } 575 576 582 public String getTitle() { 583 return StringUtil.notNull(name); 584 } 585 586 591 public Vector getNested() { 592 if (nested == null) { 593 nested = new Vector (); 594 } 595 596 return nested; 597 } 598 599 600 610 public int getNestedCount() { 611 return getNested().size(); 612 } 613 614 620 public Vector getNestedOrNull() { 621 if (nested == null) { 622 return null; 623 } 624 625 return nested; 626 } 627 628 629 634 public Iterator getNestedIterator() { 635 return getNested().iterator(); 636 } 637 638 644 public ControllerElement getParent() { 645 return parent; 646 } 647 648 653 public String getType() { 654 return type; 655 } 656 657 664 public void remove() 665 throws ControllerException { 666 if (parent == null) { 667 String myName = (this.getClass().getName() + ".remove()"); 668 throw new ControllerException(myName + 669 " :This item is not nested " + 670 "in another item - cannot remove"); 671 } 672 673 parent.removeNested(this); 674 } 675 676 677 684 public void removeNested(ControllerElement elementToRemove) 685 throws ControllerException { 686 if (nested.removeElement(elementToRemove)) { 687 return; 688 } 689 690 String myName = (this.getClass().getName() + 691 ".removeNested(ControllerElement)"); 692 throw new ControllerException(myName + "No such nested element as '" + 693 elementToRemove.getName() + 694 "' in Output " + getName()); 695 } 696 697 698 704 public void setAttribute(String attrib, String val) { 705 if (attributes == null) { 706 attributes = new HashMap (); 707 } 708 attributes.put(attrib, val); 709 } 710 711 716 public void setDescription(String newDescription) { 717 description = newDescription; 718 } 719 720 726 public void setDisplayLength(int newLength) { 727 displayLength = newLength; 728 } 729 730 735 public void setLabel(String newLabel) { 736 label = newLabel; 737 } 738 739 744 public void setLines(int newLines) { 745 lines = newLines; 746 } 747 748 753 public void setName(String newName) { 754 name = newName; 755 } 756 757 763 public void setParent(ControllerElement t) { 764 parent = t; 765 } 766 767 772 public void setType(String s) { 773 type = s; 774 } 775 776 782 public FastStringBuffer toXML(FastStringBuffer stream) { 783 stream.append("<controller-element"); 784 785 if (name != null && name.length() > 0) { 786 stream.append(" name=\""); 787 stream.append(StringUtil.xmlEscape(name)); 788 stream.append("\""); 789 } 790 if (label != null && label.length() > 0) { 791 stream.append(" label=\""); 792 stream.append(StringUtil.xmlEscape(label)); 793 stream.append("\""); 794 } 795 if (type != null && type.length() > 0) { 796 stream.append(" type=\""); 797 stream.append(StringUtil.xmlEscape(type)); 798 stream.append("\""); 799 } 800 if (description != null && description.length() > 0) { 801 stream.append(" description=\""); 802 stream.append(StringUtil.xmlEscape(description)); 803 stream.append("\""); 804 } 805 if (displayLength > 0) { 806 stream.append(" display-length=\""); 807 stream.append(displayLength); 808 stream.append("\""); 809 } 810 if (lines > 0) { 811 stream.append(" lines=\""); 812 stream.append(lines); 813 stream.append("\""); 814 } 815 816 stream.append(">\n"); 817 818 if (attributes != null && !attributes.isEmpty()) { 819 stream.append("\t<element-attributes>\n"); 820 821 for (Iterator i = attributes.keySet().iterator(); i.hasNext();) { 822 String key = (String ) i.next(); 823 String value = null; 824 825 if (key != null) { 826 value = (String ) attributes.get(key); 827 828 if (value == null) { 832 value = ""; 833 } 834 835 stream.append("\t<element-attribute name=\""); 836 stream.append(StringUtil.xmlEscape(key)); 837 stream.append("\" value=\""); 838 stream.append(StringUtil.xmlEscape(value)); 839 stream.append("\"/>\n"); 840 } 841 } 842 843 stream.append("\t</element-attributes>\n"); 844 } 845 846 847 Vector nestedBlocks = new Vector (); 848 Vector nestedOutputs = new Vector (); 849 Vector nestedTransitions = new Vector (); 850 Vector nestedInputs = new Vector (); 851 Vector nested = this.getNestedOrNull(); 852 ControllerElement ce = null; 853 if (nested != null) { 854 for (Enumeration en = nested.elements(); en.hasMoreElements();) { 855 ce = (ControllerElement) en.nextElement(); 856 857 if (ce instanceof Input) { 858 nestedInputs.addElement(ce); 859 } 860 if (ce instanceof Output) { 861 nestedOutputs.addElement(ce); 862 } 863 if (ce instanceof Transition) { 864 nestedTransitions.addElement(ce); 865 } 866 if (ce instanceof Block) { 867 nestedBlocks.addElement(ce); 868 } 869 } 870 } 871 if (nestedOutputs.size() > 0) { 872 stream.append(" <nested-outputs>\n"); 873 874 Output oneOutput = null; 875 876 for (Enumeration eno = nestedOutputs.elements(); 877 eno.hasMoreElements();) { 878 oneOutput = (Output) eno.nextElement(); 879 stream = oneOutput.toXML(stream); 880 } 881 882 stream.append(" </nested-outputs>\n"); 883 } 884 if (nestedInputs.size() > 0) { 885 stream.append(" <nested-inputs>\n"); 886 887 Input oneInput = null; 888 889 for (Enumeration eni = nestedInputs.elements(); 890 eni.hasMoreElements();) { 891 oneInput = (Input) eni.nextElement(); 892 stream = oneInput.toXML(stream); 893 } 894 895 stream.append(" </nested-inputs>\n"); 896 } 897 if (nestedTransitions.size() > 0) { 898 stream.append(" <nested-transitions>\n"); 899 900 Transition oneTransition = null; 901 902 for (Enumeration ent = nestedTransitions.elements(); 903 ent.hasMoreElements();) { 904 oneTransition = (Transition) ent.nextElement(); 905 stream = oneTransition.toXML(stream); 906 } 907 908 stream.append(" </nested-transitions>\n"); 909 } 910 if (nestedBlocks.size() > 0) { 911 stream.append(" <nested-blocks>\n"); 912 913 Block oneBlock = null; 914 915 for (Enumeration enb = nestedBlocks.elements(); 916 enb.hasMoreElements();) { 917 oneBlock = (Block) enb.nextElement(); 918 stream = oneBlock.toXML(stream); 919 } 920 921 stream.append(" </nested-blocks>\n"); 922 } 923 924 stream.append("</controller-element>\n"); 925 926 return stream; 927 } 928 929 935 public ControllerElement getNested(String nestedName) 936 throws ControllerException { 937 ControllerElement oneElement = null; 938 Vector v = getNestedOrNull(); 939 940 if (v == null) { 941 return null; 942 } 943 944 for (Enumeration e = v.elements(); e.hasMoreElements();) { 945 oneElement = (ControllerElement) e.nextElement(); 946 947 if (oneElement.getName().equals(nestedName)) { 948 return oneElement; 949 } 950 } 951 952 953 return null; 954 } 955 956 960 public Map getNestedMap() { 961 if (mappedNested == null) { 962 mappedNested = new HashMap (); 963 } 964 return mappedNested; 965 } 966 967 968 974 public synchronized void setControllerResponse(ControllerResponse newResponse) { 975 myResponse = newResponse; 976 977 Vector allNested = getNestedOrNull(); 978 979 if (allNested == null) { 980 return; 981 } 982 983 ControllerElement ce = null; 984 985 int size = allNested.size(); 986 for (int i = 0; i < size; i++) { 987 ce = (ControllerElement) allNested.get(i); 988 ce.setControllerResponse(newResponse); 989 } 990 } 991 992 protected ControllerResponse getControllerResponse() { 993 return myResponse; 994 } 995 } 996 997 998 | Popular Tags |