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.