private void writeClassBody(CodeWriter writer, FacesConfigBean bean) throws Exception {
    writer.indent();
    writer.fwrite(
        "private static Map<String,Attribute[]> ATTRIBUTE_LOOKUP=CollectionsUtils.<String,Attribute[]>map()\n");
    writer.indent();

    ComponentBean[] components = bean.getComponents();
    List<String> keys = new ArrayList<String>();
    for (int i = 0, len = components.length; i < len; i++) {
      ComponentBean comp = components[i];
      if (!comp.getComponentClass().contains("Html")) {
        continue;
      }
      String type = comp.getRendererType();
      if (type != null) {
        String family = comp.getBaseComponentType();
        type = type.substring(type.lastIndexOf('.') + 1);
        family = family.substring(family.lastIndexOf('.') + 1);
        String key = family + type;
        PropertyBean[] props = comp.getProperties();
        boolean attributeWritten = false;
        for (int ii = 0, llen = props.length; ii < llen; ii++) {
          PropertyBean aBean = props[ii];
          if (aBean.isPassThrough()) {
            if ((key.contains("Radio") || "SelectManyCheckbox".equals(key))
                && ("style".equals(aBean.getPropertyName())
                    || ("border".equals(aBean.getPropertyName())))) {
              continue;
            }
            if (attributeWritten) {
              writer.fwrite(",attr(\"");
            } else {
              keys.add(key);
              writer.fwrite(".add(\"");
              writer.write(key);
              writer.write("\",ar(\n");
              writer.indent();
              writer.fwrite("attr(\"");
            }
            writer.write(aBean.getPropertyName());
            writer.write("\"");
            if (aBean.getBehaviors() != null && !aBean.getBehaviors().isEmpty()) {
              for (String behavior : aBean.getBehaviors()) {
                writer.write(",\"");
                String behaviorName;
                if (0 == behavior.length()) {
                  behaviorName = aBean.getPropertyName();
                  // Strip leading "on" preffix.
                  if (behaviorName.length() > 2 && behaviorName.startsWith("on")) {
                    StringBuilder buffer =
                        new StringBuilder(behaviorName.substring(2, 3).toLowerCase());
                    buffer.append(behaviorName.substring(3));
                    behaviorName = buffer.toString();
                  }
                } else {
                  behaviorName = behavior;
                }
                writer.write(behaviorName);
                writer.write("\"");
              }
            }
            writer.write(")\n");
            attributeWritten = true;
          }
          if (key.contains("Button") && "onclick".equals(aBean.getPropertyName())) {
            // reset to the original state
            aBean.setPassThrough(false);
          }
        }
        if (attributeWritten) {
          writer.outdent();
          writer.fwrite("))\n");
        }
      }
    }
    writer.fwrite(".fix();\n");

    //        writer.fwrite("ATTRIBUTE_LOOKUP = Collections.unmodifiableMap(map);\n");
    //        writer.outdent();
    //        writer.fwrite("}\n\n");
    writer.outdent();
    writer.fwrite("public enum Key {\n");
    writer.indent();
    for (int i = 0, len = keys.size(); i < len; i++) {
      String key = keys.get(i);
      writer.fwrite(key.toUpperCase() + "(\"" + key + "\")");
      if (i == (len - 1)) {
        writer.write(";\n");
      } else {
        writer.write(",\n");
      }
    }
    writer.fwrite("private String key;\n");
    writer.fwrite("Key(String key) {\n");
    writer.indent();
    writer.fwrite("this.key = key;\n");
    writer.outdent();
    writer.fwrite("}\n");
    writer.fwrite("public String value() {\n");
    writer.indent();
    writer.fwrite("return this.key;\n");
    writer.outdent();
    writer.fwrite("}\n");
    writer.outdent();
    writer.fwrite("}\n");
    writer.write("\n\n");
    writer.fwrite("public static Attribute[] getAttributes(Key key) {\n");
    writer.indent();
    writer.fwrite("return ATTRIBUTE_LOOKUP.get(key.value());\n");
    writer.outdent();
    writer.fwrite("}\n");
  }
Example #2
0
  /** The tags for this TLD. */
  protected void writeTags() throws IOException {
    writer.writeComment("===================== HTML 4.0 basic tags ======================");

    Map<String, ComponentBean> componentsByComponentFamily =
        GeneratorUtil.getComponentFamilyComponentMap(configBean);
    Map<String, ArrayList<RendererBean>> renderersByComponentFamily =
        GeneratorUtil.getComponentFamilyRendererMap(
            configBean, propManager.getProperty(PropertyManager.RENDERKIT_ID));
    String targetPackage = propManager.getProperty(PropertyManager.TARGET_PACKAGE);

    for (Map.Entry entry : renderersByComponentFamily.entrySet()) {

      String componentFamily = (String) entry.getKey();
      List<RendererBean> renderers = (List<RendererBean>) entry.getValue();
      for (Iterator<RendererBean> rendererIter = renderers.iterator(); rendererIter.hasNext(); ) {

        RendererBean renderer = rendererIter.next();

        if (renderer.isIgnoreAll()) {
          continue;
        }
        String rendererType = renderer.getRendererType();
        writer.startElement("tag");

        DescriptionBean description = renderer.getDescription("");
        if (description != null) {
          String descriptionText = description.getDescription().trim();

          if (descriptionText != null) {
            writer.startElement("description");
            StringBuffer sb = new StringBuffer();
            sb.append("<![CDATA[");
            sb.append(descriptionText);
            sb.append("]]>\n");
            writer.writeText(sb.toString());
            writer.closeElement();
          }
        }

        String tagName = renderer.getTagName();

        if (tagName == null) {
          tagName =
              makeTldTagName(
                  GeneratorUtil.stripJavaxFacesPrefix(componentFamily),
                  GeneratorUtil.stripJavaxFacesPrefix(rendererType));
        }

        if (tagName == null) {
          throw new IllegalStateException("Could not determine tag name");
        }

        writer.startElement("name");
        writer.writeText(tagName);
        writer.closeElement();

        if (GeneratorUtil.makeTagClassName(
                GeneratorUtil.stripJavaxFacesPrefix(componentFamily),
                GeneratorUtil.stripJavaxFacesPrefix(rendererType))
            == null) {
          throw new IllegalStateException("Could not determine tag class name");
        }

        writer.startElement("tag-class");
        writer.writeText(
            targetPackage
                + '.'
                + GeneratorUtil.makeTagClassName(
                    GeneratorUtil.stripJavaxFacesPrefix(componentFamily),
                    GeneratorUtil.stripJavaxFacesPrefix(rendererType)));
        writer.closeElement();

        writer.startElement("body-content");
        writer.writeText(getBodyContent(tagName));
        writer.closeElement();

        // Generate tag attributes
        //

        // Component Properties first...
        //
        ComponentBean component = componentsByComponentFamily.get(componentFamily);

        PropertyBean[] properties = component.getProperties();
        PropertyBean property;

        for (int i = 0, len = properties.length; i < len; i++) {
          if (null == (property = properties[i])) {
            continue;
          }
          if (!property.isTagAttribute()) {
            continue;
          }

          writer.startElement("attribute");

          description = property.getDescription("");
          if (description != null) {
            String descriptionText = description.getDescription().trim();

            if (descriptionText != null) {
              writer.startElement("description");
              StringBuffer sb = new StringBuffer();
              sb.append("<![CDATA[");
              sb.append(descriptionText);
              sb.append("]]>\n");
              writer.writeText(sb.toString());
              writer.closeElement();
            }
          }

          String propertyName = property.getPropertyName();

          writer.startElement("name");
          writer.writeText(propertyName);
          writer.closeElement();

          writer.startElement("required");
          writer.writeText(
              property.isRequired() ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
          writer.closeElement();

          if (!"id".equals(propertyName)) {

            if (property.isMethodExpressionEnabled()) {
              writer.startElement("deferred-method");
              writer.startElement("method-signature");
              writer.writeText(property.getMethodSignature());
              writer.closeElement(2);
            } else if (property.isValueExpressionEnabled()) {
              // PENDING FIX ME
              String type = property.getPropertyClass();
              //                            String wrapperType = (String)
              //                                GeneratorUtil.convertToPrimitive(type);
              //                            if (wrapperType != null) {
              //                                type = wrapperType;
              //                            }
              writer.startElement("deferred-value");
              writer.startElement("type");
              writer.writeText(type);
              writer.closeElement(2);
            } else {
              writer.startElement("rtexprvalue");
              writer.writeText(getRtexprvalue(tagName, propertyName));
              writer.closeElement();
            }
          } else {
            writer.startElement("rtexprvalue");
            writer.writeText(getRtexprvalue(tagName, propertyName));
            writer.closeElement();
          }

          writer.closeElement(); // closes attribute element above
        } // END property FOR loop

        // Renderer Attributes Next...
        //
        AttributeBean[] attributes = renderer.getAttributes();
        AttributeBean attribute;
        for (int i = 0, len = attributes.length; i < len; i++) {
          if (null == (attribute = attributes[i])) {
            continue;
          }
          if (!attribute.isTagAttribute()) {
            continue;
          }
          if (attributeShouldBeExcluded(renderer, attribute.getAttributeName())) {
            continue;
          }

          writer.startElement("attribute");

          description = attribute.getDescription("");
          if (description != null) {
            String descriptionText = description.getDescription().trim();

            if (descriptionText != null) {
              writer.startElement("description");
              StringBuffer sb = new StringBuffer();
              sb.append("<![CDATA[");
              sb.append(descriptionText);
              sb.append("]]>\n");
              writer.writeText(sb.toString());
              writer.closeElement();
            }
          }

          String attributeName = attribute.getAttributeName();

          writer.startElement("name");
          writer.writeText(attributeName);
          writer.closeElement();

          writer.startElement("required");
          writer.writeText(
              attribute.isRequired() ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
          writer.closeElement();

          if (!"id".equals(attributeName)) {
            // PENDING FIX ME
            String type = attribute.getAttributeClass();
            // String wrapperType = (String)
            //  GeneratorUtil.convertToPrimitive(type);
            // if (wrapperType != null) {
            //    type = wrapperType;
            // }
            writer.startElement("deferred-value");
            writer.startElement("type");
            writer.writeText(type);
            writer.closeElement(2);

          } else {
            writer.startElement("rtexprvalue");
            writer.writeText(getRtexprvalue(tagName, attributeName));
            writer.closeElement();
          }

          writer.closeElement(); // closes attribute element above
        } // END attribute FOR loop

        // SPECIAL: "Binding" needs to exist on every tag..
        writer.startElement("attribute");

        writer.startElement("description");
        writer.writeText(
            "The ValueExpression linking this component to a property in a backing bean");
        writer.closeElement();

        writer.startElement("name");
        writer.writeText("binding");
        writer.closeElement();

        writer.startElement("required");
        writer.writeText("false");
        writer.closeElement();

        writer.startElement("deferred-value");
        writer.startElement("type");
        writer.writeText("javax.faces.component.UIComponent");
        writer.closeElement(2);

        // close the most recent attribute, and tag
        // elements
        writer.closeElement(2);
      }
    }

    // Include any other tags defined in the optional tag definition file.
    // These might be tags that were not picked up because they have no renderer
    // - for example "column".
    String tagDef = loadOptionalTags();
    if (tagDef != null) {
      writer.write(tagDef);
    }
  } // END writeTags