Debian Codegraph @

native/jpype_module/src/main/java/org/jpype/javadoc/JavadocExtractor.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 **************************************************************************** */
16 package org.jpype.javadoc;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.xml.xpath.XPath;
25 import javax.xml.xpath.XPathConstants;
26 import javax.xml.xpath.XPathExpressionException;
27 import javax.xml.xpath.XPathFactory;
28 import org.jpype.html.Html;
29 import org.jpype.html.Parser;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.DocumentFragment;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34
35 public class JavadocExtractor
36 {
37
38 static final JavadocTransformer transformer = new JavadocTransformer();
39 static public boolean transform = true;
40 static public boolean render = true;
41 static public boolean failures = false;
42
43 /**
44 * Search the classpath for documentation.
45 *
46 * @param cls
47 * @return
48 */
49 public static Javadoc getDocumentation(Class cls)
50 {
51 try
52 {
53 try (InputStream is = getDocumentationAsStream(cls))
54 {
55 if (is != null)
56 {
57 Parser<Document> parser = Html.newParser();
58 return extractDocument(cls, parser.parse(is));
59 }
60 }
61 } catch (Exception ex)
62 {
63 System.err.println("Failed to extract javadoc for " + cls + ", caught " + ex);
64 if (failures)
65 throw new RuntimeException(ex);
66 }
67 return null;
68 }
69
70 public static InputStream getDocumentationAsStream(Class cls)
71 {
72 InputStream is = null;
73 String name = cls.getName().replace('.', '/') + ".html";
74 ClassLoader cl = ClassLoader.getSystemClassLoader();
75
76 // Search the regular class path.
77 is = cl.getResourceAsStream(name);
78 if (is != null)
79 return is;
80
81 // Search for api documents
82 String name1 = "docs/api/" + name;
83 is = cl.getResourceAsStream(name1);
84 if (is != null)
85 return is;
86
87 // If we are dealing with Java 9+, the doc tree is different
88 try
89 {
90 Method meth = Class.class.getMethod("getModule");
91 String module = meth.invoke(cls).toString().substring(7);
92 String name2 = "docs/api/" + module + "/" + name;
93 is = cl.getResourceAsStream(name2);
94 if (is != null)
95 return is;
96 } catch (NoSuchMethodException | SecurityException | IllegalAccessException
97 | IllegalArgumentException | InvocationTargetException ex)
98 {
99 // do nothing if we are not JDK 9+
100 }
101 return null;
102 }
103
104 /**
105 * Extract the documentation from the dom.
106 *
107 * @param cls is the class being processed.
108 * @param doc is the DOM holding the javadoc.
109 * @return
110 */
111 public static Javadoc extractDocument(Class cls, Document doc)
112 {
113 JavadocRenderer renderer = new JavadocRenderer();
114 try
115 {
116 Javadoc documentation = new Javadoc();
117 XPath xPath = XPathFactory.newInstance().newXPath();
118 // Javadoc 8-13
119 Node n = (Node) xPath.compile("//div[@class='description']/ul/li").evaluate(doc, XPathConstants.NODE);
120 if (n == null)
121 { // Javadoc 14+
122 n = (Node) xPath.compile("//section[@class='description']").evaluate(doc, XPathConstants.NODE);
123 }
124 if (n == null)
125 { // Javadoc 17+
126 n = (Node) xPath.compile("//section[@class='class-description']").evaluate(doc, XPathConstants.NODE);
127 }
128 Node description = toFragment(n);
129 if (description != null)
130 {
131 documentation.descriptionNode = description;
132 if (transform)
133 transformer.transformDescription(cls, description);
134 if (render)
135 documentation.description = renderer.render(description);
136 }
137
138 Node ctorRoot = ((Node) xPath.compile("//li/a[@name='constructor.detail' or @id='constructor.detail']") // Javadoc before 17
139 .evaluate(doc, XPathConstants.NODE));
140 if (ctorRoot == null)
141 { // Javadoc 17+
142 ctorRoot = ((Node) xPath.compile("//section[@class='constructor-details']/ul")
143 .evaluate(doc, XPathConstants.NODE));
144 }
145 if (ctorRoot != null)
146 {
147 List<Node> set = convertNodes((NodeList) xPath.compile("./li/section") // Javadoc 17+
148 .evaluate(ctorRoot, XPathConstants.NODESET));
149 if (set.isEmpty())
150 { // Javadoc before 17
151 set = convertNodes((NodeList) xPath.compile("./ul/li")
152 .evaluate(ctorRoot.getParentNode(), XPathConstants.NODESET));
153 }
154 documentation.ctorsNode = set;
155 StringBuilder sb = new StringBuilder();
156 for (Node ctor : set)
157 {
158 if (transform)
159 transformer.transformMember(cls, ctor);
160 if (render)
161 sb.append(renderer.render(ctor));
162 }
163 documentation.ctors = sb.toString();
164 }
165
166 Node methodRoot = ((Node) xPath.compile("//li/a[@name='method.detail' or @id='method.detail']") // Javadoc before 17
167 .evaluate(doc, XPathConstants.NODE));
168 if (methodRoot == null)
169 { // Javadoc 17+
170 methodRoot = ((Node) xPath.compile("//section[@class='method-details']/ul")
171 .evaluate(doc, XPathConstants.NODE));
172 }
173 if (methodRoot != null)
174 {
175 List<Node> set = convertNodes((NodeList) xPath.compile("./li/section") // Javadoc 17+
176 .evaluate(methodRoot, XPathConstants.NODESET));
177 if (set.isEmpty())
178 { // Javadoc before 17
179 set = convertNodes((NodeList) xPath.compile("./ul/li")
180 .evaluate(methodRoot.getParentNode(), XPathConstants.NODESET));
181 }
182 documentation.methodNodes = set;
183 for (Node method : set)
184 {
185 if (transform)
186 transformer.transformMember(cls, method);
187 if (render)
188 {
189 String str = renderer.render(method);
190 String name = renderer.memberName;
191 if (documentation.methods.containsKey(name))
192 {
193 String old = documentation.methods.get(name);
194 str = old + str;
195 }
196 documentation.methods.put(name, str);
197 }
198 }
199 }
200
201 // Node inner = (Node) xPath.compile("//li/a[@name='nested_class_summary']").evaluate(doc, XPathConstants.NODE);
202 // if (inner != nullList)
203 // {
204 // NodeList set = (NodeList) xPath.compile("./ul/li").evaluate(inner.getParentNode(), XPathConstants.NODESET);
205 // documentation.innerNode = convertNodes(set);
206 // }
207 Node fieldRoot = ((Node) xPath.compile("//li/a[@name='field.detail' or @id='field.detail']") // Javadoc before 17
208 .evaluate(doc, XPathConstants.NODE));
209 if (fieldRoot == null)
210 { // Javadoc 17+
211 fieldRoot = ((Node) xPath.compile("//section[@class='field-details']/ul")
212 .evaluate(doc, XPathConstants.NODE));
213 }
214 if (fieldRoot != null)
215 {
216 List<Node> set = convertNodes((NodeList) xPath.compile("./li/section") // Javadoc 17+
217 .evaluate(fieldRoot, XPathConstants.NODESET));
218 if (set.isEmpty())
219 { // Javadoc before 17
220 set = convertNodes((NodeList) xPath.compile("./ul/li")
221 .evaluate(fieldRoot.getParentNode(), XPathConstants.NODESET));
222 }
223 documentation.fieldNodes = set;
224 for (Node field : set)
225 {
226 if (transform)
227 transformer.transformMember(cls, field);
228 if (render)
229 {
230 String str = renderer.render(field);
231 String name = renderer.memberName;
232 documentation.fields.put(name, str);
233 }
234 }
235 }
236
237 return documentation;
238 } catch (IOException | XPathExpressionException ex)
239 {
240 throw new RuntimeException(ex);
241 // return null;
242 }
243 }
244
245 private static List<Node> convertNodes(NodeList nl) throws IOException
246 {
247 List<Node> out = new ArrayList<>();
248 for (int i = 0; i < nl.getLength(); ++i)
249 {
250 out.add(toFragment(nl.item(i)));
251 }
252 return out;
253 }
254
255 /**
256 * Convert a portion of the document into a fragment.
257 *
258 * @param node
259 * @return
260 */
261 public static Node toFragment(Node node)
262 {
263 Document doc = node.getOwnerDocument();
264 DocumentFragment out = doc.createDocumentFragment();
265 while (node.hasChildNodes())
266 {
267 out.appendChild(node.getFirstChild());
268 }
269 if (out.getFirstChild() != null && out.getFirstChild().getNodeType() == Node.TEXT_NODE)
270 out.removeChild(out.getFirstChild());
271 if (out.getLastChild() != null && out.getLastChild().getNodeType() == Node.TEXT_NODE)
272 out.removeChild(out.getLastChild());
273 return out;
274 }
275 }