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 if (!entry.getParent().toString().matches("src[\\\\/]site[\\\\/]xdocs")
101 && fileName.endsWith(".xml")) {
102 xdocs.add(entry);
103 }
104 }
105 return xdocs;
106 }
107
108
109
110
111
112
113
114 public static Set<Path> getXdocsStyleFilePaths(Set<Path> files) {
115 final Set<Path> xdocs = new HashSet<>();
116 for (Path entry : files) {
117 final String fileName = entry.getFileName().toString();
118 if (fileName.endsWith("_style.xml")) {
119 xdocs.add(entry);
120 }
121 }
122 return xdocs;
123 }
124
125
126
127
128
129
130
131
132
133
134 public static Set<String> getModulesNamesWhichHaveXdoc() throws Exception {
135 final DocumentBuilderFactory factory = DocumentBuilderFactory
136 .newInstance();
137
138
139
140 factory.setNamespaceAware(false);
141 factory.setValidating(false);
142 factory.setFeature("http://xml.org/sax/features/namespaces", false);
143 factory.setFeature("http://xml.org/sax/features/validation", false);
144 factory.setFeature(
145 "http://apache.org/xml/features/nonvalidating/load-dtd-grammar",
146 false);
147 factory.setFeature(
148 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
149 false);
150
151 final Set<String> modulesNamesWhichHaveXdoc = new HashSet<>();
152
153 for (Path path : getXdocsConfigFilePaths(getXdocsFilePaths())) {
154 final DocumentBuilder builder = factory.newDocumentBuilder();
155 final Document document = builder.parse(path.toFile());
156
157
158
159
160
161 document.getDocumentElement().normalize();
162
163 final NodeList nodeList = document.getElementsByTagName("section");
164
165 for (int i = 0; i < nodeList.getLength(); i++) {
166 final Node currentNode = nodeList.item(i);
167 if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
168 final Element module = (Element) currentNode;
169 final String moduleName = module.getAttribute("name");
170 if (!"Content".equals(moduleName)
171 && !"Overview".equals(moduleName)) {
172 modulesNamesWhichHaveXdoc.add(moduleName);
173 }
174 }
175 }
176 }
177 return modulesNamesWhichHaveXdoc;
178 }
179
180 }