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.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
42
43
44
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
55
56
57
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
73
74
75
76
77
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
92
93
94
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
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
112
113
114
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
129
130
131
132
133
134
135
136 public static Set<String> getModulesNamesWhichHaveXdoc() throws Exception {
137 final DocumentBuilderFactory factory = DocumentBuilderFactory
138 .newInstance();
139
140
141
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
160
161
162
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 }