View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.HashSet;
26  import java.util.Set;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  import javax.xml.parsers.ParserConfigurationException;
33  
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  import org.w3c.dom.Node;
37  import org.w3c.dom.NodeList;
38  import org.xml.sax.SAXException;
39  
40  /**
41   * XdocUtil.
42   *
43   * @noinspection ClassOnlyUsedInOnePackage
44   * @noinspectionreason ClassOnlyUsedInOnePackage - class is internal tool, and only used in testing
45   */
46  public final class XdocUtil {
47  
48      public static final String DIRECTORY_PATH = "src/site/xdoc";
49  
50      private XdocUtil() {
51      }
52  
53      /**
54       * Gets xdocs file paths.
55       *
56       * @return a set of xdocs file paths.
57       * @throws IOException if an I/O error occurs.
58       */
59      public static Set<Path> getXdocsFilePaths() throws IOException {
60          final Path directory = Path.of(DIRECTORY_PATH);
61          try (Stream<Path> stream = Files.find(directory, Integer.MAX_VALUE,
62                  (path, attr) -> {
63                      return attr.isRegularFile()
64                              && (path.toString().endsWith(".xml")
65                              || path.toString().endsWith(".xml.vm"));
66                  })) {
67              return stream.collect(Collectors.toUnmodifiableSet());
68          }
69      }
70  
71      /**
72       * Gets xdocs template file paths. These are files ending with .xml.template.
73       * This module will be removed once
74       * <a href="https://github.com/checkstyle/checkstyle/issues/13426">#13426</a> is resolved.
75       *
76       * @return a set of xdocs template file paths.
77       * @throws IOException if an I/O error occurs.
78       */
79      public static Set<Path> getXdocsTemplatesFilePaths() throws IOException {
80          final Path directory = Path.of(DIRECTORY_PATH);
81          try (Stream<Path> stream = Files.find(directory, Integer.MAX_VALUE,
82                  (path, attr) -> {
83                      return attr.isRegularFile()
84                              && path.toString().endsWith(".xml.template");
85                  })) {
86              return stream.collect(Collectors.toUnmodifiableSet());
87          }
88      }
89  
90      /**
91       * Gets xdocs documentation file paths.
92       *
93       * @param files set of all xdoc files
94       * @return a set of xdocs config file paths.
95       */
96      public static Set<Path> getXdocsConfigFilePaths(Set<Path> files) {
97          final Set<Path> xdocs = new HashSet<>();
98          for (Path entry : files) {
99              final String fileName = entry.getFileName().toString();
100             // until https://github.com/checkstyle/checkstyle/issues/13132
101             if (fileName.startsWith("config_")
102                     || !entry.getParent().toString().matches("src[\\\\/]site[\\\\/]xdocs")
103                     && fileName.endsWith(".xml")) {
104                 xdocs.add(entry);
105             }
106         }
107         return xdocs;
108     }
109 
110     /**
111      * Gets xdocs style file paths.
112      *
113      * @param files set of all xdoc files
114      * @return a set of xdocs style file paths.
115      */
116     public static Set<Path> getXdocsStyleFilePaths(Set<Path> files) {
117         final Set<Path> xdocs = new HashSet<>();
118         for (Path entry : files) {
119             final String fileName = entry.getFileName().toString();
120             if (fileName.endsWith("_style.xml")) {
121                 xdocs.add(entry);
122             }
123         }
124         return xdocs;
125     }
126 
127     /**
128      * Gets names of checkstyle's modules which are documented in xdocs.
129      *
130      * @return a set of checkstyle's modules which have xdoc documentation.
131      * @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies
132      *              the configuration requested.
133      * @throws IOException if any IO errors occur.
134      * @throws SAXException if any parse errors occur.
135      */
136     public static Set<String> getModulesNamesWhichHaveXdoc() throws Exception {
137         final DocumentBuilderFactory factory = DocumentBuilderFactory
138                 .newInstance();
139 
140         // Validations of XML file make parsing too slow, that is why we disable
141         // all validations.
142         factory.setNamespaceAware(false);
143         factory.setValidating(false);
144         factory.setFeature("http://xml.org/sax/features/namespaces", false);
145         factory.setFeature("http://xml.org/sax/features/validation", false);
146         factory.setFeature(
147                 "http://apache.org/xml/features/nonvalidating/load-dtd-grammar",
148                 false);
149         factory.setFeature(
150                 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
151                 false);
152 
153         final Set<String> modulesNamesWhichHaveXdoc = new HashSet<>();
154 
155         for (Path path : getXdocsConfigFilePaths(getXdocsFilePaths())) {
156             final DocumentBuilder builder = factory.newDocumentBuilder();
157             final Document document = builder.parse(path.toFile());
158 
159             // optional, but recommended
160             // FYI:
161             // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-
162             // java-how-does-it-work
163             document.getDocumentElement().normalize();
164 
165             final NodeList nodeList = document.getElementsByTagName("section");
166 
167             for (int i = 0; i < nodeList.getLength(); i++) {
168                 final Node currentNode = nodeList.item(i);
169                 if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
170                     final Element module = (Element) currentNode;
171                     final String moduleName = module.getAttribute("name");
172                     if (!"Content".equals(moduleName)
173                             && !"Overview".equals(moduleName)) {
174                         modulesNamesWhichHaveXdoc.add(moduleName);
175                     }
176                 }
177             }
178         }
179         return modulesNamesWhichHaveXdoc;
180     }
181 
182 }