protected void generate()

in tobago-tool/tobago-tool-apt/src/main/java/org/apache/myfaces/tobago/apt/processor/FacesConfigGenerator.java [154:240]


  protected void generate() throws Exception {
    final Document document;
    final String content = IOUtils.toString(new FileInputStream(sourceFacesConfigFile), StandardCharsets.UTF_8);
    final SAXBuilder builder = new SAXBuilder();
    document = builder.build(new StringReader(content));

    // Normalise line endings. For some reason, JDOM replaces \r\n inside a comment with \n.
    normaliseLineEndings(document);

    // rewrite DOM as a string to find differences, since text outside the root element is not tracked

    final org.jdom2.Element rootElement = document.getRootElement();

    rootElement.setNamespace(Namespace.getNamespace("https://jakarta.ee/xml/ns/jakartaee"));
    final Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
//    rootElement.addNamespaceDeclaration(Namespace.getNamespace("xi", "http://www.w3.org/2001/XInclude"));
    rootElement.setAttribute(new Attribute("schemaLocation",
        "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_3_0.xsd", xsi));
    rootElement.setAttribute("version", "3.0");

    final Namespace namespace = rootElement.getNamespace();
    applyNamespace(rootElement, namespace);
    final List<org.jdom2.Element> components = rootElement.getChildren(COMPONENT, namespace);

    final List<org.jdom2.Element> newComponents = new ArrayList<>();
    final List<org.jdom2.Element> newRenderer = new ArrayList<>();
    final List<org.jdom2.Element> newConverters = new ArrayList<>();
    final List<org.jdom2.Element> newValidators = new ArrayList<>();

    for (final TypeElement element : getTypes()) {
      if (element.getAnnotation(UIComponentTag.class) != null) {
        addElement(element, newComponents, newRenderer, namespace);
      } else if (element.getAnnotation(Converter.class) != null) {
        addConverter(element, newConverters, namespace);
      } else if (element.getAnnotation(Validator.class) != null) {
        addValidator(element, newValidators, namespace);
      }
    }

    final List<org.jdom2.Element> elementsToAdd = new ArrayList<>();
    // sort out duplicates
    for (final org.jdom2.Element newElement : newComponents) {
      final boolean found = containsElement(components, newElement);
      if (!found) {
        elementsToAdd.add(newElement);
      }
    }
    if (!elementsToAdd.isEmpty()) {
      // if faces-config contains no component section add the components after factory or application
      final int lastIndex = getIndexAfter(rootElement, COMPONENT, FACTORY, APPLICATION);
      rootElement.addContent(lastIndex, elementsToAdd);
    }
    if (!newRenderer.isEmpty()) {
      org.jdom2.Element renderKit = getFirstElementByName(rootElement, RENDER_KIT);
      if (renderKit == null) {
        renderKit = new org.jdom2.Element(RENDER_KIT, namespace);
        final int last = getIndexAfter(rootElement, CONVERTER, COMPONENT, FACTORY, APPLICATION, BEHAVIOR);
        rootElement.addContent(last, renderKit);
      }
      final org.jdom2.Element renderKitId = new org.jdom2.Element(RENDER_KIT_ID, namespace);
      renderKitId.setText("tobago");
      renderKit.addContent(0, renderKitId);
      final org.jdom2.Element renderKitClass = new org.jdom2.Element(RENDER_KIT_CLASS, namespace);
      renderKitClass.setText("org.apache.myfaces.tobago.renderkit.TobagoRenderKit");
      renderKit.addContent(1, renderKitClass);
      renderKit.addContent(2, newRenderer);
    }
    if (!newConverters.isEmpty()) {
      final int last = getIndexAfter(rootElement, RENDER_KIT, CONVERTER, COMPONENT, FACTORY, APPLICATION, BEHAVIOR);
      rootElement.addContent(last, newConverters);
    }
    if (!newValidators.isEmpty()) {
      rootElement.addContent(newValidators);
    }
    final FileObject resource = processingEnv.getFiler().createResource(
        StandardLocation.SOURCE_OUTPUT, "", targetFacesConfigFile);
    info("Writing to file: " + resource.toUri());

    try (Writer writer = resource.openWriter()) {
      final StringWriter facesConfig = new StringWriter(1024);
      final Format format = Format.getPrettyFormat();
      format.setLineSeparator(SEPARATOR);
      final XMLOutputter out = new XMLOutputter(format);
      out.output(document, facesConfig);
      writer.append(facesConfig.toString());
    }
  }