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.api.LocalizedMessage;
33 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34 import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
35
36
37
38
39 public final class DetailNodeTreeStringPrinter {
40
41
42 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
43
44
45 private DetailNodeTreeStringPrinter() {
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 public static DetailNode parseJavadocAsDetailNode(DetailAST blockComment) {
65 final JavadocDetailNodeParser parser = new JavadocDetailNodeParser();
66 final ParseStatus status = parser.parseJavadocAsDetailNode(blockComment);
67 if (status.getParseErrorMessage() != null) {
68 throw new IllegalArgumentException(getParseErrorMessage(status.getParseErrorMessage()));
69 }
70 return status.getTree();
71 }
72
73
74
75
76
77
78 private static DetailNode parseJavadocAsDetailNode(String javadocComment) {
79 final DetailAST blockComment = CommonUtil.createBlockCommentNode(javadocComment);
80 return parseJavadocAsDetailNode(blockComment);
81 }
82
83
84
85
86
87
88 private static String getParseErrorMessage(ParseErrorMessage parseErrorMessage) {
89 final LocalizedMessage lmessage = new LocalizedMessage(
90 parseErrorMessage.getLineNumber(),
91 "com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
92 parseErrorMessage.getMessageKey(),
93 parseErrorMessage.getMessageArguments(),
94 "",
95 DetailNodeTreeStringPrinter.class,
96 null);
97 return "[ERROR:" + parseErrorMessage.getLineNumber() + "] " + lmessage.getMessage();
98 }
99
100
101
102
103
104
105
106
107 public static String printTree(DetailNode ast, String rootPrefix, String prefix) {
108 final StringBuilder messageBuilder = new StringBuilder(1024);
109 DetailNode node = ast;
110 while (node != null) {
111 if (node.getType() == JavadocTokenTypes.JAVADOC) {
112 messageBuilder.append(rootPrefix);
113 }
114 else {
115 messageBuilder.append(prefix);
116 }
117 messageBuilder.append(getIndentation(node))
118 .append(JavadocUtil.getTokenName(node.getType())).append(" -> ")
119 .append(JavadocUtil.escapeAllControlChars(node.getText())).append(" [")
120 .append(node.getLineNumber()).append(':').append(node.getColumnNumber())
121 .append(']').append(LINE_SEPARATOR)
122 .append(printTree(JavadocUtil.getFirstChild(node), rootPrefix, prefix));
123 node = JavadocUtil.getNextSibling(node);
124 }
125 return messageBuilder.toString();
126 }
127
128
129
130
131
132
133 private static String getIndentation(DetailNode node) {
134 final boolean isLastChild = JavadocUtil.getNextSibling(node) == null;
135 DetailNode currentNode = node;
136 final StringBuilder indentation = new StringBuilder(1024);
137 while (currentNode.getParent() != null) {
138 currentNode = currentNode.getParent();
139 if (currentNode.getParent() == null) {
140 if (isLastChild) {
141
142
143 indentation.append("`--");
144 }
145 else {
146 indentation.append("|--");
147 }
148 }
149 else {
150 if (JavadocUtil.getNextSibling(currentNode) == null) {
151 indentation.insert(0, " ");
152 }
153 else {
154 indentation.insert(0, "| ");
155 }
156 }
157 }
158 return indentation.toString();
159 }
160
161
162
163
164
165
166
167 private static DetailNode parseFile(File file) throws IOException {
168 final FileText text = new FileText(file.getAbsoluteFile(),
169 System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
170 return parseJavadocAsDetailNode(text.getFullText().toString());
171 }
172
173 }