-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prettify.java
195 lines (175 loc) · 5.06 KB
/
Prettify.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.PrintWriter;
public class Prettify {
public static void main(String[] args) {
FileSuite fs = new FileSuite();
ArrayList<PrettifyToken> ts = fs.readFile(args[0]);
System.out.println("Converting File: " + args[0]);
if(ts != null){
System.out.println("Starting to parse curly brackets...");
ts = (new CurlyReducer(ts)).reduce();
System.out.println("Starting to reduce white space...");
ts = (new WhiteSpaceReducer(ts)).reduce();
System.out.println("Starting to inject whitespace metadata...");
ts = (new IndentReducer(ts)).reduce();
System.out.println("Starting to convet token array to string...");
String s = (new Interpreter(ts)).interpretTokens();
fs.createFile((args[0]).substring(0,args[0].length()-5)+"_converted.java",s);
System.out.println("Converting Done.");
}
}
}
class FileSuite {
public FileSuite() {
}
public ArrayList<PrettifyToken> readFile(String filename) {
File file = new File(filename);
try {
Scanner sc = new Scanner(file);
ArrayList<PrettifyToken> fileContent = new ArrayList<PrettifyToken>();
while (sc.hasNextLine()) {
fileContent.add(new PrettifyToken(sc.nextLine()));
}
sc.close();
return fileContent;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public void createFile(String filename, String content) {
try{
PrintWriter writer = new PrintWriter(filename, "UTF-8");
writer.println(content);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Token {
private String rawContent;
public Token(String ct) {
rawContent = ct;
}
public String getRawContent() {
return rawContent;
}
}
class PrettifyToken extends Token {
private int indentCt;
public PrettifyToken(String ct) {
super(ct);
indentCt = 0;
}
public void setIndentCt(int i) {
indentCt = i;
}
public int getIndentCt() {
return indentCt;
}
}
abstract class TokenReducer {
abstract ArrayList<PrettifyToken> reduce();
}
class CurlyReducer extends TokenReducer {
private ArrayList<PrettifyToken> content;
private ArrayList<PrettifyToken> newContent;
public CurlyReducer(ArrayList<PrettifyToken> ct) {
content = ct;
newContent = new ArrayList<PrettifyToken>();
}
public ArrayList<PrettifyToken> curlyInspect(PrettifyToken t) {
ArrayList<PrettifyToken> newTokens = new ArrayList<PrettifyToken>();
String raw = t.getRawContent();
String newRaw = "";
for(char c : raw.toCharArray()) {
if(c=='}' || c == '{') {
newTokens.add(new PrettifyToken(newRaw));
newTokens.add(new PrettifyToken(c+""));
newRaw = "";
} else {
newRaw += c;
}
}
if(newRaw != "")
newTokens.add(new PrettifyToken(newRaw));
return newTokens;
}
public ArrayList<PrettifyToken> reduce(){
for(PrettifyToken t:content){
newContent.addAll(curlyInspect(t));
}
return newContent;
}
}
class IndentReducer extends TokenReducer {
private ArrayList<PrettifyToken> content;
private ArrayList<PrettifyToken> newContent;
private int globalIndent;
public IndentReducer(ArrayList<PrettifyToken> ct) {
content = ct;
globalIndent = 0;
newContent = new ArrayList<PrettifyToken>();
}
public PrettifyToken indentInspector(PrettifyToken t) {
PrettifyToken newToken = new PrettifyToken(t.getRawContent());
newToken.setIndentCt(globalIndent);
if(t.getRawContent().equals("}")) {
globalIndent--;
newToken.setIndentCt(globalIndent);
}
if(t.getRawContent().equals("{"))
globalIndent++;
return newToken;
}
public ArrayList<PrettifyToken> reduce(){
for(PrettifyToken t:content){
newContent.add(indentInspector(t));
}
return newContent;
}
}
class WhiteSpaceReducer extends TokenReducer {
private ArrayList<PrettifyToken> content;
private ArrayList<PrettifyToken> newContent;
private int globalIndent;
public WhiteSpaceReducer(ArrayList<PrettifyToken> ct) {
content = ct;
newContent = new ArrayList<PrettifyToken>();
}
public ArrayList<PrettifyToken> reduce(){
for(PrettifyToken t:content){
String newRaw = t.getRawContent().trim();
if(!newRaw.equals(""))
newContent.add(new PrettifyToken(newRaw));
}
return newContent;
}
}
class Interpreter {
private String finalString;
private ArrayList<PrettifyToken> initTokens;
public Interpreter(ArrayList<PrettifyToken> tokens) {
initTokens = tokens;
finalString = "";
}
public String tokenToString(PrettifyToken t) {
String s = "";
for(int i = 0; i<t.getIndentCt();i++)
s+="\t";
s+=t.getRawContent();
s+="\n";
return s;
}
public String interpretTokens() {
for(PrettifyToken t : initTokens) {
finalString += tokenToString(t);
}
return finalString;
}
}