1 23 package com.sun.enterprise.tools.jsfext.layout.xml; 24 25 import com.sun.enterprise.tools.jsfext.event.handlers.Handler; 26 import com.sun.enterprise.tools.jsfext.event.handlers.HandlerDefinition; 27 import com.sun.enterprise.tools.jsfext.event.handlers.IODescriptor; 28 import com.sun.enterprise.tools.jsfext.layout.descriptor.ComponentType; 29 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutAttribute; 30 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent; 31 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutDefinition; 32 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElement; 33 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutFacet; 34 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutForEach; 35 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutIf; 36 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutMarkup; 37 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutStaticText; 38 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutWhile; 39 import com.sun.enterprise.tools.jsfext.layout.descriptor.Resource; 40 import com.sun.enterprise.tools.jsfext.util.IncludeInputStream; 41 42 import java.io.IOException ; 43 import java.io.BufferedInputStream ; 44 import java.io.InputStream ; 45 import java.net.URL ; 46 import java.util.ArrayList ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Map ; 51 52 import javax.xml.parsers.DocumentBuilder ; 53 import javax.xml.parsers.DocumentBuilderFactory ; 54 import javax.xml.parsers.ParserConfigurationException ; 55 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Node ; 58 import org.w3c.dom.NodeList ; 59 import org.w3c.dom.NamedNodeMap ; 60 61 import org.xml.sax.EntityResolver ; 62 import org.xml.sax.ErrorHandler ; 63 import org.xml.sax.SAXException ; 64 65 66 74 public class XMLLayoutDefinitionReader { 75 76 84 public XMLLayoutDefinitionReader(URL url, EntityResolver entityResolver, ErrorHandler errorHandler, String baseURI) { 85 _url = url; 86 _entityResolver = entityResolver; 87 _errorHandler = errorHandler; 88 _baseURI = baseURI; 89 } 90 91 94 public URL getURL() { 95 return _url; 96 } 97 98 101 public EntityResolver getEntityResolver() { 102 return _entityResolver; 103 } 104 105 108 public ErrorHandler getErrorHandler() { 109 return _errorHandler; 110 } 111 112 115 public String getBaseURI() { 116 return _baseURI; 117 } 118 119 128 public LayoutDefinition read() throws IOException { 129 InputStream inputStream = new IncludeInputStream( 131 new BufferedInputStream (getURL().openStream())); 132 Document doc = null; 133 134 try { 135 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 137 dbf.setNamespaceAware(true); 138 dbf.setValidating(true); 139 dbf.setIgnoringComments(true); 140 dbf.setIgnoringElementContentWhitespace(false); 141 dbf.setCoalescing(false); 142 dbf.setExpandEntityReferences(true); 144 145 DocumentBuilder db = null; 147 try { 148 db = dbf.newDocumentBuilder(); 149 } catch (ParserConfigurationException ex) { 150 throw new RuntimeException (ex); 151 } 152 if (getEntityResolver() != null) { 153 db.setEntityResolver(getEntityResolver()); 154 } 155 if (getErrorHandler() != null) { 156 db.setErrorHandler(getErrorHandler()); 157 } 158 159 try { 161 doc = db.parse(inputStream, getBaseURI()); 162 } catch (SAXException ex) { 163 throw new RuntimeException (ex); 164 } 165 } finally { 166 try { 167 inputStream.close(); 168 } catch (Exception ex) { 169 } 171 } 172 173 return createLayoutDefinition(doc); 175 } 176 177 186 private LayoutDefinition createLayoutDefinition(Document doc) { 187 Node node = doc.getDocumentElement(); 189 if (!node.getNodeName().equalsIgnoreCase(LAYOUT_DEFINITION_ELEMENT)) { 190 throw new RuntimeException ("Document Element must be '" 191 + LAYOUT_DEFINITION_ELEMENT + "'"); 192 } 193 194 LayoutDefinition ld = new LayoutDefinition(""); 196 197 List childElements = getChildElements(node, RESOURCES_ELEMENT); 199 Iterator it = childElements.iterator(); 200 if (it.hasNext()) { 201 addResources(ld, (Node ) it.next()); 203 } 204 205 childElements = getChildElements(node, TYPES_ELEMENT); 207 it = childElements.iterator(); 208 if (it.hasNext()) { 209 addTypes(ld, (Node ) it.next()); 211 } 212 213 childElements = getChildElements(node, HANDLERS_ELEMENT); 215 it = childElements.iterator(); 216 if (it.hasNext()) { 217 cacheHandlerDefs((Node ) it.next()); 219 } 220 221 childElements = getChildElements(node, EVENT_ELEMENT); 223 it = childElements.iterator(); 224 if (it.hasNext()) { 225 Node eventNode = (Node ) it.next(); 228 String type = (String ) getAttributes(eventNode). 229 get(TYPE_ATTRIBUTE); 230 231 List handlers = ld.getHandlers(type); 233 ld.setHandlers(type, getHandlers(eventNode, handlers)); 234 } 235 236 childElements = getChildElements(node, LAYOUT_ELEMENT); 238 it = childElements.iterator(); 239 if (it.hasNext()) { 240 addChildLayoutElements(ld, (Node ) it.next()); 242 } else { 243 throw new RuntimeException ("A '" + LAYOUT_ELEMENT 244 + "' element is required in the XML document!"); 245 } 246 247 return ld; 249 } 250 251 259 private void addResources(LayoutDefinition ld, Node node) { 260 Iterator it = getChildElements(node, RESOURCE_ELEMENT).iterator(); 262 263 while (it.hasNext()) { 265 ld.addResource(createResource((Node ) it.next())); 267 } 268 } 269 270 279 private Resource createResource(Node node) { 280 Map attributes = getAttributes(node); 282 String id = (String ) attributes.get(ID_ATTRIBUTE); 283 String extraInfo = 284 (String ) attributes.get(EXTRA_INFO_ATTRIBUTE); 285 String factoryClass = 286 (String ) attributes.get(FACTORY_CLASS_ATTRIBUTE); 287 288 if ((factoryClass == null) || (id == null) || (extraInfo == null) 290 || (factoryClass.trim().equals("")) || (id.trim().equals("")) 291 || (extraInfo.trim().equals(""))) { 292 throw new RuntimeException ("'" + ID_ATTRIBUTE + "', '" 293 + EXTRA_INFO_ATTRIBUTE + "', and '" 294 + FACTORY_CLASS_ATTRIBUTE + "' are required attributes of '" 295 + RESOURCE_ELEMENT + "' Element!"); 296 } 297 298 return new Resource(id, extraInfo, factoryClass); 300 } 301 302 311 private void addTypes(LayoutDefinition ld, Node node) { 312 Iterator it = getChildElements(node, COMPONENT_TYPE_ELEMENT).iterator(); 314 315 while (it.hasNext()) { 317 ld.addComponentType(createComponentType((Node ) it.next())); 318 } 319 } 320 321 330 private ComponentType createComponentType(Node node) { 331 Map attributes = getAttributes(node); 333 String id = (String ) attributes.get(ID_ATTRIBUTE); 334 String factoryClass = 335 (String ) attributes.get(FACTORY_CLASS_ATTRIBUTE); 336 337 if ((factoryClass == null) || (id == null) 339 || (factoryClass.trim().equals("")) || (id.trim().equals(""))) { 340 throw new RuntimeException ("Both '" + ID_ATTRIBUTE + "' and '" 341 + FACTORY_CLASS_ATTRIBUTE + "' are required attributes of '" 342 + COMPONENT_TYPE_ELEMENT + "' Element!"); 343 } 344 345 return new ComponentType(id, factoryClass); 347 } 348 349 357 private void cacheHandlerDefs(Node node) { 358 HandlerDefinition def = null; 359 360 Iterator it = 362 getChildElements(node, HANDLER_DEFINITION_ELEMENT).iterator(); 363 while (it.hasNext()) { 364 def = createHandlerDefinition((Node ) it.next()); 366 _handlerDefs.put(def.getId(), def); 367 } 368 } 369 370 387 public HandlerDefinition createHandlerDefinition(Node node) { 388 389 Map attributes = getAttributes(node); 391 String value = (String ) attributes.get(ID_ATTRIBUTE); 392 HandlerDefinition hd = new HandlerDefinition(value); 393 394 396 value = (String ) attributes.get(CLASS_NAME_ATTRIBUTE); 398 if ((value != null) && !value.equals("")) { 399 String tmpStr = 401 (String ) attributes.get(METHOD_NAME_ATTRIBUTE); 402 if ((tmpStr == null) || tmpStr.equals("")) { 403 throw new IllegalArgumentException ("You must provide a '" 404 + METHOD_NAME_ATTRIBUTE + "' attribute on the '" 405 + HANDLER_DEFINITION_ELEMENT + "' element with " 406 + CLASS_NAME_ATTRIBUTE + " atttribute equal to '" 407 + value + "'."); 408 } 409 hd.setHandlerMethod(value, tmpStr); 410 } 411 412 List handlers = hd.getChildHandlers(); 416 hd.setChildHandlers(getHandlers(node, handlers)); 417 418 addInputDefs(hd, node); 420 421 addOutputDefs(hd, node); 423 424 return hd; 426 } 427 428 443 private List getHandlers(Node node, List handlers) { 444 Iterator it = getChildElements(node, HANDLER_ELEMENT).iterator(); 446 447 if (handlers == null) { 449 handlers = new ArrayList (); 450 } 451 while (it.hasNext()) { 452 handlers.add(createHandler((Node ) it.next())); 454 } 455 456 return handlers; 458 } 459 460 471 private Handler createHandler(Node handlerNode) { 472 String id = (String ) getAttributes(handlerNode). 474 get(ID_ATTRIBUTE); 475 if ((id == null) || (id.trim().equals(""))) { 476 throw new RuntimeException ("'" + ID_ATTRIBUTE 477 + "' attribute not found on '" + HANDLER_ELEMENT 478 + "' Element!"); 479 } 480 481 HandlerDefinition handlerDef = (HandlerDefinition) _handlerDefs.get(id); 483 if (handlerDef == null) { 484 throw new IllegalArgumentException (HANDLER_ELEMENT + " elements " 485 + ID_ATTRIBUTE + " attribute must match the " 486 + ID_ATTRIBUTE + " attribute of a " 487 + HANDLER_DEFINITION_ELEMENT + ". A HANDLER_ELEMENT with '" 488 + id + "' was specified, however there is no cooresponding " 489 + HANDLER_DEFINITION_ELEMENT + " with a matching " 490 + ID_ATTRIBUTE + " attribute."); 491 } 492 493 Handler handler = new Handler(handlerDef); 495 496 Map attributes = null; 498 Node inputNode = null; 499 Iterator it = getChildElements(handlerNode, INPUT_ELEMENT).iterator(); 500 while (it.hasNext()) { 501 inputNode = (Node ) it.next(); 503 attributes = getAttributes(inputNode); 504 handler.setInputValue( 505 (String ) attributes.get(NAME_ATTRIBUTE), 506 getValueFromNode(inputNode, attributes)); 507 } 508 509 it = getChildElements(handlerNode, OUTPUT_MAPPING_ELEMENT).iterator(); 511 while (it.hasNext()) { 512 attributes = getAttributes((Node ) it.next()); 514 handler.setOutputMapping( 515 (String ) attributes.get(OUTPUT_NAME_ATTRIBUTE), 516 (String ) attributes.get(TARGET_KEY_ATTRIBUTE), 517 (String ) attributes.get(TARGET_TYPE_ATTRIBUTE)); 518 } 519 520 return handler; 522 } 523 524 534 private void addInputDefs(HandlerDefinition hd, Node hdNode) { 535 Iterator it = getChildElements(hdNode, INPUT_DEF_ELEMENT).iterator(); 537 538 while (it.hasNext()) { 540 hd.addInputDef(createIODescriptor((Node ) it.next())); 542 } 543 } 544 545 555 private void addOutputDefs(HandlerDefinition hd, Node hdNode) { 556 Iterator it = getChildElements(hdNode, OUTPUT_DEF_ELEMENT).iterator(); 558 559 while (it.hasNext()) { 561 hd.addOutputDef(createIODescriptor((Node ) it.next())); 563 } 564 } 565 566 580 private IODescriptor createIODescriptor(Node node) { 581 Map attributes = getAttributes(node); 583 String name = (String ) attributes.get(NAME_ATTRIBUTE); 584 if ((name == null) || name.equals("")) { 585 throw new IllegalArgumentException ("Name must be provided!"); 586 } 587 String type = (String ) attributes.get(TYPE_ATTRIBUTE); 588 if ((type == null) || type.equals("")) { 589 throw new IllegalArgumentException ("Type must be provided!"); 590 } 591 Object def = attributes.get(DEFAULT_ATTRIBUTE); 592 String req = (String ) attributes.get(REQUIRED_ATTRIBUTE); 593 594 IODescriptor ioDesc = new IODescriptor(name, type); 596 ioDesc.setDefault(def); 597 if (req != null) { 598 ioDesc.setRequired(Boolean.valueOf(req).booleanValue()); 599 } 600 602 return ioDesc; 604 } 605 606 612 private void addChildLayoutElements(LayoutElement layElt, Node node) { 613 Iterator it = getChildElements(node).iterator(); 615 616 Node childNode = null; 621 String name = null; 622 while (it.hasNext()) { 623 childNode = (Node ) it.next(); 624 name = childNode.getNodeName(); 625 if (name.equalsIgnoreCase(IF_ELEMENT)) { 626 layElt.addChildLayoutElement( 628 createLayoutIf(layElt, childNode)); 629 } else if (name.equalsIgnoreCase(ATTRIBUTE_ELEMENT)) { 630 LayoutElement childElt = 632 createLayoutAttribute(layElt, childNode); 633 if (childElt != null) { 634 layElt.addChildLayoutElement(childElt); 635 } 636 } else if (name.equalsIgnoreCase(MARKUP_ELEMENT)) { 637 layElt.addChildLayoutElement( 639 createLayoutMarkup(layElt, childNode)); 640 } else if (name.equalsIgnoreCase(FACET_ELEMENT)) { 641 layElt.addChildLayoutElement( 643 createLayoutFacet(layElt, childNode)); 644 } else if (name.equalsIgnoreCase(STATIC_TEXT_ELEMENT)) { 645 layElt.addChildLayoutElement( 647 createLayoutStaticText(layElt, childNode)); 648 } else if (name.equalsIgnoreCase(COMPONENT_ELEMENT)) { 649 layElt.addChildLayoutElement( 651 createLayoutComponent(layElt, childNode)); 652 } else if (name.equalsIgnoreCase(EVENT_ELEMENT)) { 653 name = (String ) getAttributes(childNode). 656 get(TYPE_ATTRIBUTE); 657 List handlers = layElt.getHandlers(name); 659 layElt.setHandlers(name, getHandlers(childNode, handlers)); 660 } else if (name.equalsIgnoreCase(FOREACH_ELEMENT)) { 661 layElt.addChildLayoutElement( 663 createLayoutForEach(layElt, childNode)); 664 } else if (name.equalsIgnoreCase(WHILE_ELEMENT)) { 665 layElt.addChildLayoutElement( 667 createLayoutWhile(layElt, childNode)); 668 } else if (name.equalsIgnoreCase(EDIT_ELEMENT)) { 669 layElt.addChildLayoutElement( 671 createEditLayoutComponent(layElt, childNode)); 672 } else { 673 throw new RuntimeException ("Unknown Element Found: '" 674 + childNode.getNodeName() + "' under '" 675 + node.getNodeName() + "'."); 676 } 677 } 678 } 679 680 688 private LayoutElement createLayoutIf(LayoutElement parent, Node node) { 689 String condition = (String ) getAttributes(node).get( 691 CONDITION_ATTRIBUTE); 692 if ((condition == null) || (condition.trim().equals(""))) { 693 throw new RuntimeException ("'" + CONDITION_ATTRIBUTE 694 + "' attribute not found on '" + IF_ELEMENT + "' Element!"); 695 } 696 697 LayoutElement ifElt = new LayoutIf(parent, condition); 699 700 addChildLayoutElements(ifElt, node); 702 703 return ifElt; 705 } 706 707 718 private LayoutElement createLayoutForEach(LayoutElement parent, Node node) { 719 String list = (String ) getAttributes(node).get( 721 LIST_ATTRIBUTE); 722 if ((list == null) || (list.trim().equals(""))) { 723 throw new RuntimeException ("'" + LIST_ATTRIBUTE 724 + "' attribute not found on '" + FOREACH_ELEMENT 725 + "' Element!"); 726 } 727 String key = (String ) getAttributes(node).get( 728 KEY_ATTRIBUTE); 729 if ((key == null) || (key.trim().equals(""))) { 730 throw new RuntimeException ("'" + KEY_ATTRIBUTE 731 + "' attribute not found on '" + FOREACH_ELEMENT 732 + "' Element!"); 733 } 734 735 LayoutElement forEachElt = new LayoutForEach(parent, list, key); 737 738 addChildLayoutElements(forEachElt, node); 740 741 return forEachElt; 743 } 744 745 755 private LayoutElement createLayoutWhile(LayoutElement parent, Node node) { 756 String condition = (String ) getAttributes(node).get( 758 CONDITION_ATTRIBUTE); 759 if ((condition == null) || (condition.trim().equals(""))) { 760 throw new RuntimeException ("'" + CONDITION_ATTRIBUTE 761 + "' attribute not found on '" + WHILE_ELEMENT 762 + "' Element!"); 763 } 764 765 LayoutElement whileElt = new LayoutWhile(parent, condition); 767 768 addChildLayoutElements(whileElt, node); 770 771 return whileElt; 773 } 774 775 783 private LayoutElement createLayoutAttribute(LayoutElement parent, Node node) { 784 Map attributes = getAttributes(node); 786 String name = (String ) attributes.get(NAME_ATTRIBUTE); 787 if ((name == null) || (name.trim().equals(""))) { 788 throw new RuntimeException ("'" + NAME_ATTRIBUTE 789 + "' attribute not found on '" + ATTRIBUTE_ELEMENT 790 + "' Element!"); 791 } 792 LayoutElement attributeElt = null; 793 794 LayoutComponent comp = null; 797 if (parent instanceof LayoutComponent) { 798 comp = (LayoutComponent) parent; 799 } else { 800 comp = getParentLayoutComponent(parent); 801 } 802 if (comp != null) { 803 addOption(comp, node); 805 } else { 806 String value = (String ) attributes.get(VALUE_ATTRIBUTE); 807 String property = (String ) attributes.get(PROPERTY_ATTRIBUTE); 808 809 attributeElt = new LayoutAttribute(parent, name, value, property); 811 812 addChildLayoutElements(attributeElt, node); 814 } 815 816 return attributeElt; 818 } 819 820 827 private LayoutElement createLayoutMarkup(LayoutElement parent, Node node) { 828 Map attributes = getAttributes(node); 830 String tag = (String ) attributes.get(TAG_ATTRIBUTE); 831 if ((tag == null) || (tag.trim().equals(""))) { 832 throw new RuntimeException ("'" + TAG_ATTRIBUTE 833 + "' attribute not found on '" + MARKUP_ELEMENT 834 + "' Element!"); 835 } 836 837 LayoutElement markupElt = null; 840 if ((parent instanceof LayoutComponent) 841 || isNestedLayoutComponent(parent)) { 842 ComponentType type = ensureMarkupType(parent); 844 markupElt = new LayoutComponent( 845 parent, MARKUP_ELEMENT + _markupCount++, type); 846 LayoutComponent markupComp = ((LayoutComponent) markupElt); 847 markupComp.addOption("tag", tag); 848 markupComp.setNested(true); 849 markupComp.setFacetChild(false); 850 851 addChildLayoutComponentChildren(markupComp, node); 853 } else { 854 String type = (String ) attributes.get(TYPE_ATTRIBUTE); 856 markupElt = new LayoutMarkup(parent, tag, type); 857 858 addChildLayoutElements(markupElt, node); 860 } 861 862 return markupElt; 864 } 865 866 876 private LayoutElement createLayoutFacet(LayoutElement parent, Node node) { 877 String id = (String ) getAttributes(node).get(ID_ATTRIBUTE); 880 if ((id == null) || (id.trim().equals(""))) { 881 throw new RuntimeException ("'" + ID_ATTRIBUTE 882 + "' attribute not found on '" + FACET_ELEMENT 883 + "' Element!"); 884 } 885 886 LayoutFacet facetElt = new LayoutFacet(parent, id); 888 889 String rendered = (String ) getAttributes(node).get(RENDERED_ATTRIBUTE); 891 boolean isRendered = true; 892 if ((rendered == null) || rendered.trim().equals("") 893 || rendered.equals(AUTO_RENDERED)) { 894 isRendered = !isNestedLayoutComponent(facetElt); 896 } else { 897 isRendered = Boolean.getBoolean(rendered); 898 } 899 facetElt.setRendered(isRendered); 900 901 addChildLayoutElements(facetElt, node); 903 904 return facetElt; 906 } 907 908 922 private static boolean isLayoutComponentChild(LayoutElement elt) { 923 elt = elt.getParent(); 924 while (elt != null) { 925 if (elt instanceof LayoutComponent) { 926 return true; 927 } else if (elt instanceof LayoutFacet) { 928 return false; 930 } 931 elt = elt.getParent(); 932 } 933 934 return false; 936 } 937 938 948 public static boolean isNestedLayoutComponent(LayoutElement elt) { 949 return (getParentLayoutComponent(elt) != null); 950 } 951 952 958 private LayoutElement createLayoutComponent(LayoutElement parent, Node node) { 959 Map attributes = getAttributes(node); 961 String id = (String ) attributes.get(ID_ATTRIBUTE); 962 String type = (String ) attributes.get(TYPE_ATTRIBUTE); 963 if ((type == null) || (type.trim().equals(""))) { 964 throw new RuntimeException ("'" + TYPE_ATTRIBUTE 965 + "' attribute not found on '" + COMPONENT_ELEMENT 966 + "' Element!"); 967 } 968 969 LayoutComponent component = new LayoutComponent(parent, id, 971 getComponentType(parent, type)); 972 973 String overwrite = (String ) attributes.get(OVERWRITE_ATTRIBUTE); 975 if ((overwrite != null) && (overwrite.length() > 0)) { 976 component.setOverwrite(Boolean.valueOf(overwrite).booleanValue()); 977 } 978 979 component.setNested(isNestedLayoutComponent(component)); 989 990 if (isLayoutComponentChild(component)) { 992 component.setFacetChild(false); 993 } else { 994 while (parent != null) { 997 if (parent instanceof LayoutFacet) { 998 if (isLayoutComponentChild(parent)) { 1003 id = parent.getUnevaluatedId(); 1004 } 1005 break; 1006 } 1007 parent = parent.getParent(); 1008 } 1009 component.addOption(LayoutComponent.FACET_NAME, id); 1011 } 1012 1013 addChildLayoutComponentChildren(component, node); 1015 1016 return component; 1018 } 1019 1020 1023 private void addChildLayoutComponentChildren(LayoutComponent component, Node node) { 1024 Iterator it = getChildElements(node).iterator(); 1026 1027 Node childNode = null; 1030 String name = null; 1031 while (it.hasNext()) { 1032 childNode = (Node ) it.next(); 1033 name = childNode.getNodeName(); 1034 if (name.equalsIgnoreCase(COMPONENT_ELEMENT)) { 1035 component.addChildLayoutElement( 1037 createLayoutComponent(component, childNode)); 1038 } else if (name.equalsIgnoreCase(FACET_ELEMENT)) { 1039 component.addChildLayoutElement( 1041 createLayoutFacet(component, childNode)); 1042 } else if (name.equalsIgnoreCase(OPTION_ELEMENT)) { 1043 addOption(component, childNode); 1045 } else if (name.equalsIgnoreCase(EVENT_ELEMENT)) { 1046 name = (String ) getAttributes(childNode). 1049 get(TYPE_ATTRIBUTE); 1050 1051 List handlers = component.getHandlers(name); 1053 component.setHandlers(name, getHandlers(childNode, handlers)); 1054 } else if (name.equalsIgnoreCase(EDIT_ELEMENT)) { 1055 component.addChildLayoutElement( 1057 createEditLayoutComponent(component, childNode)); 1058 } else if (name.equalsIgnoreCase(MARKUP_ELEMENT)) { 1059 component.addChildLayoutElement( 1061 createLayoutMarkup(component, childNode)); 1062 } else if (name.equalsIgnoreCase(ATTRIBUTE_ELEMENT)) { 1063 createLayoutAttribute(component, childNode); 1069 } else { 1070 throw new RuntimeException ("Unknown Element Found: '" 1071 + childNode.getNodeName() + "' under '" 1072 + COMPONENT_ELEMENT + "'."); 1073 } 1074 } 1075 } 1076 1077 1090 private LayoutElement createEditLayoutComponent(LayoutElement parent, Node node) { 1091 parent = createEditPopupMenuLayoutComponent(parent, node); 1093 1094 Map attributes = getAttributes(node); 1096 String id = (String ) attributes.get(ID_ATTRIBUTE); 1097 1098 ComponentType type = ensureEditAreaType(parent); 1100 LayoutComponent component = 1101 new LayoutComponent(parent, EDITABLE + id, type); 1102 parent.addChildLayoutElement(component); 1103 1104 component.setNested(isNestedLayoutComponent(component)); 1106 component.setFacetChild(false); 1107 component.addOption(EDITABLE, Boolean.TRUE); 1109 addChildLayoutComponentChildren(component, node); 1111 1112 return parent; 1113 } 1114 1115 1119 private LayoutElement createEditPopupMenuLayoutComponent(LayoutElement parent, Node node) { 1120 Map attributes = getAttributes(node); 1122 String id = (String ) attributes.get(ID_ATTRIBUTE); 1123 1124 ComponentType type = ensurePopupMenuType(parent); 1126 LayoutComponent popupMenu = 1127 new LayoutComponent(parent, EDIT_MENU + id, type); 1128 1129 popupMenu.setNested(isNestedLayoutComponent(popupMenu)); 1131 popupMenu.setFacetChild(false); 1132 1133 1135 return popupMenu; 1137 } 1138 1139 1143 private ComponentType ensurePopupMenuType(LayoutElement elt) { 1144 LayoutDefinition ld = elt.getLayoutDefinition(); 1146 ComponentType type = ld.getComponentType(POPUP_MENU_TYPE); 1147 if (type == null) { 1148 type = new ComponentType(POPUP_MENU_TYPE, POPUP_MENU_TYPE_CLASS); 1150 ld.addComponentType(type); 1151 } 1152 1153 return type; 1155 } 1156 1157 1161 private ComponentType ensureEditAreaType(LayoutElement elt) { 1162 LayoutDefinition ld = elt.getLayoutDefinition(); 1164 ComponentType type = ld.getComponentType(EDIT_AREA_TYPE); 1165 if (type == null) { 1166 type = new ComponentType(EDIT_AREA_TYPE, EDIT_AREA_TYPE_CLASS); 1168 ld.addComponentType(type); 1169 } 1170 1171 return type; 1173 } 1174 1175 1179 private ComponentType ensureMarkupType(LayoutElement elt) { 1180 LayoutDefinition ld = elt.getLayoutDefinition(); 1182 ComponentType type = ld.getComponentType(MARKUP_ELEMENT); 1183 if (type == null) { 1184 type = new ComponentType(MARKUP_ELEMENT, MARKUP_FACTORY_CLASS); 1186 ld.addComponentType(type); 1187 } 1188 1189 return type; 1191 } 1192 1193 1201 private void addOption(LayoutComponent component, Node node) { 1202 Map attributes = getAttributes(node); 1204 1205 String name = (String ) attributes.get(NAME_ATTRIBUTE); 1207 if ((name == null) || (name.trim().equals(""))) { 1208 throw new RuntimeException ("'" + NAME_ATTRIBUTE 1209 + "' attribute not found on '" + OPTION_ELEMENT 1210 + "' Element!"); 1211 } 1212 name = name.trim(); 1213 1214 Object value = getValueFromNode(node, attributes); 1216 1217 component.addOption(name, value); 1219 } 1220 1221 1236 private Object getValueFromNode(Node node, Map attributes) { 1237 Object value = attributes.get(VALUE_ATTRIBUTE); 1238 if (value == null) { 1239 List list = new ArrayList (); 1242 Iterator it = getChildElements(node, LIST_ELEMENT).iterator(); 1243 while (it.hasNext()) { 1244 list.add(getAttributes((Node ) it.next()). 1246 get(VALUE_ATTRIBUTE)); 1247 } 1248 if (list.size() > 0) { 1249 value = list; 1251 } 1252 } 1253 return value; 1254 } 1255 1256 1262 private LayoutElement createLayoutStaticText(LayoutElement parent, Node node) { 1263 LayoutStaticText text = 1265 new LayoutStaticText(parent, "", getTextNodesAsString(node)); 1266 1267 1270 1272 return text; 1274 } 1275 1276 1277 1278 1282 1291 public List getChildElements(Node node) { 1292 return getChildElements(node, null); 1293 } 1294 1295 1306 public List getChildElements(Node node, String name) { 1307 NodeList nodes = node.getChildNodes(); 1309 if (nodes == null) { 1310 return new ArrayList (0); 1312 } 1313 1314 List list = new ArrayList (); 1316 1317 Node childNode = null; 1319 for (int idx = 0; idx < nodes.getLength(); idx++) { 1320 childNode = nodes.item(idx); 1321 if (childNode.getNodeType() != Node.ELEMENT_NODE) { 1322 continue; 1324 } 1325 1326 if ((name == null) || childNode.getNodeName().equalsIgnoreCase(name)) { 1328 list.add(childNode); 1329 } 1330 } 1331 1332 return list; 1334 } 1335 1336 1337 1349 public String getTextNodesAsString(Node node) { 1350 NodeList nodes = node.getChildNodes(); 1352 if (nodes == null) { 1353 return null; 1355 } 1356 1357 StringBuffer buf = new StringBuffer (""); 1359 1360 Node childNode = null; 1362 for (int idx = 0; idx < nodes.getLength(); idx++) { 1363 childNode = nodes.item(idx); 1364 if ((childNode.getNodeType() != Node.TEXT_NODE) 1365 && (childNode.getNodeType() != Node.CDATA_SECTION_NODE)) { 1366 continue; 1368 } 1369 buf.append(childNode.getNodeValue()); 1370 } 1371 1372 return buf.toString(); 1374 } 1375 1376 1377 1378 1388 public Map getAttributes(Node node) { 1389 NamedNodeMap attributes = node.getAttributes(); 1391 if ((attributes == null) || (attributes.getLength() == 0)) { 1392 return new HashMap (0); 1394 } 1395 1396 Map map = new HashMap (); 1398 1399 Node attNode = null; 1401 for (int idx = 0; idx < attributes.getLength(); idx++) { 1402 attNode = attributes.item(idx); 1403 map.put(attNode.getNodeName().toLowerCase(), 1404 attNode.getNodeValue()); 1405 } 1406 1407 return map; 1409 } 1410 1411 1412 1423 public ComponentType getComponentType(LayoutElement elt, String type) { 1424 ComponentType componentType = 1426 elt.getLayoutDefinition().getComponentType(type); 1427 if (componentType == null) { 1428 throw new IllegalArgumentException ("ComponentType '" + type 1429 + "' not defined!"); 1430 } 1431 return componentType; 1432 } 1433 1434 1442 public static LayoutComponent getParentLayoutComponent(LayoutElement elt) { 1443 elt = elt.getParent(); 1444 while (elt != null) { 1445 if (elt instanceof LayoutComponent) { 1446 return (LayoutComponent) elt; 1447 } 1448 elt = elt.getParent(); 1449 } 1450 return null; 1451 } 1452 1453 1454 1458 public static final String ATTRIBUTE_ELEMENT = 1459 "attribute"; 1460 public static final String COMPONENT_ELEMENT = 1461 "component"; 1462 public static final String COMPONENT_TYPE_ELEMENT = 1463 "componenttype"; 1464 public static final String EDIT_ELEMENT = 1465 "edit"; 1466 public static final String EVENT_ELEMENT = 1467 "event"; 1468 public static final String FACET_ELEMENT = 1469 "facet"; 1470 public static final String FOREACH_ELEMENT = 1471 "foreach"; 1472 public static final String HANDLER_ELEMENT = 1473 "handler"; 1474 public static final String HANDLERS_ELEMENT = 1475 "handlers"; 1476 public static final String HANDLER_DEFINITION_ELEMENT = 1477 "handlerdefinition"; 1478 public static final String IF_ELEMENT = 1479 "if"; 1480 public static final String INPUT_DEF_ELEMENT = 1481 "inputdef"; 1482 public static final String INPUT_ELEMENT = 1483 "input"; 1484 public static final String LAYOUT_DEFINITION_ELEMENT = 1485 "layoutdefinition"; 1486 public static final String LAYOUT_ELEMENT = 1487 "layout"; 1488 public static final String LIST_ELEMENT = 1489 "list"; 1490 public static final String MARKUP_ELEMENT = 1491 "markup"; 1492 public static final String OPTION_ELEMENT = 1493 "option"; 1494 public static final String OUTPUT_DEF_ELEMENT = 1495 "outputdef"; 1496 public static final String OUTPUT_MAPPING_ELEMENT = 1497 "outputmapping"; 1498 public static final String STATIC_TEXT_ELEMENT = 1499 "statictext"; 1500 public static final String TYPES_ELEMENT = 1501 "types"; 1502 public static final String RESOURCES_ELEMENT = 1503 "resources"; 1504 public static final String RESOURCE_ELEMENT = 1505 "resource"; 1506 public static final String WHILE_ELEMENT = 1507 "while"; 1508 1509 public static final String CLASS_NAME_ATTRIBUTE = 1510 "classname"; 1511 public static final String CONDITION_ATTRIBUTE = 1512 "condition"; 1513 public static final String DEFAULT_ATTRIBUTE = 1514 "default"; 1515 public static final String DESCRIPTION_ATTRIBUTE = 1516 "description"; 1517 public static final String EXTRA_INFO_ATTRIBUTE = 1518 "extrainfo"; 1519 public static final String FACTORY_CLASS_ATTRIBUTE = 1520 "factoryclass"; 1521 public static final String ID_ATTRIBUTE = 1522 "id"; 1523 public static final String KEY_ATTRIBUTE = 1524 "key"; 1525 public static final String LIST_ATTRIBUTE = 1526 "list"; 1527 public static final String METHOD_NAME_ATTRIBUTE = 1528 "methodname"; 1529 public static final String NAME_ATTRIBUTE = 1530 "name"; 1531 public static final String OUTPUT_NAME_ATTRIBUTE = 1532 "outputname"; 1533 public static final String OVERWRITE_ATTRIBUTE = 1534 "overwrite"; 1535 public static final String PROPERTY_ATTRIBUTE = 1536 "property"; 1537 public static final String RENDERED_ATTRIBUTE = 1538 "rendered"; 1539 public static final String REQUIRED_ATTRIBUTE = 1540 "required"; 1541 public static final String TAG_ATTRIBUTE = 1542 "tag"; 1543 public static final String TARGET_KEY_ATTRIBUTE = 1544 "targetkey"; 1545 public static final String TARGET_TYPE_ATTRIBUTE = 1546 "targettype"; 1547 public static final String TYPE_ATTRIBUTE = 1548 "type"; 1549 public static final String VALUE_ATTRIBUTE = 1550 "value"; 1551 1552 public static final String AUTO_RENDERED = 1553 "auto"; 1554 public static final String EDITABLE = 1555 "editableContent"; 1556 public static final String EDIT_MENU = 1557 "editMenu"; 1558 1559 public static final String EDIT_AREA_TYPE = 1560 "editArea"; 1561 public static final String POPUP_MENU_TYPE = 1562 "popupMenu"; 1563 public static final String EDIT_AREA_TYPE_CLASS = 1564 "com.sun.enterprise.tools.jsfext.component.factory.basic.EditAreaFactory"; 1565 public static final String MARKUP_FACTORY_CLASS = 1566 "com.sun.enterprise.tools.jsfext.component.factory.basic.MarkupFactory"; 1567 public static final String POPUP_MENU_TYPE_CLASS = 1568 "com.sun.enterprise.tools.jsfext.component.factory.basic.PopupMenuFactory"; 1569 1570 1571 1574 1576 private URL _url = null; 1577 private EntityResolver _entityResolver = null; 1578 private ErrorHandler _errorHandler = null; 1579 private String _baseURI = null; 1580 1581 private Map _handlerDefs = new HashMap (); 1582 private int _markupCount = 1; 1583} 1584 | Popular Tags |