1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25
26 import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage;
27 import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseStatus;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.DetailNode;
30 import com.puppycrawl.tools.checkstyle.api.FileText;
31 import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
32 import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
33 import com.puppycrawl.tools.checkstyle.utils.ParserUtil;
34
35
36
37
38 public final class DetailNodeTreeStringPrinter {
39
40
41 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
42
43
44 private DetailNodeTreeStringPrinter() {
45
46 }
47
48
49
50
51
52
53
54
55 public static String printFileAst(File file) throws IOException {
56 return printTree(parseFile(file), "", "");
57 }
58
59
60
61
62
63
64
65
66 public static DetailNode parseJavadocAsDetailNode(DetailAST blockComment) {
67 final JavadocDetailNodeParser parser = new JavadocDetailNodeParser();
68 final ParseStatus status = parser.parseJavadocAsDetailNode(blockComment);
69 if (status.getParseErrorMessage() != null) {
70 throw new IllegalArgumentException(getParseErrorMessage(status.getParseErrorMessage()));
71 }
72 return status.getTree();
73 }
74
75
76
77
78
79
80
81 private static DetailNode parseJavadocAsDetailNode(String javadocComment) {
82 final DetailAST blockComment = ParserUtil.createBlockCommentNode(javadocComment);
83 return parseJavadocAsDetailNode(blockComment);
84 }
85
86
87
88
89
90
91
92 private static String getParseErrorMessage(ParseErrorMessage parseErrorMessage) {
93 final LocalizedMessage message = new LocalizedMessage(
94 "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
95 DetailNodeTreeStringPrinter.class,
96 parseErrorMessage.getMessageKey(),
97 parseErrorMessage.getMessageArguments());
98 return "[ERROR:" + parseErrorMessage.getLineNumber() + "] " + message.getMessage();
99 }
100
101
102
103
104
105
106
107
108
109 public static String printTree(DetailNode ast, String rootPrefix, String prefix) {
110 final StringBuilder messageBuilder = new StringBuilder(1024);
111 DetailNode node = ast;
112 while (node != null) {
113 if (node.getType() == JavadocTokenTypes.JAVADOC) {
114 messageBuilder.append(rootPrefix);
115 }
116 else {
117 messageBuilder.append(prefix);
118 }
119 messageBuilder.append(getIndentation(node))
120 .append(JavadocUtil.getTokenName(node.getType())).append(" -> ")
121 .append(JavadocUtil.escapeAllControlChars(node.getText())).append(" [")
122 .append(node.getLineNumber()).append(':').append(node.getColumnNumber())
123 .append(']').append(LINE_SEPARATOR)
124 .append(printTree(JavadocUtil.getFirstChild(node), rootPrefix, prefix));
125 node = JavadocUtil.getNextSibling(node);
126 }
127 return messageBuilder.toString();
128 }
129
130
131
132
133
134
135
136 private static String getIndentation(DetailNode node) {
137 final boolean isLastChild = JavadocUtil.getNextSibling(node) == null;
138 DetailNode currentNode = node;
139 final StringBuilder indentation = new StringBuilder(1024);
140 while (currentNode.getParent() != null) {
141 currentNode = currentNode.getParent();
142 if (currentNode.getParent() == null) {
143 if (isLastChild) {
144
145
146 indentation.append("`--");
147 }
148 else {
149 indentation.append("|--");
150 }
151 }
152 else {
153 if (JavadocUtil.getNextSibling(currentNode) == null) {
154 indentation.insert(0, " ");
155 }
156 else {
157 indentation.insert(0, "| ");
158 }
159 }
160 }
161 return indentation.toString();
162 }
163
164
165
166
167
168
169
170
171 private static DetailNode parseFile(File file) throws IOException {
172 final FileText text = new FileText(file.getAbsoluteFile(),
173 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
174 return parseJavadocAsDetailNode(text.getFullText().toString());
175 }
176
177 }