martes, 20 de octubre de 2009

Source code formatter library for Java

Recently I was looking for a Java Library that could be include in an applet (with no any dependencies), after looking for awhile I could not find any library (there are a few out there but all of them have dependencies with other libraries). 

I found a C++ program named Artistic Style (http://astyle.sourceforge.net/) , so I decided to migrate the source code from C++ to Java, and the jastyle project arises (http://sourceforge.net/projects/jastyle/).

There are two ways in which jastyle could be used, as a library or as a console application. See how easy is to format Java source code with jastyle.

First, go to (http://sourceforge.net/projects/jastyle/) and download the lastest jastyle.jar , then choose the Java source file that you want to give format. The options will be shown if you just type:

java -jar jastyle.jar 

Now let's see how It works with an unformatted java source code:




package net.barenca.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Random;

import oracle.jdbc.OracleDriver;

public class OracleBlobTest{
public static void main(String[] args) throws Exception
{
String filename="/home/barenca/Documents/LDAP.pdf";
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream inputStream = new BufferedInputStream(fis);

int length = inputStream.available();
DriverManager.registerDriver(new OracleDriver());
String url="jdbc:oracle:thin:@192.168.1.253:1521:devdb";
Connection conn = DriverManager.getConnection(url
, "prueba",
"prueba");
String insert = "insert into test_blob(id,content)values(?,?)";

PreparedStatement pst = conn.prepareStatement(insert);
int x = new Random(new Date().getTime()).nextInt(100000);

pst.setInt(1, x);
pst.setBinaryStream(2, inputStream, length);
pst.execute();
pst.close();

conn.close();
inputStream.close();
}}

And after java -jar jastyle.jar --style=java OracleBlobTest.java this is the result:



package net.barenca.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Random;
import oracle.jdbc.OracleDriver;
public class OracleBlobTest {
public static void main(String[] args) throws Exception {
String filename ="/home/barenca/Documents/LDAP.pdf";
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream inputStream = new BufferedInputStream(fis);
int length = inputStream.available();
DriverManager.registerDriver(new OracleDriver());
String url ="jdbc:oracle:thin:@192.168.1.253:1521:devdb";
Connection conn = DriverManager.getConnection(url
, "prueba","prueba");
String insert = "insert into test_blob(id,content)values(?,?)";
PreparedStatement pst = conn.prepareStatement(insert);
int x = new Random(new Date().getTime()).nextInt(100000);
pst.setInt(1, x);
pst.setBinaryStream(2, inputStream, length);
pst.execute();
pst.close();
conn.close();
inputStream.close();
}
}

I think is not perfect, but It helps a lot :) .

Now, as a library :



import java.io.Reader;
import java.io.FileReader;
import java.io.BufferedReader;
import net.barenca.jastyle.ASFormatter;
import net.barenca.jastyle.FormatterHelper;

public static void main(String[] args) throws Exception
{

ASFormatter formatter = new ASFormatter();
formatter.setJavaStyle();
Reader in = new BufferedReader(new FileReader("OracleBlobTest.java"));

String formatted = FormatterHelper.format(in,formatter);

// and that's it!
}


The best thing is that this library does not need any additional jar library!!!.


I hope you enjoy it!.


Best regards.

Héctor.


3 comentarios:

Anónimo dijo...

i tried this lib with this code:

public static void main(String[] args) {
try {
ASFormatter formatter = new ASFormatter();
Reader in = new BufferedReader(new StringReader("public class HelloWord{ public static void main(String[] args){System.out.println(\"hello world\");}}"));

formatter.setJavaStyle();

String formatted = FormatterHelper.format(in,formatter);

System.out.println(formatted);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

but it returned me an Exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.charAt(StringBuffer.java:162)
at net.barenca.jastyle.ASFormatter.isNextCharOpeningBracket(ASFormatter.java:2127)
at net.barenca.jastyle.ASFormatter.nextLine(ASFormatter.java:1238)
at net.barenca.jastyle.FormatterHelper.format(FormatterHelper.java:52)

how can i solve this? :\

Anónimo dijo...

if i give \n after { on "void main(String[] args){System.out.println(\"hello world\");}}"));"

it works fine.

hamed mirzaei dijo...

Hi,
It works fine but it has some bugs that makes it a little bad:
1. it converts "==" to "= =".
2. It will be very good if it can insert "{" and "}" in places where it is missed. for example converts "if(a==b) c=1;" to "if(a==b){ c=1;}"

Thanks,
Hamed Mirzaei