I know this is silly but I can’t overcome my curiosity. Is it possible to write a shell script to format a piece of java code?
For example, if a user writes in a code:
public class Super{
public static void main(String[] args){
System.out.println("Hello world");
int a=0;
if(a==100)
{
System.out.println("Hello world");
}
else
{
System.out.println("Hello world with else");
}
}
}
I would like to write a shell script which would make the code like this.
public class Super
{
public static void main(String[] args)
{
System.out.println("Hello world");
int a=0;
if(a==100){
System.out.println("Hello world");
}
else{
System.out.println("Hello world with else");
}
}
To be precise, we should change the formatting of flower brackets. If it is try/catch or control structures we should change it to same line and if it is function/method/class it should come in next line.I have little knowledge about sed and awk which can do this task so easily. Also I know this can be done using eclipse.
Well, I’ve had some free time on my hands, so I decided to relive my good old linux days :]
After reading a bit about awk and sed, I’ve decided that it might be better to use both, as it is easier to add indentation in awk and parse strings in sed.
Here is the ~/sed_script that formats the source file:
# delete indentation s/^ \+//g # format lines with class s/^\(.\+class.\+\) *\({.*\)$/\1\n\2/g # format lines with methods s/^\(public\|private\)\( \+static\)\?\( \+void\)\? \+\(.\+(.*)\) *\({.*\)$/\1\2\3 \4\n\5/g # format lines with other structures /^\(if\|else\|for\|while\|case\|do\|try\)\([^{]*\)$/,+1 { # get lines not containing '{' # along with the next line /.*{.*/ d # delete the next line with '{' s/\([^{]*\)/\1 {/g # and add '{' to the first line }And here is the ~/awk_script that adds indentation:
BEGIN { depth = 0 } /}/ { depth = depth - 1 } { getPrefix(depth) print prefix $0 } /{/ { depth = depth + 1 } function getPrefix(depth) { prefix = "" for (i = 0; i < depth; i++) { prefix = prefix " "} return prefix }And you use them like that:
> sed -f ~/sed_script ~/file_to_format > ~/.tmp_sed > awk -f ~/awk_script ~/.tmp_sedIt is far from proper formatting tool, but I hope it will do OK as a sample script for reference :] Good luck with your learning.