1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44 public final class XdocUtil {
45
46 public static final String DIRECTORY_PATH = "src/xdocs";
47
48 private XdocUtil() {
49 }
50
51
52
53
54
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
69
70
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
85
86
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
101
102
103
104
105
106
107 public static Set<String> getModulesNamesWhichHaveXdoc() throws Exception {
108 final DocumentBuilderFactory factory = DocumentBuilderFactory
109 .newInstance();
110
111
112
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
131
132
133
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 }