Skip to content

Commit

Permalink
[http-core] Handle @param Newlines in Javadoc Reader (#351)
Browse files Browse the repository at this point in the history
* Update JavadocParserTest.java

* Revert "Update JavadocParserTest.java"

This reverts commit a6ff641.

* handle newlines in params
  • Loading branch information
SentryMan authored Dec 12, 2023
1 parent 411267c commit 05009d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ class JavadocParser {
private static final int PARAM_DESC = 7;
private static final int RETURN_DESC = 8;
private static final int IGNORE = 9;
private static final int CODE = 10;
private static final String DEPRECATED = "deprecated";

private int previousState = TEXT;

private StringBuilder currentParam = new StringBuilder();
private StringBuilder currentDoclet = new StringBuilder();
private final StringBuilder currentParam = new StringBuilder();
private final StringBuilder currentDoclet = new StringBuilder();
private StringBuilder currentContent;

private int state = TEXT;

private String returnDesc = "";

private Map<String, String> params = new LinkedHashMap<>();
private final Map<String, String> params = new LinkedHashMap<>();

private boolean deprecated;

private boolean hasReturn;

/**
* Parse the javadoc.
*/
Expand All @@ -48,6 +51,7 @@ Javadoc parse(String text) {
currentContent = mainContent;

for (char c : text.toCharArray()) {

switch (state) {
case RETURN_DESC:
if (c == '\n') {
Expand All @@ -57,6 +61,11 @@ Javadoc parse(String text) {
if (state == PARAM_DESC) {
processSetParam();
}
case CODE:
if (state == CODE && c == '}') {
state = previousState;
break;
}
case TEXT:
processText(c);
continue;
Expand All @@ -77,32 +86,44 @@ Javadoc parse(String text) {
}
}

if (state == RETURN_DESC) {
returnDesc = currentContent.toString();
if (hasReturn) {
returnDesc = mergeLines(currentContent.toString());
}

return splitMain(mainContent.toString().trim());
}

private void processSetParam() {
params.put(currentParam.toString(), currentContent.toString().trim());
params.put(currentParam.toString(), mergeLines(currentContent.toString().trim()));
}

private void processReturnDesc(StringBuilder mainContent) {
state = TEXT;
previousState = TEXT;
returnDesc = currentContent.toString();
currentContent = mainContent;
currentContent = new StringBuilder(returnDesc);
}

private void processText(char c) {
if (c == '{' || c == '@') {
currentDoclet.delete(0, currentDoclet.length());
state = DOCLET_START;
} else if (c == '<') {
state = TAG_START;
} else if (c != '}' && c != '>') {
currentContent.append(c);
switch (c) {
case '{':
case '@':
currentDoclet.delete(0, currentDoclet.length());
state = DOCLET_START;
break;
case '<':
state = TAG_START;
break;
case '\n':
if (state == CODE) {
currentContent.append("\\n");
return;
}
default:
if (c != '}' && c != '>') {
currentContent.append(c);
}
break;
}
}

Expand Down Expand Up @@ -132,23 +153,22 @@ private void processDocletStart(char c) {
deprecated = true;
}
state = IGNORE;
} else {
if (docletName.equals("param")) {
currentParam.delete(0, currentParam.length());
state = PARAM_NAME;
}
if (docletName.equals("return")) {
currentContent = new StringBuilder();
state = RETURN_DESC;
previousState = RETURN_DESC;
}
} else if ("param".equals(docletName)) {
currentParam.delete(0, currentParam.length());
state = PARAM_NAME;
} else if ("return".equals(docletName)) {
currentContent = new StringBuilder();
state = RETURN_DESC;
previousState = RETURN_DESC;
hasReturn=true;
} else if ("@code".equals(docletName)) {
state = CODE;
}
} else {
currentDoclet.append(c);
}
}


private Javadoc splitMain(String mainText) {

String desc = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,22 @@ public void lines() {
assertThat(parser.mergeLines("\n one \n two \n three \n")).isEqualTo("one two three");

}

@Test
public void parse_param_with_newline() {

JavadocParser parser = new JavadocParser();

Javadoc doc = parser.parse(
"This is a description with <b>bold</b> and {@code some code}\n" +
"@since 1.0\n" +
"@param foo The\n foo param\n" +
"@param bar The {@code \n} param\n" +
"@return The {@value return}\n value\n");

assertEquals("This is a description with bold and some code", doc.getSummary());
assertEquals("The foo param", doc.getParams().get("foo"));
assertEquals("The \\n param", doc.getParams().get("bar"));
assertEquals("The return value", doc.getReturnDescription());
}
}

0 comments on commit 05009d8

Please sign in to comment.