Generated HTML data extraction hacks
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.springframework.ide.vscode.commons.javadoc.internal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.ide.vscode.commons.java.IField;
|
||||
@@ -206,7 +207,7 @@ public class JavadocContents {
|
||||
|
||||
int javadocStart = indexOfEndLink + JavadocConstants.ANCHOR_SUFFIX_LENGTH;
|
||||
int javadocEnd = indexOfNextElement == -1 ? indexOfBottom : Math.min(indexOfNextElement, indexOfBottom);
|
||||
range = new int[]{javadocStart, javadocEnd};
|
||||
range = sanitizeRange(new int[]{javadocStart, javadocEnd});
|
||||
} else {
|
||||
// the anchor has no suffix
|
||||
range = UNKNOWN_FORMAT;
|
||||
@@ -237,7 +238,10 @@ public class JavadocContents {
|
||||
lastIndex = this.indexOfMethodDetails == -1 ? lastIndex : this.indexOfMethodDetails;
|
||||
|
||||
// we take the end of class data
|
||||
final int indexOfStartOfClassData = CharOperation.indexOf(JavadocConstants.START_OF_CLASS_DATA, this.content, false);
|
||||
this.indexOfEndOfClassData = CharOperation.indexOf(JavadocConstants.END_OF_CLASS_DATA, this.content, false, lastIndex);
|
||||
int[] classDataRange = sanitizeRange(new int[] { indexOfStartOfClassData + JavadocConstants.START_OF_CLASS_DATA.length, indexOfEndOfClassData});
|
||||
this.indexOfEndOfClassData = classDataRange[1];
|
||||
|
||||
// try to find the field detail end
|
||||
this.indexOfFieldsBottom =
|
||||
@@ -252,9 +256,38 @@ public class JavadocContents {
|
||||
|
||||
this.indexOfAllMethodsBottom = this.indexOfEndOfClassData;
|
||||
|
||||
// Get rid of possible <ul><li> tag wrappers
|
||||
int[] fieldsRange = sanitizeRange(new int[] {indexOfFieldDetails + JavadocConstants.FIELD_DETAIL.length, indexOfFieldsBottom});
|
||||
indexOfFieldDetails = fieldsRange[0];
|
||||
indexOfFieldsBottom = fieldsRange[1];
|
||||
|
||||
int[] methodsRange = sanitizeRange(new int[] {
|
||||
indexOfAllMethodsTop + (indexOfAllMethodsTop == indexOfConstructorDetails
|
||||
? JavadocConstants.CONSTRUCTOR_DETAIL.length : JavadocConstants.METHOD_DETAIL.length),
|
||||
indexOfAllMethodsBottom });
|
||||
indexOfAllMethodsTop = methodsRange[0];
|
||||
indexOfAllMethodsBottom = methodsRange[1];
|
||||
|
||||
// Remove trailing extra tag closings
|
||||
String badEnding = "</li>\n</ul>\n</li>\n</ul>";
|
||||
indexOfAllMethodsBottom = trimBadEnding(badEnding, indexOfAllMethodsBottom, badEnding.length() / 2);
|
||||
|
||||
this.hasComputedChildrenSections = true;
|
||||
|
||||
}
|
||||
|
||||
private int trimBadEnding(String badEnding, int end) {
|
||||
return trimBadEnding(badEnding, end, badEnding.length());
|
||||
}
|
||||
|
||||
private int trimBadEnding(String badEnding, int end, int numberOfCharsToTrim) {
|
||||
if (Arrays.equals(CharOperation.subarray(content, end - badEnding.length(), end), badEnding.toCharArray())) {
|
||||
return end - numberOfCharsToTrim;
|
||||
} else {
|
||||
return end;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the ranges of the parts of the javadoc that describe each child of the type (fields, methods)
|
||||
*/
|
||||
@@ -534,6 +567,54 @@ public class JavadocContents {
|
||||
start = afterHierarchy;
|
||||
}
|
||||
|
||||
this.typeDocRange = new int[]{start, indexOfNextSummary};
|
||||
int indexOfClassDescriptionEnd = trimBadEnding("<div class=\"summary\">\n<ul class=\"blockList\">\n<li class=\"blockList\">\n", indexOfNextSummary);
|
||||
indexOfClassDescriptionEnd = trimBadEnding("</li>\n</ul>\n</div>\n", indexOfClassDescriptionEnd);
|
||||
this.typeDocRange = new int[]{start, indexOfClassDescriptionEnd};
|
||||
this.typeDocRange = sanitizeRange(typeDocRange);
|
||||
|
||||
}
|
||||
|
||||
private int[] trimRange(int[] range) {
|
||||
int start = range[0];
|
||||
int end = range[1];
|
||||
|
||||
while (start < content.length && start < end && Character.isWhitespace(content[start])) {
|
||||
start++;
|
||||
}
|
||||
|
||||
while (end > 0 && start < end && Character.isWhitespace(content[end-1])) {
|
||||
end--;
|
||||
}
|
||||
|
||||
return new int[] { start, end };
|
||||
}
|
||||
|
||||
private int[] trimTag(int[] range, CharSequence tag) {
|
||||
int start = range[0];
|
||||
int end = range[1];
|
||||
|
||||
char[] startingTag = ("<" + tag).toCharArray();
|
||||
char[] closingTag = ("</" + tag + ">").toCharArray();
|
||||
char[] ending = CharOperation.subarray(content, end - closingTag.length, end);
|
||||
char[] starting = CharOperation.subarray(content, start, start + startingTag.length);
|
||||
if (Arrays.equals(closingTag, ending) && Arrays.equals(startingTag, starting)) {
|
||||
return new int[] { CharOperation.indexOf('>', content, start, end) + 1, end - closingTag.length};
|
||||
} else {
|
||||
return range;
|
||||
}
|
||||
}
|
||||
|
||||
private int[] sanitizeRange(int[] range) {
|
||||
boolean changed = false;
|
||||
do {
|
||||
int[] newRange = trimRange(range);
|
||||
newRange = trimTag(newRange, "div");
|
||||
newRange = trimTag(newRange, "ul");
|
||||
newRange = trimTag(newRange, "li");
|
||||
changed = !Arrays.equals(newRange, range);
|
||||
range = newRange;
|
||||
} while (changed);
|
||||
return range;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.springframework.ide.vscode.commons.maven;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -318,9 +317,6 @@ public class JavaIndexTest {
|
||||
assertNotNull(method);
|
||||
|
||||
String expected = String.join("\n",
|
||||
"",
|
||||
"<ul class=\"blockList\">",
|
||||
"<li class=\"blockList\">",
|
||||
"<h4>size</h4>",
|
||||
"<pre>public int size()</pre>",
|
||||
"<div class=\"block\">Returns the number of elements in this list.</div>"
|
||||
@@ -340,9 +336,6 @@ public class JavaIndexTest {
|
||||
assertNotNull(method);
|
||||
|
||||
String expected = String.join("\n",
|
||||
"",
|
||||
"<ul class=\"blockList\">",
|
||||
"<li class=\"blockList\">",
|
||||
"<h4>ArrayList</h4>"
|
||||
);
|
||||
assertEquals(expected, method.getJavaDoc().html().substring(0, expected.length()));
|
||||
@@ -360,18 +353,31 @@ public class JavaIndexTest {
|
||||
|
||||
IField field = type.getField("BANNER_LOCATION_PROPERTY_VALUE");
|
||||
assertNotNull(field);
|
||||
assertTrue(field.getJavaDoc().html().contains("<h4>BANNER_LOCATION_PROPERTY_VALUE</h4>"));
|
||||
String expected = String.join("\n",
|
||||
"<h4>BANNER_LOCATION_PROPERTY_VALUE</h4>",
|
||||
"<pre>public static final <a href=\"http://docs.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true\" title=\"class or interface in java.lang\">String</a> BANNER_LOCATION_PROPERTY_VALUE</pre>",
|
||||
"<div class=\"block\">Default banner location.</div>",
|
||||
"<dl>",
|
||||
"<dt><span class=\"seeLabel\">See Also:</span></dt>",
|
||||
"<dd><a href=\"../../../constant-values.html#org.springframework.boot.SpringApplication.BANNER_LOCATION_PROPERTY_VALUE\">Constant Field Values</a></dd>",
|
||||
"</dl>"
|
||||
);
|
||||
assertEquals(expected, field.getJavaDoc().html());
|
||||
|
||||
IMethod method = type.getMethod("getListeners", Stream.empty());
|
||||
assertNotNull(method);
|
||||
String expected = String.join("\n",
|
||||
"",
|
||||
"<ul class=\"blockList\">",
|
||||
"<li class=\"blockList\">",
|
||||
"<h4>getListeners</h4>"
|
||||
);
|
||||
assertEquals(expected, method.getJavaDoc().html().substring(0, expected.length()));
|
||||
|
||||
expected = String.join("\n",
|
||||
"<h4>getListeners</h4>",
|
||||
"<pre>public <a href=\"http://docs.oracle.com/javase/6/docs/api/java/util/Set.html?is-external=true\" title=\"class or interface in java.util\">Set</a><org.springframework.context.ApplicationListener<?>> getListeners()</pre>",
|
||||
"<div class=\"block\">Returns read-only ordered Set of the <code>ApplicationListener</code>s that will be",
|
||||
" applied to the SpringApplication and registered with the <code>ApplicationContext</code>",
|
||||
" .</div>",
|
||||
"<dl>",
|
||||
"<dt><span class=\"returnLabel\">Returns:</span></dt>",
|
||||
"<dd>the listeners</dd>",
|
||||
"</dl>"
|
||||
);
|
||||
assertEquals(expected, method.getJavaDoc().html());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -383,28 +389,85 @@ public class JavaIndexTest {
|
||||
IType type = project.findType("hello.Greeting");
|
||||
|
||||
assertNotNull(type);
|
||||
String expected = "<div class=\"block\">Comment for Greeting class</div>";
|
||||
String expected = "Comment for Greeting class";
|
||||
assertEquals(expected, type.getJavaDoc().html().substring(0, expected.length()));
|
||||
|
||||
IField field = type.getField("id");
|
||||
assertNotNull(field);
|
||||
expected = String.join("\n",
|
||||
"",
|
||||
"<ul class=\"blockListLast\">",
|
||||
"<li class=\"blockList\">",
|
||||
"<h4>id</h4>"
|
||||
"<h4>id</h4>",
|
||||
"<pre>protected final long id</pre>",
|
||||
"<div class=\"block\">Comment for id field</div>"
|
||||
);
|
||||
assertEquals(expected, field.getJavaDoc().html().substring(0, expected.length()));
|
||||
assertEquals(expected, field.getJavaDoc().html());
|
||||
|
||||
IMethod method = type.getMethod("getId", Stream.empty());
|
||||
assertNotNull(method);
|
||||
expected = String.join("\n",
|
||||
"",
|
||||
"<ul class=\"blockList\">",
|
||||
"<li class=\"blockList\">",
|
||||
"<h4>getId</h4>"
|
||||
"<h4>getId</h4>",
|
||||
"<pre>public long getId()</pre>",
|
||||
"<div class=\"block\">Comment for getId()</div>"
|
||||
);
|
||||
assertEquals(expected, method.getJavaDoc().html().substring(0, expected.length()));
|
||||
assertEquals(expected, method.getJavaDoc().html());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void html_testInnerClassJavadocForOutputFolder() throws Exception {
|
||||
MavenProjectClasspath.providerType = JavadocProviderTypes.HTML;
|
||||
Path projectPath = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
|
||||
MavenCore.generateJavadocFolderForMavenProject(projectPath);
|
||||
MavenJavaProject project = createMavenProject(projectPath);
|
||||
|
||||
IType type = project.findType("hello.Greeting$TestInnerClass");
|
||||
assertNotNull(type);
|
||||
assertEquals("Comment for inner class", type.getJavaDoc().html());
|
||||
|
||||
IField field = type.getField("innerField");
|
||||
assertNotNull(field);
|
||||
String expected = String.join("\n",
|
||||
"<h4>innerField</h4>",
|
||||
"<pre>protected int innerField</pre>",
|
||||
"<div class=\"block\">Comment for inner field</div>"
|
||||
);
|
||||
assertEquals(expected, field.getJavaDoc().html());
|
||||
|
||||
IMethod method = type.getMethod("getInnerField", Stream.empty());
|
||||
assertNotNull(method);
|
||||
expected = String.join("\n",
|
||||
"<h4>getInnerField</h4>",
|
||||
"<pre>public int getInnerField()</pre>",
|
||||
"<div class=\"block\">Comment for method inside nested class</div>"
|
||||
);
|
||||
assertEquals(expected, method.getJavaDoc().html());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void html_testInnerClassLevel2_JavadocForOutputFolder() throws Exception {
|
||||
MavenProjectClasspath.providerType = JavadocProviderTypes.HTML;
|
||||
Path projectPath = projectsCache.get("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
|
||||
MavenCore.generateJavadocFolderForMavenProject(projectPath);
|
||||
MavenJavaProject project = createMavenProject(projectPath);
|
||||
|
||||
IType type = project.findType("hello.Greeting$TestInnerClass$TestInnerClassLevel2");
|
||||
assertNotNull(type);
|
||||
assertEquals("Comment for level 2 nested class", type.getJavaDoc().html());
|
||||
|
||||
IField field = type.getField("innerLevel2Field");
|
||||
assertNotNull(field);
|
||||
String expected = String.join("\n",
|
||||
"<h4>innerLevel2Field</h4>",
|
||||
"<pre>protected int innerLevel2Field</pre>",
|
||||
"<div class=\"block\">Comment for level 2 inner field</div>"
|
||||
);
|
||||
assertEquals(expected, field.getJavaDoc().html());
|
||||
|
||||
IMethod method = type.getMethod("getInnerLevel2Field", Stream.empty());
|
||||
assertNotNull(method);
|
||||
expected = String.join("\n",
|
||||
"<h4>getInnerLevel2Field</h4>",
|
||||
"<pre>public int getInnerLevel2Field()</pre>",
|
||||
"<div class=\"block\">Comment for method inside level 2 nested class</div>"
|
||||
);
|
||||
assertEquals(expected, method.getJavaDoc().html());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class Greeting {
|
||||
/**
|
||||
* Comment for inner field
|
||||
*/
|
||||
int innerField;
|
||||
protected int innerField;
|
||||
|
||||
/**
|
||||
* Comment for method inside nested class
|
||||
@@ -49,5 +49,23 @@ public class Greeting {
|
||||
return innerField;
|
||||
}
|
||||
|
||||
/**
|
||||
* Comment for level 2 nested class
|
||||
*/
|
||||
public class TestInnerClassLevel2 {
|
||||
|
||||
/**
|
||||
* Comment for level 2 inner field
|
||||
*/
|
||||
protected int innerLevel2Field;
|
||||
|
||||
/**
|
||||
* Comment for method inside level 2 nested class
|
||||
*/
|
||||
public int getInnerLevel2Field() {
|
||||
return innerField;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user