/**
   * Writes out the facet info for a faceted-project based on the packaging.
   *
   * @param writer
   * @param packaging
   */
  private void writeModuleTypeFacetCore(XMLWriter writer, String packaging) {
    writer.startElement(ELT_FACETED_PROJECT);
    if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)) // $NON-NLS-1$
    {
      writeFacetFixedElement(writer, FACET_JST_JAVA); // fixed
      writeFacetFixedElement(writer, FACET_JST_WEB); // fixed
      String servletVersion;
      if (config.getJeeVersion() != null) {
        servletVersion =
            JeeUtils.getJeeDescriptorFromJeeVersion(config.getJeeVersion()).getServletVersion();
      } else {
        servletVersion = JeeUtils.resolveServletVersion(config.getProject());
      }
      writeFacetInstalledElement(writer, FACET_JST_WEB, servletVersion); // installed
      writeFacetInstalledElement(
          writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion(config.getProject())); // installed
    } else if (Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase(packaging)) // $NON-NLS-1$
    {
      writeFacetFixedElement(writer, FACET_JST_JAVA); // fixed
      writeFacetFixedElement(writer, FACET_JST_EJB); // fixed

      String ejbVersion;
      if (config.getJeeVersion() != null) {
        ejbVersion =
            JeeUtils.getJeeDescriptorFromJeeVersion(config.getJeeVersion()).getEjbVersion();
      } else {
        ejbVersion = JeeUtils.resolveEjbVersion(config.getProject());
      }
      writeFacetInstalledElement(writer, FACET_JST_EJB, ejbVersion); // installed
      writeFacetInstalledElement(
          writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion(config.getProject())); // installed
    } else if (Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)) // $NON-NLS-1$
    {
      if (this.config.getWorkspaceConfiguration().getWebsphereVersion() != null) {
        writer.startElement("runtime");
        writer.addAttribute(
            "name", config.getWorkspaceConfiguration().getDefaultDeployServerName());
        writer.endElement(); // runtime

        writeFacetInstalledElement(
            writer,
            FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR,
            this.config.getWorkspaceConfiguration().getWebsphereVersion()); // installed
        writeFacetInstalledElement(
            writer,
            FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR,
            this.config.getWorkspaceConfiguration().getWebsphereVersion()); // installed
      }
      writeFacetFixedElement(writer, FACET_JST_EAR); // fixed
      String jeeVersion;
      if (config.getJeeVersion() != null) {
        jeeVersion =
            JeeUtils.getJeeDescriptorFromJeeVersion(config.getJeeVersion()).getJeeVersion();
      } else {
        jeeVersion = JeeUtils.resolveJeeVersion(config.getProject());
      }
      writeFacetInstalledElement(writer, FACET_JST_EAR, jeeVersion); // installed
    } else if (Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase(packaging)) // $NON-NLS-1$
    {
      writeFacetFixedElement(writer, FACET_JST_JAVA); // fixed
      writeFacetFixedElement(writer, FACET_JST_UTILITY); // fixed
      writeFacetInstalledElement(writer, FACET_JST_UTILITY, "1.0"); // $NON-NLS-1$
      writeFacetInstalledElement(
          writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion(config.getProject())); // installed
      // installed
    }

    writeAdditionalProjectFacets(writer);

    writer.endElement(); // faceted-project
  }
  /**
   * Writer entry point
   *
   * @throws MojoExecutionException
   */
  public void write() throws MojoExecutionException {
    String packaging = config.getProject().getPackaging();

    if (!Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)
        && !Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)
        && !Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase(packaging)) {
      return;
    }

    FileWriter w;

    try {
      w =
          new FileWriter(
              new File(config.getEclipseProjectDirectory(), MYECLIPSE_MYMETADATA_FILENAME));
    } catch (IOException ex) {
      throw new MojoExecutionException(
          Messages.getString("EclipsePlugin.erroropeningfile"), ex); // $NON-NLS-1$
    }

    XMLWriter writer = new PrettyPrintXMLWriter(w, "UTF-8", null);

    writer.startElement(MYECLIPSE_METADATA_PROJECT);
    writer.addAttribute(MYECLIPSE_METADATA_PROJECT_TYPE, getMyEclipseProjectType(packaging));
    writer.addAttribute(MYECLIPSE_METADATA_PROJECT_NAME, config.getEclipseProjectName());
    writer.addAttribute(MYECLIPSE_METADATA_PROJECT_ID, config.getEclipseProjectName());

    if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)) {
      // Find web application context root from maven-war-plugin configuration.
      // ArtifactId is used as the default value
      String warContextRoot =
          IdeUtils.getPluginSetting(
              config.getProject(),
              JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
              "warContextRoot",
              //$NON-NLS-1$
              "/" + config.getProject().getArtifactId());

      writer.addAttribute(MYECLIPSE_METADATA_PROJECT_CONTEXT_ROOT, warContextRoot);

      writer.addAttribute(MYECLIPSE_METADATA_PROJECT_J2EE_SPEC, getJeeVersion());
      // TODO : use maven final name
      writer.addAttribute(
          MYECLIPSE_METADATA_PROJECT_ARCHIVE, config.getEclipseProjectName() + ".war");
    }

    if (Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase(packaging)) {
      // TODO : use maven final name
      writer.addAttribute(
          MYECLIPSE_METADATA_PROJECT_ARCHIVE, config.getEclipseProjectName() + ".ear");
    }

    writer.startElement(MYECLIPSE_METADATA_PROJECT_ATTRIBUTES);
    if (Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase(packaging)) {
      writer.startElement(MYECLIPSE_METADATA_PROJECT_ATTRIBUTE);
      writer.addAttribute("name", "webrootdir");
      // TODO : retrieve this from project configuration
      writer.addAttribute("value", "src/main/webapp");
      writer.endElement();
    }
    // Close <attributes>
    writer.endElement();

    // Close <project-module>
    writer.endElement();

    IOUtil.close(w);
  }