1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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.DirectoryStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.HashSet;
28  import java.util.Set;
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   * @noinspection ClassOnlyUsedInOnePackage
43   */
44  public final class XdocUtil {
45  
46      public static final String DIRECTORY_PATH = "src/xdocs";
47  
48      private XdocUtil() {
49      }
50  
51      /**
52       * Gets xdocs file paths.
53       * @return a list of xdocs file paths.
54       * @throws IOException if an I/O error occurs.
55       */
56      public static Set<Path> getXdocsFilePaths() throws IOException {
57          final Path directory = Paths.get(DIRECTORY_PATH);
58          try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, "*.xml")) {
59              final Set<Path> xdocs = new HashSet<>();
60              for (Path entry : stream) {
61                  xdocs.add(entry);
62              }
63              return xdocs;
64          }
65      }
66  
67      /**
68       * Gets xdocs documentation file paths.
69       * @param files list of all xdoc files
70       * @return a list of xdocs config file paths.
71       */
72      public static Set<Path> getXdocsConfigFilePaths(Set<Path> files) {
73          final Set<Path> xdocs = new HashSet<>();
74          for (Path entry : files) {
75              final String fileName = entry.getFileName().toString();
76              if (fileName.startsWith("config_")) {
77                  xdocs.add(entry);
78              }
79          }
80          return xdocs;
81      }
82  
83      /**
84       * Gets xdocs style file paths.
85       * @param files list of all xdoc files
86       * @return a list of xdocs style file paths.
87       */
88      public static Set<Path> getXdocsStyleFilePaths(Set<Path> files) {
89          final Set<Path> xdocs = new HashSet<>();
90          for (Path entry : files) {
91              final String fileName = entry.getFileName().toString();
92              if (fileName.endsWith("_style.xml")) {
93                  xdocs.add(entry);
94              }
95          }
96          return xdocs;
97      }
98  
99      /**
100      * Gets names of checkstyle's modules which are documented in xdocs.
101      * @return a set of checkstyle's modules which have xdoc documentation.
102      * @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies
103      *              the configuration requested.
104      * @throws IOException if any IO errors occur.
105      * @throws SAXException if any parse errors occur.
106      */
107     public static Set<String> getModulesNamesWhichHaveXdoc() throws Exception {
108         final DocumentBuilderFactory factory = DocumentBuilderFactory
109                 .newInstance();
110 
111         // Validations of XML file make parsing too slow, that is why we disable
112         // all validations.
113         factory.setNamespaceAware(false);
114         factory.setValidating(false);
115         factory.setFeature("http://xml.org/sax/features/namespaces", false);
116         factory.setFeature("http://xml.org/sax/features/validation", false);
117         factory.setFeature(
118                 "http://apache.org/xml/features/nonvalidating/load-dtd-grammar",
119                 false);
120         factory.setFeature(
121                 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
122                 false);
123 
124         final Set<String> modulesNamesWhichHaveXdoc = new HashSet<>();
125 
126         for (Path path : getXdocsConfigFilePaths(getXdocsFilePaths())) {
127             final DocumentBuilder builder = factory.newDocumentBuilder();
128             final Document document = builder.parse(path.toFile());
129 
130             // optional, but recommended
131             // FYI:
132             // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-
133             // java-how-does-it-work
134             document.getDocumentElement().normalize();
135 
136             final NodeList nodeList = document.getElementsByTagName("section");
137 
138             for (int i = 0; i < nodeList.getLength(); i++) {
139                 final Node currentNode = nodeList.item(i);
140                 if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
141                     final Element module = (Element) currentNode;
142                     final String moduleName = module.getAttribute("name");
143                     if (!"Content".equals(moduleName)
144                             && !"Overview".equals(moduleName)) {
145                         modulesNamesWhichHaveXdoc.add(moduleName);
146                     }
147                 }
148             }
149         }
150         return modulesNamesWhichHaveXdoc;
151     }
152 
153 }