View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.internal.utils;
21  
22  import java.io.File;
23  import java.io.Reader;
24  import java.nio.charset.StandardCharsets;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.nio.file.StandardCopyOption;
28  import java.util.Set;
29  
30  import org.apache.maven.doxia.parser.Parser;
31  import org.apache.maven.doxia.sink.Sink;
32  import org.apache.maven.doxia.sink.SinkFactory;
33  import org.codehaus.plexus.DefaultPlexusContainer;
34  import org.codehaus.plexus.PlexusContainer;
35  import org.codehaus.plexus.util.ReaderFactory;
36  
37  import com.puppycrawl.tools.checkstyle.site.XdocsTemplateParser;
38  import com.puppycrawl.tools.checkstyle.site.XdocsTemplateSinkFactory;
39  
40  /**
41   * Generates xdoc content from xdoc templates.
42   * This module will be removed once
43   * <a href="https://github.com/checkstyle/checkstyle/issues/13426">#13426</a> is resolved.
44   */
45  public final class XdocGenerator {
46      private static final String XDOCS_TEMPLATE_HINT = "xdocs-template";
47  
48      private XdocGenerator() {
49      }
50  
51      public static void generateXdocContent() throws Exception {
52          final PlexusContainer plexus = new DefaultPlexusContainer();
53          final Set<Path> templatesFilePaths = XdocUtil.getXdocsTemplatesFilePaths();
54  
55          for (Path path : templatesFilePaths) {
56              final String pathToFile = path.toString();
57              final File inputFile = new File(pathToFile);
58              final File tempFile = File.createTempFile(pathToFile.replace(".template", ""), "");
59              tempFile.deleteOnExit();
60              final XdocsTemplateSinkFactory sinkFactory = (XdocsTemplateSinkFactory)
61                      plexus.lookup(SinkFactory.ROLE, XDOCS_TEMPLATE_HINT);
62              final Sink sink = sinkFactory.createSink(tempFile.getParentFile(),
63                      tempFile.getName(), String.valueOf(StandardCharsets.UTF_8));
64              final XdocsTemplateParser parser = (XdocsTemplateParser)
65                      plexus.lookup(Parser.ROLE, XDOCS_TEMPLATE_HINT);
66              try (Reader reader = ReaderFactory.newReader(inputFile,
67                      String.valueOf(StandardCharsets.UTF_8))) {
68                  parser.parse(reader, sink);
69              }
70              finally {
71                  sink.close();
72              }
73              final File outputFile = new File(pathToFile.replace(".template", ""));
74              final StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING;
75              Files.copy(tempFile.toPath(), outputFile.toPath(), copyOption);
76          }
77      }
78  }