Here is a first lousy attempt at it kicking off the effort to create a community sourcecode compressor for Java :point:
[icode]LousySourcecodeCompressor.java[/icode]
import java.io.*;
import java.util.regex.*;
public class LousySourcecodeCompressor {
public static void main(String[] args) throws Exception {
File file = new File(args[0]);
int origLength = 0;
StringBuilder trimmed = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while (true) {
String line = br.readLine();
if (line == null)
break; // reached end of file
origLength += line.length() + 1; // +1 for line-break
line = line.trim(); // strip optional whitespace
if (line.isEmpty())
continue; // strip empty lines
trimmed.append(line).append('\n');
}
String code = trimmed.toString();
// make a lousy attempt at stripping comments
code = Pattern.compile("//[a-zA-Z\\s]+$", Pattern.MULTILINE).matcher(code).replaceAll("");
// make a lousy attempt at stripping annotations
code = Pattern.compile("^@[a-zA-Z]+$", Pattern.MULTILINE).matcher(code).replaceAll("");
// make a lousy attempt at stripping optional whitespace
code = code.replaceAll("\\s*([\\+\\-\\*/%,\\(\\)\\{\\}\\[\\]=;:<>!])\\s*", "$1");
System.out.println(code.length() + "/" + origLength);
System.out.println();
System.out.println(code);
}
}
which takes OddEntry.java and produces the following (N.B.: newlines inserted by me)
853/1189
package net.indiespot.java4k.entries;import java.awt.*;import net.indiespot.java4k.Java4kRev1;public
class OddEntry extends Java4kRev1{public OddEntry(){name="Odd Entry";}public void render(Graphics2D
g){g.setColor(new Color(128,64,128));g.drawString("Drag the mouse a little...",8,20);g.setColor(new
Color(0,64,128));int b,r,a,c,q,t;for(r=150;r>=30;r-=15){t=((elapsed()+130_000)/((200-r)/25));b=r/3;
a=(w-r)/2;c=(h-r)/2;q=(int)(t%(r+b));g.drawLine(a+Math.max(0,q-b),c,a+Math.min(r,q),c);
q=(r+b)-(int)((t+r)%(r+b));g.drawLine(a,c+Math.max(0,q-b),a,c+Math.min(r,q));
q=(r+b)-(int)((t+r-b)%(r+b));g.drawLine(a+Math.max(0,q-b),c+r,a+Math.min(r,q),c+r);
q=(int)((t-r)%(r+b));g.drawLine(a+r,c+Math.max(0,q-b),a+r,c+Math.min(r,q));}if(mouse.dragArea
!=null){g.setColor(new Color(128,64,128));Rectangle w=mouse.dragArea;g.drawRect(w.x,w.y,w.width,w.height);}}}
Note that it does not yet respect string literals, which makes it borderline useless