Tuesday, April 28, 2009
Java regex tip
Friday, April 17, 2009
Parsing with JavaCC
options{
STATIC=false;
}
PARSER_BEGIN(FLW)
import java.io.*;
public class FLW{
public static void main(String ar[]){
CustomerDTO dtos = getDTO("xxxx 40,dddddddddd eeeeeeeeeer");
System.out.println("First Name: "+dtos.getFName());
System.out.println("Last Name: "+dtos.getLName());
System.out.println("Address : "+dtos.getAddress());
}
static CustomerDTO getDTO(String inString){
Reader reader = new StringReader(inString);
FLW parser = new FLW(reader);
StringBuffer buf = new StringBuffer();
try{
return parser.parse();
}
catch(Exception e){
System.out.println("exception");
e.printStackTrace();
}
return null;
}
}
PARSER_END(FLW)
TOKEN:{<SPACE:" ">}
TOKEN:{<#COMMA:",">}
TOKEN:{<FIRST_NAME:(<LETTER>){4}>}
TOKEN:{<ADDRESS:(<NUMBER>){2}(<COMMA>){1}(<LETTER>){10}>}
TOKEN:{<LAST_NAME:(<LETTER>){11}>}
TOKEN:{<#LETTER:["a"-"z","A"-"Z"]>}
TOKEN:{<#NUMBER:["0"-"9"]>}
CustomerDTO parse():
{
Token fName;
Token lName;
Token address;
CustomerDTO cusDTO = new CustomerDTO();
}
{
(
((fName=<FIRST_NAME>
{cusDTO.setFName(fName.image);}))
<SPACE>
(address=<ADDRESS>)
{cusDTO.setAddress(address.image);}
<SPACE>
(lName=<LAST_NAME>)
{cusDTO.setLName(lName.image);}
)
{return cusDTO;}
}
Note that you have to first install JavaCC and also create the CustomerDTO which is in the default class path in the above example and store this in a file named xxx.jj. What the above code does is basically break down the string message passed in the main method and put the relevant data in the relvant attributes of the DTO. Note that JavaCC automatically hanldes EOF(End of file). Hope this helps anyone who is looking at how to use JavaCC for such scenario.
Wednesday, April 8, 2009
JFreeChart For The Web Cont.....
JFreeChart chart = ChartFactory.createBarChart(chartTitle,
xAxisName, yAxisName, dataSet, PlotOrientation.
VERTICAL, false, true, false);
OutputStream outStream = response.getOutputStream();
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ChartUtilities.writeChartAsPNG(byteArray, chart, 800, 500);
byte[] byteStream = null;
byteStream = byteArray.toByteArray();
response.setContentType("image/png");
response.setContentLength((int) byteStream.length);
response
.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
outStream.write(byteStream);
outStream.flush();
outStream.close();
Tuesday, April 7, 2009
JFreeChart For The Web
Ever needed to create a JFreeChart and display it in your web application?Well people i was in need of the same and after searching in vain i finally was able to put together a solution and thought i should share it with you all which will save you a considerable amount of development time.
1. Create a jsp xxx.jsp and include the following line within the jsp;
<img src="xxx.jsp" alt="Progress chart" />
2. Then you need to write your servlet code. Include the following code snippet in your servlet which will
create a temp image file in your server and use that to stream it to your response. Note that i have created a
simple bar chart in the following code snippet,but you can change it with what ever chart that you are
working with.
public
static void createBarChart(String chartTitle, String xAxisName, String yAxisName, DefaultCategoryDataset dataSet,HttpServletRequest request, HttpServletResponse response) {
try { // NOTE : I have filled in the Dataset with a DefaultCategoryDataset // data setJFreeChart chart = ChartFactory.createBarChart(chartTitle,
xAxisName, yAxisName, dataSet, PlotOrientation.
VERTICAL, false, true, false);File image = File.createTempFile(
"image", "tmp");ChartUtilities.saveChartAsPNG(image, chart, 500, 300);
FileInputStream fileInStream =
new FileInputStream(image);OutputStream outStream = response.getOutputStream();
long fileLength; byte[] byteStream;fileLength = image.length();
byteStream =
new byte[(int) fileLength];fileInStream.read(byteStream, 0, (
int) fileLength);response.setContentType(
"image/png");response.setContentLength((
int) fileLength);response
.setHeader(
"Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");response.setHeader(
"Pragma", "no-cache");fileInStream.close();
outStream.write(byteStream);
outStream.flush();
outStream.close();
}
catch (IOException e) {}
}
Thats it guys. Include the following in your servlet code and your good to go with JFreeChartsin you web application. Hope this helps.
Friday, April 3, 2009
EJB 3 Slight Perfomance Increasing tips
String builder/buffer performance
Hibernate load vs get
Simple Select SQL Tips
Oracle Outer Join Simplified
Google goes black
Check it out @ www.blackle.com
Thursday, April 2, 2009
Continuous File Reading In Java
private static void continuousFileReader(String fileName) {
String appFileName = fileName+"/fileName.txt";
File file = new File(appFileName);
if(!(file.exists())){
System.out.println("Relevant File Does Not Exist At " +fileName+" Hence Exiting System");
System.exit(1);
}
long lengthBefore = 0;
long length = 0;
while(true){
RandomAccessFile reader = null;
try {
if ((length = file.length()) > lengthBefore) {
try {
reader = new RandomAccessFile(file,"r");
reader.seek(lengthBefore);
lengthBefore = length;
String line = null;
while (!((line = reader.readLine()) == null)) {
// do whatever with contents
}
} catch (FileNotFoundException ex) {
//handle exception
}
catch (IOException ex) {
//handle exception
}
}
Thread.sleep(10000);
} catch (InterruptedException ex) {
try {
if(reader!=null){
reader.close();
}
} catch (IOException ex1) {
//handle exception
}
}
}
}
And one more thing to note is to remember to block the main thread using a while(true) condition because otherwise the continuous file reading will end if the main thread halts ;) .
This is my very first post and my first time blogging so if you guys see any mistakes, constructive criticisms or what ever you might call it please feel free to share your thoughts :D.
Until then, its Adieu from my side.