| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 77 |
is = cl.getResourceAsStream(name); |
| 78 |
if (is != null) |
| 79 |
return is; |
| 80 |
|
| 81 |
|
| 82 |
String name1 = "docs/api/" + name; |
| 83 |
is = cl.getResourceAsStream(name1); |
| 84 |
if (is != null) |
| 85 |
return is; |
| 86 |
|
| 87 |
|
| 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 |
|
| 100 |
} |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 119 |
Node n = (Node) xPath.compile("//div[@class='description']/ul/li").evaluate(doc, XPathConstants.NODE); |
| 120 |
if (n == null) |
| 121 |
{ |
| 122 |
n = (Node) xPath.compile("//section[@class='description']").evaluate(doc, XPathConstants.NODE); |
| 123 |
} |
| 124 |
if (n == null) |
| 125 |
{ |
| 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']") |
| 139 |
.evaluate(doc, XPathConstants.NODE)); |
| 140 |
if (ctorRoot == null) |
| 141 |
{ |
| 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") |
| 148 |
.evaluate(ctorRoot, XPathConstants.NODESET)); |
| 149 |
if (set.isEmpty()) |
| 150 |
{ |
| 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']") |
| 167 |
.evaluate(doc, XPathConstants.NODE)); |
| 168 |
if (methodRoot == null) |
| 169 |
{ |
| 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") |
| 176 |
.evaluate(methodRoot, XPathConstants.NODESET)); |
| 177 |
if (set.isEmpty()) |
| 178 |
{ |
| 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 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
Node fieldRoot = ((Node) xPath.compile("//li/a[@name='field.detail' or @id='field.detail']") |
| 208 |
.evaluate(doc, XPathConstants.NODE)); |
| 209 |
if (fieldRoot == null) |
| 210 |
{ |
| 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") |
| 217 |
.evaluate(fieldRoot, XPathConstants.NODESET)); |
| 218 |
if (set.isEmpty()) |
| 219 |
{ |
| 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 |
|
| 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 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
} |