CodeFormatting.java 5.26 KB
   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
package jcircus.util;

/*
* CodeFormatting.java
*
* Formats an unformatted code in order to make it more readable
* Created on 15 March 2005, 13:53
* @author Marcel Oliveira
*/

import java.util.StringTokenizer;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CodeFormatting {
public static String SEQUENCE = ";";
public static String OPEN_BLOCK = "{";
public static String CLOSE_BLOCK = "}";
public static String LINE_BREAK = "\n";
public static String TAB = "\t";
/*
* Insert a given string after a given symbol
*/
private static String insertString(String input, String insertString, String afterString){
String output = "";
StringTokenizer tokenizer = new StringTokenizer(input,afterString,true);
while(tokenizer.hasMoreTokens()){
String next = tokenizer.nextToken();
if(next.equals(afterString)){
output = output + next + insertString;
} else {
output = output + next.trim();
}
}
return output;
}

/*
* Insert line breaks after a given symbol
*/
private static String insertLineBreaks(String input, String afterString){
return insertString(input, LINE_BREAK, afterString);
}
/*
* Counts how many times c happens in str
*/
private static int count(char c, String str) {
int count = 0;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == c) {
count++;
}
}
return count;
}
/*
* Returns a string containing n characters c
*/
private static String repeat(char c, int n) {
String output = "";
for(int i=0; i<n; i++){
output = output + c;
}
return output;
}

/*
* Insert tabs after a given symbol
*/
private static String insertTabs(String input){
String output = "";
String done = "";
String toDo = "";
StringTokenizer tokenizer = new StringTokenizer(input,LINE_BREAK,true);
while(tokenizer.hasMoreTokens()){
String next = tokenizer.nextToken();
done = done + next;
if(next.equals(LINE_BREAK)){
int numberOfOpenBlocks = count(OPEN_BLOCK.charAt(0),done);
int numberOfCloseBlocks = count(CLOSE_BLOCK.charAt(0),done);
int numberOfTabs = numberOfOpenBlocks - numberOfCloseBlocks;
output = output + next + repeat(TAB.charAt(0),numberOfTabs);
} else {
output = output + next.trim();
}
}
// Removing extra tabs that were inserted before closing blocks
output = output.replaceAll(TAB+CLOSE_BLOCK,CLOSE_BLOCK);
return output;
/*
String correctOutput = "";
tokenizer = new StringTokenizer(output,CLOSE_BLOCK,true);
while(tokenizer.hasMoreTokens()){
String next = tokenizer.nextToken();
if(next.equals(CLOSE_BLOCK)){
correctOutput = correctOutput + next;
} else {
correctOutput = correctOutput + next.substring(0,next.length()-1);
}
}
return correctOutput;
*/
}

public static String format(String input){
String output = input;
if (input != null) {
output = output.replaceAll(LINE_BREAK,"");
output = insertLineBreaks(output,OPEN_BLOCK);
output = insertLineBreaks(output,CLOSE_BLOCK);
output = insertLineBreaks(output,SEQUENCE);
output = insertTabs(output);
}
return output;
}

public static void main(String args[]){
String input = "";
BufferedReader reader = null;
String line;
try {
reader = new BufferedReader(new FileReader(new File("source.txt")));
line = reader.readLine();
while (line != null) {
input = input + line + CodeFormatting.LINE_BREAK;
line = reader.readLine();
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
String output = CodeFormatting.format(input);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(new File("target.txt")));
writer.write(output);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
}
}