ASTLiteral extends AbstractJavaTypeNode { static String determineTextBlockContent(String image) { String content = image.replaceAll("\r\n|\r", "\n"); int start = determineContentStart(content); content = content.substring(start, content.length() - TEXTBLOCK_DELIMITER.length()); int prefixLength = Integer.MAX_VALUE; List<String> lines = Arrays.asList(content.split("\\n")); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!StringUtils.isAllBlank(line) || i == lines.size() - 1) { prefixLength = Math.min(prefixLength, countLeadingWhitespace(line)); } } if (prefixLength == Integer.MAX_VALUE) { prefixLength = 0; } StringBuilder sb = new StringBuilder(content.length()); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!StringUtils.isAllBlank(line) && line.length() >= prefixLength) { line = line.substring(prefixLength); } line = removeTrailingWhitespace(line); sb.append(line); boolean isLastLine = i == lines.size() - 1; boolean isFirstLine = i == 0; if (!isLastLine || !isFirstLine && !StringUtils.isAllBlank(line)) { sb.append('\n'); } } interpretEscapeSequences(sb); return sb.toString(); } @InternalApi @Deprecated  ASTLiteral(int id); @InternalApi @Deprecated  ASTLiteral(JavaParser p, int id); @Override Object jjtAccept(JavaParserVisitor visitor, Object data); @InternalApi @Deprecated void setIntLiteral(); boolean isIntLiteral(); boolean isLongLiteral(); @InternalApi @Deprecated void setFloatLiteral(); boolean isFloatLiteral(); boolean isDoubleLiteral(); int getValueAsInt(); long getValueAsLong(); float getValueAsFloat(); double getValueAsDouble(); @InternalApi @Deprecated void setCharLiteral(); boolean isCharLiteral(); @InternalApi @Deprecated void setStringLiteral(); boolean isStringLiteral(); String getEscapedStringLiteral(); boolean isSingleCharacterStringLiteral(); boolean isTextBlock(); String getTextBlockContent();  }
@Test public void testTextBlockContent() { assertEquals("empty text block", "", ASTLiteral.determineTextBlockContent("\"\"\"\n \"\"\"")); assertEquals("single line text block", "winter", ASTLiteral.determineTextBlockContent("\"\"\"\n winter\"\"\"")); assertEquals("single line text block with LF", "winter\n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " winter\n" + " \"\"\"")); assertEquals("basic text block example with html", "<html>\n" + " <body>\n" + " <p>Hello, world</p>\n" + " </body>\n" + "</html>\n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " <html> \n" + " <body>\n" + " <p>Hello, world</p> \n" + " </body> \n" + " </html> \n" + " \"\"\"")); assertEquals("text block with escapes", "<html>\r\n" + " <body>\r\n" + " <p>Hello, world</p>\r\n" + " </body>\r\n" + "</html>\r\n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " <html>\\r\n" + " <body>\\r\n" + " <p>Hello, world</p>\\r\n" + " </body>\\r\n" + " </html>\\r\n" + " \"\"\"")); assertEquals("escaped text block in inside text block", "String text = \"\"\"\n" + " A text block inside a text block\n" + "\"\"\";\n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " String text = \\\"\"\"\n" + " A text block inside a text block\n" + " \\\"\"\";\n" + " \"\"\"")); assertEquals("new escape: line continuation", "Lorem ipsum dolor sit amet, consectetur adipiscing " + "elit, sed do eiusmod tempor incididunt ut labore " + "et dolore magna aliqua.", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " Lorem ipsum dolor sit amet, consectetur adipiscing \\\n" + " elit, sed do eiusmod tempor incididunt ut labore \\\n" + " et dolore magna aliqua.\\\n" + " \"\"\"")); assertEquals("new escape: space escape", "red \n" + "green \n" + "blue \n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " red \\s\n" + " green\\s\n" + " blue \\s\n" + " \"\"\"")); assertEquals("with crlf line endings", "<html>\n" + " <body>\n" + " <p>Hello, world</p>\n" + " </body>\n" + "</html>\n", ASTLiteral.determineTextBlockContent("\"\"\"\r\n" + " <html> \r\n" + " <body>\r\n" + " <p>Hello, world</p> \r\n" + " </body> \r\n" + " </html> \r\n" + " \"\"\"")); assertEquals("with cr line endings", "<html>\n" + " <body>\n" + " <p>Hello, world</p>\n" + " </body>\n" + "</html>\n", ASTLiteral.determineTextBlockContent("\"\"\"\r" + " <html> \r" + " <body>\r" + " <p>Hello, world</p> \r" + " </body> \r" + " </html> \r" + " \"\"\"")); assertEquals("empty line directly after opening", "\ntest\n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + " \n" + " test\n" + " \"\"\"")); assertEquals("empty crlf line directly after opening", "\ntest\n", ASTLiteral.determineTextBlockContent("\"\"\"\r\n" + " \r\n" + " test\r\n" + " \"\"\"")); assertEquals("empty line directly after opening without indentation", "\ntest\n", ASTLiteral.determineTextBlockContent("\"\"\"\n" + "\n" + "test\n" + "\"\"\"")); assertEquals("empty crlf line directly after opening without indentation", "\ntest\n", ASTLiteral.determineTextBlockContent("\"\"\"\r\n" + "\r\n" + "test\r\n" + "\"\"\"")); assertEquals("text block with backslash escape", "\\test\n", ASTLiteral.determineTextBlockContent("\"\"\"\n \\\\test\n \"\"\"")); }