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.


jueves, 15 de octubre de 2009

Mapping Windows Key (Super) in Ubuntu

Probably one of the functions that a Windows user is accustom to is the mapping of the Windows key + E (this opens the Explorer -not the Internet Explorer), this combination of key mapping is not enabled by default in Ubuntu (I have tested it in Ubuntu 9.04, XUbuntu 8.10 and 9.04).

So, let me show you how easy is to map the windows key + E in order to have the same behavior in Ubuntu as Windows does it.

First go to option menu System -->Preferences-->Keyboard and then choose the tab "Layouts" and then "Layout options", and go to the Alt/Win key behavior subtree, open it and click on the radio button Supper key is mapped to Win keys and then click on close (or accept) and click on the another close button.




Second, go to System --> Preferences --> Keyboard Shortcuts and look for the Desktop section and click on the Home folder row, now press at the same time the two keys Windows + E (like in Windows - plus symbol should not be pressed), once you have done it, the combination of keys (the shortcut) to open Nautilus is ready!, accept the change.



Now continue mapping the keys you are accustom to. I hope this short description is useful for you.


Best regards.




lunes, 22 de junio de 2009

Error: No JDK found on PATH, Please correct the SetJavaHome directive or add the directive

If you have seen the following error while trying to execute Oracle SQL Developer:

Error: No JDK found on PATH
Please correct the SetJavaHome directive or add the directive
in /home/barenca/bin/sqldeveloper/sqldeveloper/bin/sqldeveloper.conf or
sqldeveloper-Linux.conf
to point to the JDK installation.

You just need to indicate the J2SDK path in the file sqldeveloper/bin/sqldeveloper.conf and that's it!.

Edit the file ./sqldeveloper/bin/sqldeveloper.conf and add the path of your j2sdk installation (in Linux/unix which java and then follow the symbolic links):

SetJavaHome /usr/lib/jvm/java-6-sun

And that's it!, enjoy it.

jueves, 18 de junio de 2009

Sound Volume keys in IceWM (tested with Latitude D620)

Some days ago I took sometime and I installed Antix 8, not everything is according with the features I was waiting for, but It is OK.

One thing It was missing was the configuration for the Sound Volume keys, so this is what I did for enabling the keys on my Laptop.

Go to the Antix Control Center, click on Edit IceWM settings, click on keys tab and append the following to the file:

key "XF86AudioLowerVolume" amixer -q set Master playback 5-

key "XF86AudioRaiseVolume" amixer -q set Master playback 5+

key "XF86AudioMute" amixer sset Master toggle

Then save the file.

Now click on the statup tab and append the following lines:

xmodmap -e 'keycode 176 = XF86AudioRaiseVolume'

xmodmap -e 'keycode 174 = XF86AudioLowerVolume'

xmodmap -e 'keycode 160 = XF86AudioMute'

Then save the file.

Close the session and It's done!.