diff --git a/javaparser-core-testing/src/test/java/com/github/javaparser/printer/YamlPrinterTest.java b/javaparser-core-testing/src/test/java/com/github/javaparser/printer/YamlPrinterTest.java index 7847ae2d62..409f52f5f5 100644 --- a/javaparser-core-testing/src/test/java/com/github/javaparser/printer/YamlPrinterTest.java +++ b/javaparser-core-testing/src/test/java/com/github/javaparser/printer/YamlPrinterTest.java @@ -26,6 +26,7 @@ import org.junit.Test; import com.github.javaparser.JavaParser; +import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.expr.Expression; public class YamlPrinterTest { @@ -91,4 +92,29 @@ public void testWithColonFollowedByLineSeparatorInValue() { String output = yamlPrinter.output(expression); assertEquals(expectedOutput, output); } + + @Test + public void testParsingJavadocWithQuoteAndNewline() { + String code = "/**" + System.lineSeparator() + + " * \" this comment contains a quote and newlines" + System.lineSeparator() + + " */" + System.lineSeparator() + + "public class Dog {" + System.lineSeparator() + + "" + System.lineSeparator() + + "}"; + String expectedOutput = "---" + System.lineSeparator(); + expectedOutput += "root(Type=CompilationUnit): " + System.lineSeparator(); + expectedOutput += " types: " + System.lineSeparator(); + expectedOutput += " - type(Type=ClassOrInterfaceDeclaration): " + System.lineSeparator(); + expectedOutput += " isInterface: \"false\"" + System.lineSeparator(); + expectedOutput += " name(Type=SimpleName): " + System.lineSeparator(); + expectedOutput += " identifier: \"Dog\"" + System.lineSeparator(); + expectedOutput += " comment(Type=JavadocComment): " + System.lineSeparator(); + expectedOutput += " content: \"\\n * \\\" this comment contains a quote\\n \"" + System.lineSeparator(); + expectedOutput += "..."; + + YamlPrinter yamlPrinter = new YamlPrinter(true); + CompilationUnit computationUnit = JavaParser.parse(code); + String output = yamlPrinter.output(computationUnit); + assertEquals(expectedOutput, output); + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/printer/YamlPrinter.java b/javaparser-core/src/main/java/com/github/javaparser/printer/YamlPrinter.java index 3f8e99da57..85dfc0d944 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/printer/YamlPrinter.java +++ b/javaparser-core/src/main/java/com/github/javaparser/printer/YamlPrinter.java @@ -69,11 +69,7 @@ public void output(Node node, String name, int level, StringBuilder builder) { level++; for (PropertyMetaModel a : attributes) { - String text = "\"" + a.getValue(node).toString().replace("\\", "\\\\"). - replaceAll("\"", "\\\\\"").replace("\n", "\\n").replace("\t", "\\t") - + "\""; - builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \"" - + text + "\""); + builder.append(System.lineSeparator() + indent(level) + a.getName() + ": " + escapeValue(a.getValue(node).toString())); } for (PropertyMetaModel sn : subNodes) { @@ -101,4 +97,9 @@ private String indent(int level) { sb.append(" "); return sb.toString(); } + + private String escapeValue(String value) { + return "\"" + value.replace("\\", "\\\\").replaceAll("\"", "\\\\\"").replace("\n", "\\n"). + replace("\t", "\\t") + "\""; + } }