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