Skip to content

Commit

Permalink
Merge pull request #5127 from neilcsmith-net/javadoc17-parsing
Browse files Browse the repository at this point in the history
Fix Javadoc 17 parsing
  • Loading branch information
neilcsmith-net authored Jan 3, 2023
2 parents 6afa3a9 + 6b70931 commit f1569af
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 28 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1337,8 +1337,9 @@ jobs:
- name: java.source.queriesimpl
run: ant $OPTS -f java/java.source.queriesimpl test

# - name: java.sourceui
# run: ant $OPTS -f java/java.sourceui test
- name: java.sourceui
if: env.test_java == 'true' && success()
run: ant $OPTS -f java/java.sourceui test

- name: java.testrunner
run: ant $OPTS -f java/java.testrunner test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

import com.sun.tools.javac.code.Symbol.ClassSymbol;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -261,10 +261,21 @@ public URL getLocation(@NonNull final RemoteJavadocPolicy rjp) throws RemoteJava
try {
String charset = null;
for (;;) {
try (Reader reader = charset == null?
try (BufferedReader reader = new BufferedReader(charset == null ?
new InputStreamReader(this.openStream()) :
new InputStreamReader(this.openStream(), charset)){
new InputStreamReader(this.openStream(), charset))) {
if (urls.size() > 1) {
reader.mark(256);
String line = reader.readLine();
if (line.contains("<!DOCTYPE") && line.contains("HTML>")) {
index = 2;
if (jdocRoot != null) {
jdocCache.put(jdocRoot,index);
}
break;
} else {
reader.reset();
}
final HTMLEditorKit.Parser parser = new ParserDelegator();
final int[] state = {-1};
try {
Expand Down Expand Up @@ -936,10 +947,6 @@ private static final class FragmentBuilder {

FragmentBuilder(@NonNull ElementKind kind) {
int size = FILTERS.size();
// JDK-8046068 changed the constructor format from "Name" to "<init>"
if (kind == ElementKind.CONSTRUCTOR) {
size *= 2;
}
this.sbs = new StringBuilder[size];
for (int i = 0; i < sbs.length; i++) {
sbs[i] = new StringBuilder();
Expand All @@ -948,24 +955,18 @@ private static final class FragmentBuilder {

@NonNull
FragmentBuilder constructor(@NonNull final CharSequence text) {
CharSequence constructor = text;
for (int i = 0; i < sbs.length;) {
for (int j = 0; j < FILTERS.size(); j++) {
sbs[i].append(FILTERS.get(j).convert(constructor));
i++;
}
constructor = "<init>";
for (int i = 0; i < sbs.length; i++) {
// JDK-8046068 changed the constructor format from "Name" to "<init>"
CharSequence constructor = i >= 2 ? "<init>" : text;
sbs[i].append(FILTERS.get(i).convert(constructor));
}
return this;
}

@NonNull
FragmentBuilder append(@NonNull final CharSequence text) {
for (int i = 0; i < sbs.length;) {
for (int j = 0; j < FILTERS.size(); j++) {
sbs[i].append(FILTERS.get(j).convert(text));
i++;
}
for (int i = 0; i < sbs.length; i++) {
sbs[i].append(FILTERS.get(i).convert(text));
}
return this;
}
Expand Down
5 changes: 5 additions & 0 deletions java/java.sourceui/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ javac.source=1.8
javadoc.arch=${basedir}/arch.xml
spec.version.base=1.67.0

# failing or missing test files
test.config.default.excludes=\
**/FastIndexTest.class,\
**/MarkupTagProcessorTest.class

test.config.stableBTD.includes=**/*Test.class
test.config.stableBTD.excludes=\
**/ElementHeadersTest.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,28 @@ private static int[] parseClass(Reader reader, final HTMLEditorKit.Parser parser

int div_counter = 0;
int li_counter = 0;
int section_counter = 0;
int nextHRPos = -1;
int lastHRPos = -1;

@Override
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t == HTML.Tag.HR){
if (state[0] == TEXT_START){
if (t == HTML.Tag.HR) {
if (state[0] == TEXT_START) {
nextHRPos = pos;
}
lastHRPos = pos;
} else if (state[0] == AFTER_DIV
&& t instanceof HTML.UnknownTag
&& "section".equalsIgnoreCase(t.toString())) {
if (a.containsAttribute(HTML.Attribute.ENDTAG, "true")) {
if (--section_counter < 0) {
offset[3] = pos;
state[0] = INIT;
}
} else {
section_counter++;
}
}
}

Expand Down Expand Up @@ -367,13 +379,34 @@ private static int[] parseMember(Reader reader, final Collection<? extends Strin
int div_counter = 0;
int dl_counter = 0;
int li_counter = 0;
int section_counter = 0;
int hrPos = -1;
boolean startWithNextText;

@Override
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t == HTML.Tag.HR && state[0] == PRE_CLOSE){
if (t == HTML.Tag.HR && state[0] == PRE_CLOSE) {
hrPos = pos;
} else if (t instanceof HTML.UnknownTag
&& "section".equalsIgnoreCase(t.toString())) {
if (a.containsAttribute(HTML.Attribute.ENDTAG, "true")) {
if (state[0] != INIT) {
if (--section_counter < 0) {
state[0] = INIT;
offset[1] = hrPos;
}
}
} else {
if (state[0] == INIT) {
String attrId = (String) a.getAttribute(HTML.Attribute.ID);
if (names.contains(attrId)) {
// we have found desired javadoc member info anchor
state[0] = A_OPEN;
}
} else {
section_counter++;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public HTMLJavadocParserTest(String testName) {
super(testName);
}

/**
/*
* Test of getJavadocText method ised with HTML produced by standard doclet.
*/
*
public void testGetJavadocText() throws MalformedURLException {
URL url = HTMLJavadocParserTest.class.getResource("HTMLJavadocParser.html");
String result = HTMLJavadocParser.getJavadocText(url, false);
Expand All @@ -46,10 +46,11 @@ public void testGetJavadocText() throws MalformedURLException {
new URL(url, "HTMLJavadocParser.html#getJavadocText(java.net.URL, boolean)"), false);
assertNotNull(result);
}
*/

/**
/*
* Test of getJavadocText method used with javadoc from Android SDK.
*/
*
public void testGetAndroidJavadocText() throws MalformedURLException {
URL url = HTMLJavadocParserTest.class.getResource("Activity.html");
String result = HTMLJavadocParser.getJavadocText(url, false);
Expand All @@ -70,7 +71,56 @@ public void testGetAndroidJavadocText() throws MalformedURLException {
assertTrue(result.contains("See Also"));
}
*/

/**
* Test of getJavadocText method used with class output from javadoc 17.
*/
public void testJavadoc17Class() throws Exception {
URL root = HTMLJavadocParserTest.class.getResource("Javadoc17Class.html");
String result = HTMLJavadocParser.getJavadocText(root, false);
assertNotNull(result);
assertTrue(result.contains("This is an example class."));
assertFalse(result.contains("</section>"));

URL url = appendFragment(root, "<init>(java.lang.String)");
result = HTMLJavadocParser.getJavadocText(url, false);
assertTrue(result.contains("This is a constructor taking a single String parameter."));
assertFalse(result.contains("</section>"));

url = appendFragment(root, "<init>(java.lang.String,java.lang.String)");
result = HTMLJavadocParser.getJavadocText(url, false);
assertTrue(result.contains("This is a constructor taking two String parameters."));
assertFalse(result.contains("</section>"));

url = appendFragment(root, "hi()");
result = HTMLJavadocParser.getJavadocText(url, false);
assertTrue(result.contains("A method."));
assertFalse(result.contains("</section>"));
}

/**
* Test of getJavadocText method used with enum output from javadoc 17.
*/
public void testJavadoc17Enum() throws Exception {
URL root = HTMLJavadocParserTest.class.getResource("Javadoc17Enum.html");
String result = HTMLJavadocParser.getJavadocText(root, false);
assertNotNull(result);
assertTrue(result.contains("This is an example enum."));
assertFalse(result.contains("</section>"));

URL url = appendFragment(root, "FIRST");
result = HTMLJavadocParser.getJavadocText(url, false);
assertTrue(result.contains("The first value."));
assertFalse(result.contains("</section>"));

url = appendFragment(root, "hi()");
result = HTMLJavadocParser.getJavadocText(url, false);
assertTrue(result.contains("A method."));
assertFalse(result.contains("</section>"));

}

/**
* Test of getJavadocText method used with class output from javadoc 11.
*/
Expand Down Expand Up @@ -151,6 +201,7 @@ public void testJavadoc8Enum() throws Exception {
assertTrue(result.contains("A method."));
}

/*
public void test199194() throws MalformedURLException {
URL url = HTMLJavadocParserTest.class.getResource("JavaApplication1.html");
String result = HTMLJavadocParser.getJavadocText(url, false);
Expand All @@ -173,6 +224,7 @@ public void test209707() throws MalformedURLException {
assertNotNull(result);
assertTrue(result.contains("the selected file or"));
}
*/

private static URL appendFragment(URL root, String unencodedFragment) throws Exception {
StringBuilder uri = new StringBuilder(root.toExternalForm());
Expand Down
Loading

0 comments on commit f1569af

Please sign in to comment.