Skip to content

Commit

Permalink
Merge pull request #1200 from GorgiAstro/fix-javadoc-extraction-17
Browse files Browse the repository at this point in the history
Support extracting  javadoc HTML files generated by Javadoc 17
  • Loading branch information
marscher authored Oct 2, 2024
2 parents 354968c + f32eb49 commit 6b89751
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
48 changes: 38 additions & 10 deletions native/java/org/jpype/javadoc/JavadocExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Javadoc getDocumentation(Class cls)
}
} catch (Exception ex)
{
System.err.println("Failed to extract javadoc for " + cls);
System.err.println("Failed to extract javadoc for " + cls + ", caught " + ex);
if (failures)
throw new RuntimeException(ex);
}
Expand Down Expand Up @@ -117,8 +117,12 @@ public static Javadoc extractDocument(Class cls, Document doc)
XPath xPath = XPathFactory.newInstance().newXPath();
// Javadoc 8-13
Node n = (Node) xPath.compile("//div[@class='description']/ul/li").evaluate(doc, XPathConstants.NODE);
if (n == null) // Javadoc 14+
if (n == null) { // Javadoc 14+
n = (Node) xPath.compile("//section[@class='description']").evaluate(doc, XPathConstants.NODE);
}
if (n == null) { // Javadoc 17+
n = (Node) xPath.compile("//section[@class='class-description']").evaluate(doc, XPathConstants.NODE);
}
Node description = toFragment(n);
if (description != null)
{
Expand All @@ -129,12 +133,20 @@ public static Javadoc extractDocument(Class cls, Document doc)
documentation.description = renderer.render(description);
}

Node ctorRoot = ((Node) xPath.compile("//li/a[@name='constructor.detail' or @id='constructor.detail']")
Node ctorRoot = ((Node) xPath.compile("//li/a[@name='constructor.detail' or @id='constructor.detail']") // Javadoc before 17
.evaluate(doc, XPathConstants.NODE));
if (ctorRoot == null) { // Javadoc 17+
ctorRoot = ((Node) xPath.compile("//section[@class='constructor-details']/ul")
.evaluate(doc, XPathConstants.NODE));
}
if (ctorRoot != null)
{
List<Node> set = convertNodes((NodeList) xPath.compile("./ul/li")
List<Node> set = convertNodes((NodeList) xPath.compile("./li/section") // Javadoc 17+
.evaluate(ctorRoot, XPathConstants.NODESET));
if (set.isEmpty()) { // Javadoc before 17
set = convertNodes((NodeList) xPath.compile("./ul/li")
.evaluate(ctorRoot.getParentNode(), XPathConstants.NODESET));
}
documentation.ctorsNode = set;
StringBuilder sb = new StringBuilder();
for (Node ctor : set)
Expand All @@ -147,12 +159,20 @@ public static Javadoc extractDocument(Class cls, Document doc)
documentation.ctors = sb.toString();
}

Node methodRoot = ((Node) xPath.compile("//li/a[@name='method.detail' or @id='method.detail']")
Node methodRoot = ((Node) xPath.compile("//li/a[@name='method.detail' or @id='method.detail']") // Javadoc before 17
.evaluate(doc, XPathConstants.NODE));
if (methodRoot == null) { // Javadoc 17+
methodRoot = ((Node) xPath.compile("//section[@class='method-details']/ul")
.evaluate(doc, XPathConstants.NODE));
}
if (methodRoot != null)
{
List<Node> set = convertNodes((NodeList) xPath.compile("./ul/li")
.evaluate(methodRoot.getParentNode(), XPathConstants.NODESET));
List<Node> set = convertNodes((NodeList) xPath.compile("./li/section") // Javadoc 17+
.evaluate(methodRoot, XPathConstants.NODESET));
if (set.isEmpty()) { // Javadoc before 17
set = convertNodes((NodeList) xPath.compile("./ul/li")
.evaluate(methodRoot.getParentNode(), XPathConstants.NODESET));
}
documentation.methodNodes = set;
for (Node method : set)
{
Expand All @@ -178,12 +198,20 @@ public static Javadoc extractDocument(Class cls, Document doc)
// NodeList set = (NodeList) xPath.compile("./ul/li").evaluate(inner.getParentNode(), XPathConstants.NODESET);
// documentation.innerNode = convertNodes(set);
// }
Node fieldRoot = ((Node) xPath.compile("//li/a[@name='field.detail' or @id='field.detail']")
Node fieldRoot = ((Node) xPath.compile("//li/a[@name='field.detail' or @id='field.detail']") // Javadoc before 17
.evaluate(doc, XPathConstants.NODE));
if (fieldRoot == null) { // Javadoc 17+
fieldRoot = ((Node) xPath.compile("//section[@class='field-details']/ul")
.evaluate(doc, XPathConstants.NODE));
}
if (fieldRoot != null)
{
List<Node> set = convertNodes((NodeList) xPath.compile("./ul/li")
.evaluate(fieldRoot.getParentNode(), XPathConstants.NODESET));
List<Node> set = convertNodes((NodeList) xPath.compile("./li/section") // Javadoc 17+
.evaluate(fieldRoot, XPathConstants.NODESET));
if (set.isEmpty()) { // Javadoc before 17
set = convertNodes((NodeList) xPath.compile("./ul/li")
.evaluate(fieldRoot.getParentNode(), XPathConstants.NODESET));
}
documentation.fieldNodes = set;
for (Node field : set)
{
Expand Down
8 changes: 5 additions & 3 deletions native/java/org/jpype/javadoc/JavadocTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ void handleDescription(Node node, Workspace data)
{
data.hr = true;
parent.removeChild(node);
} else if (name.equals("pre"))
} else if (name.equals("pre") || // Javadoc pre-17
(name.equals("div") && e.getAttribute("class").equals("type-signature"))) // Javadoc 17+
{
DomUtilities.removeWhitespace(node);
doc.renameNode(node, null, "signature");
Expand All @@ -124,10 +125,11 @@ void handleMembers(Node node, Workspace ws)
String name = e.getTagName();
Document doc = e.getOwnerDocument();

if (name.equals("h4"))
if (name.equals("h4") || name.equals("h3")) // h4 for Javadoc pre-17, h3 for Javadoc 17+
{
doc.renameNode(node, null, "title");
} else if (name.equals("pre"))
} else if (name.equals("pre") || // Javadoc pre-17
(name.equals("div") && (e.getAttribute("class").equals("member-signature")))) // Javadoc 17+
{
doc.renameNode(node, null, "signature");
DomUtilities.traverseDFS(node, this::pass1, Node.ELEMENT_NODE, ws);
Expand Down

0 comments on commit 6b89751

Please sign in to comment.