KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > tools > patcomp > PrettyCodeEmitter


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.tools.patcomp;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26
27 public class PrettyCodeEmitter implements CodeEmitter {
28     private PrintStream JavaDoc out;
29     private int indent;
30     private String JavaDoc last = "";
31     private boolean newline;
32
33     private static final HashSet JavaDoc<String JavaDoc> noSpaceBefore = new HashSet JavaDoc<String JavaDoc>();
34     private static final HashSet JavaDoc<String JavaDoc> noSpaceAfter = new HashSet JavaDoc<String JavaDoc>();
35     private static final HashSet JavaDoc<String JavaDoc> alwaysSpaceBeforeAndAfter = new HashSet JavaDoc<String JavaDoc>();
36     static {
37         String JavaDoc[] both = new String JavaDoc[]{ ".", "(", ")", "[", "]"};
38         String JavaDoc[] before = new String JavaDoc[]{ ",", ";" };
39         noSpaceAfter.addAll(Arrays.asList(both));
40         noSpaceBefore.addAll(Arrays.asList(both));
41         noSpaceBefore.addAll(Arrays.asList(before));
42
43         String JavaDoc[] always = new String JavaDoc[]{ "||","&&","=","?",":" };
44         alwaysSpaceBeforeAndAfter.addAll(Arrays.asList(always));
45     }
46
47     public PrettyCodeEmitter(PrintStream JavaDoc out) {
48         this.out = out;
49         this.indent = 0;
50         this.newline = true;
51     }
52
53     public void emitToken(String JavaDoc token) {
54         if (token.equals("}")) {
55             newline = true;
56             --indent;
57         }
58
59         if (newline) {
60             out.println();
61             indent();
62             newline = false;
63         } else if (alwaysSpaceBeforeAndAfter.contains(last) || alwaysSpaceBeforeAndAfter.contains(token) ||
64                     !(noSpaceAfter.contains(last) || noSpaceBefore.contains(token)))
65             out.print(" ");
66
67         out.print(token);
68
69         if (token.equals(";") || token.equals("}"))
70             newline = true;
71         else if (token.equals("{")) {
72             newline = true;
73             ++indent;
74         }
75
76         last = token;
77     }
78
79     public void finish() throws IOException JavaDoc {
80         out.println();
81         out.flush();
82     }
83
84     private void indent() {
85         for (int i = 0; i < indent; ++i)
86             out.print("\t");
87     }
88 }
89
90 // vim:ts=4
91
Popular Tags