private void _generateComponent()

in maven-faces-plugin/src/main/java/org/apache/myfaces/trinidadbuild/plugin/faces/GenerateComponentsMojo.java [119:316]


  private void _generateComponent(
    ComponentBean component) throws MojoExecutionException
  {
    ComponentGenerator generator;

    String fullClassName = component.getComponentClass();

    // TODO: This must be changed in the future...
    JsfVersion version = JsfVersion.getVersion(jsfVersion);
    boolean isVersionGreaterThan11 = version != JsfVersion.JSF_1_1;

    if (component.isTrinidadComponent())
    {
      generator = new TrinidadComponentGenerator(getLog(), isVersionGreaterThan11);
    }
    else
    {
      generator = new MyFacesComponentGenerator(getLog(), isVersionGreaterThan11);
    }

    try
    {
      getLog().debug("Generating " + fullClassName +
                     ", with generator: " + generator.getClass().getName());

      StringWriter sw = new StringWriter();
      PrettyWriter out = new PrettyWriter(sw);

      String className = Util.getClassFromFullClass(fullClassName);
      String componentFamily = component.findComponentFamily();

      if (componentFamily == null)
      {
        getLog().warn("Missing <component-family> for \"" +
                       fullClassName + "\", generation of this Component is skipped");
      }
      else
      {
        String packageName = Util.getPackageFromFullClass(fullClassName);
        String fullSuperclassName = component.findComponentSuperclass();
        String superclassName = Util.getClassFromFullClass(fullSuperclassName);

        // make class name fully qualified in case of collision
        if (superclassName.equals(className))
          superclassName = fullSuperclassName;

        // TODO: remove this bogosity
        if (superclassName.equals("UIXMenuHierarchy") ||
            superclassName.equals("UIXTable") ||
            superclassName.equals("UIXHierarchy") ||
            superclassName.equals("UIXMenuTree") ||
            className.equals("CoreTree"))
        {
          superclassName = fullSuperclassName;
        }


        String componentType = component.getComponentType();

        // Handle both the case where we have the old-style FooTemplate.java that will be
        // flattened into a single class Foo and the new style Foo.java subclass of
        // PartialFoo, in which case we generate the package-private PartialFoo class.

        // Use template file if it exists
        String templatePath = Util.convertClassToSourcePath(fullClassName, "Template.java");
        File templateFile = new File(templateSourceDirectory, templatePath);
        boolean hasTemplate = templateFile.exists();

        String subclassPath = Util.convertClassToSourcePath(fullClassName, ".java");
        File subclassFile = new File(templateSourceDirectory, subclassPath);
        boolean hasSubclass = subclassFile.exists();

        // we should never have both the tempalte and the subclass
        if (hasTemplate && hasSubclass)
          throw new IllegalStateException("Both old style " + templatePath + " and new style " +
                                          subclassPath + " component templates exist!");

        SourceTemplate template = null;

        String outClassName;
        String outFullClassName;
        int    defaultConstructorModifier;

        if (hasSubclass)
        {
          getLog().debug("Using subclass " + subclassPath);

          outClassName     = "Partial" + className;
          outFullClassName = Util.getPackageFromFullClass(fullClassName) + '.' + outClassName;

          defaultConstructorModifier = 0; // package pivate

          // copy the file template to the destination directory
          File destFile = new File(generatedSourceDirectory, subclassPath);

          Util.copyFile(subclassFile, destFile);
          destFile.setReadOnly();
        }
        else
        {
          outClassName               = className;
          outFullClassName           = fullClassName;
          defaultConstructorModifier = Modifier.PUBLIC;

          if (hasTemplate)
          {
            getLog().debug("Using template " + templatePath);
            template = new SourceTemplate(templateFile);
            template.substitute(className + "Template", className);
            template.readPreface();
          }
        }

        String sourcePath = Util.convertClassToSourcePath(outFullClassName, ".java");
        File targetFile = new File(generatedSourceDirectory, sourcePath);

        // header/copyright
        writePreamble(out);

        // package
        out.println("package " + packageName + ";");
        out.println();

        // imports
        generator.writeImports(out, template, packageName,
                      fullSuperclassName, superclassName,
                      component);

        // class
        generator.writeClassBegin(out, outClassName, superclassName, component, template, hasSubclass);

        // static final constants
        generator.writePropertyValueConstants(out, component);
        generator.writePropertyConstants(out, superclassName, component);
        generator.writeFacetConstants(out, component);
        generator.writeGenericConstants(out, componentFamily, componentType);
        if (component.isClientBehaviorHolder())
        {
          generator.writeClientBehaviorConstants(out, component);
        }

        // public constructors and methods
        generator.writeConstructor(out, component, outClassName, defaultConstructorModifier);

        // insert template code
        if (template != null)
        {
          template.writeContent(out);
          template.close();
        }

        generator.writeFacetMethods(out, component);

        if (template == null)
        {
          generator.writePropertyMethods(out, component);
        }
        else
        {
          generator.writePropertyMethods(out, component, template.getIgnoreMethods());
        }

        if (!suppressListenerMethods)
          generator.writeListenerMethods(out, component);

        if (component.isClientBehaviorHolder())
          generator.writeClientBehaviorMethods(out, component);

        generator.writeStateManagementMethods(out, component);

        generator.writeGetFamily(out);

        // protected constructors and methods
        // TODO: reverse this order, to make protected constructor go first
        //       for now we want consistency with previous code generation
        generator.writeOther(out, component, outClassName);

        generator.writeClassEnd(out);

        out.close();

        // delay write in case of error
        // timestamp should not be updated when an error occurs
        // delete target file first, because it is readonly
        targetFile.getParentFile().mkdirs();
        targetFile.delete();
        FileWriter fw = new FileWriter(targetFile);
        StringBuffer buf = sw.getBuffer();
        fw.write(buf.toString());
        fw.close();
        targetFile.setReadOnly();
      }
    }
    catch (IOException e)
    {
      getLog().error("Error generating " + fullClassName, e);
    }
  }