Monday, March 17, 2008

Generate Excel Reports with JXL.jar

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class ExcelReport {

public void GenerateReport(HashMap hmHdrs, Vector vecData)throws Exception {

WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
WritableSheet sheet = workbook.createSheet("WRMS Report", 0);
WritableFont arialbld=new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, true);
WritableFont arialfnt = new WritableFont(WritableFont.ARIAL, 10);

Vector keyset=new Vector();
Iterator itr=hmHdrs.keySet().iterator();
int i=0;
WritableCellFormat exelwrtfrmt = new WritableCellFormat (arialbld);
while (itr.hasNext()) {
keyset.add(itr.next().toString());
}

int recs=vecData.size();
int cols=keyset.size();
for(int index=0;index<cols;index++){
String value=hmHdrs.get(keyset.get(index).toString()).toString();
Label labl = new Label(index,0, value, exelwrtfrmt);
sheet.addCell(labl);
}
exelwrtfrmt = new WritableCellFormat (arialfnt);
for(int indx=0;indx<recs;indx++){
HashMap rec=(HashMap)vecData.get(indx);
for(int index=0;index<cols;index++){
String value=rec.get(keyset.get(index).toString()).toString();
Label labl = new Label(index,indx+2, value, exelwrtfrmt);
sheet.addCell(labl);
}
}
workbook.write();
workbook.close();
}
}

To download through JSP use
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=sampleName.xls");
WritableWorkbook w = Workbook.createWorkbook(response.getOutputStream());

Thursday, September 13, 2007

Invoke JS functions through J2SE6.0

The The Java SE 6 installation comes with sample applications to demonstrate many of the release's new features. One of these samples, ScriptPad, is a 99 percent JavaScript application that loads and executes other scripts through the Java scripting API. It uses Java to load the Rhino JavaScript engine and execute the five script files that are built into ScriptPad. Included in the sample/scripting/scriptpad folder within your Java SE 6 installation root folder, ScriptPad comes complete with a NetBeans 5.5 project file, which you can use to easily build and execute it.

You can also use the application (through the Java Scripting API) to run other scripts that you've written.For instance, the script shown within the ScriptPad window in Figure 1 accesses Java's static System object, invokes its method (getProperties), uses the resulting Properties object to get the name of the host operating system, and outputs the result in an alert window.

This is a sample code to combine the scripting and Java. It demonstrate how to integration really is.The following Java code loads the Rhino JavaScript engine, loads the script contained within the file browse.js, and executes the function named browse

Code:

import java.util.*;
import java.io.*;
import javax.script.*;

public class Main
{
public Main()
{
try {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine engine = m.getEngineByName("javascript");
if ( engine != null )
{
InputStream is = this.getClass().getResourceAsStream("browse.js");
Reader reader = new InputStreamReader(is);
engine.eval(reader);
Invocable invocableEngine = (Invocable)engine;
invocableEngine.invokeFunction("browse");
}
}
catch ( Exception e ) {
e.printStackTrace();
}
}

public static void main(String[] args)
{
Main m = new Main();
}
}

This browse.js functions uses new Swing Desktop API in JDK 6. To use this with scriptpad, open this in scriptpad and use "Tools->Run Script" menu.

browse.js

function browse() {
var guiPkgs = new JavaImporter(java.awt, java.awt.event,
javax.swing, javax.swing.undo,
javax.swing.event, javax.swing.text);
with (guiPkgs) {
var desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
else
{
alert("no desktop support");
return;
}
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(new java.net.URI("www.ericbruno.com"));
}
else
{
alert("no browser support");
}
}
}
if (this.application != undefined) {
// add "Browse" menu item under "Tools" menu
this.application.addTool("Browse", browse);
}

This paradoxical example where Java invokes a script that in turn invokes Java to load an HTML page demonstrates just how dynamic the scripting API can be.

Friday, September 7, 2007

Object Serialization with XMLEncoder

Java API provides XMLEncoder class as an alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. For example, the following fragment can be used to create a textual representation the supplied JavaBean and all its properties.

XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("Test.xml")));
e.writeObject(new JButton("Hello, world"));
e.close();

The XMLEncoder class provides a default denotation for JavaBeans in which they are represented as XML documents complying with version 1.0 of the XML specification and the UTF-8 character encoding of the Unicode/ISO 10646 character set.

The XML syntax uses the following conventions:

  • Each element represents a method call.
  • The "object" tag denotes an expression whose value is to be used as the argument to the enclosing element.
  • The "void" tag denotes a statement which will be executed, but whose result will not be used as an argument to the enclosing method.
  • Elements which contain elements use those elements as arguments, unless they have the tag: "void".
  • The name of the method is denoted by the "method" attribute.
  • XML's standard "id" and "idref" attributes are used to make references to previous expressions - so as to deal with circularities in the object graph.
  • The "class" attribute is used to specify the target of a static method or constructor explicitly; its value being the fully qualified name of the class.
  • Elements with the "void" tag are executed using the outer context as the target if no target is defined by a "class" attribute.
  • Java's String class is treated specially and is written Hello, world where the characters of the string are converted to bytes using the UTF-8 character encoding.


The below code is a sample student bean class to demonstrate the Usage of XMLEncoder and XMLDecoder

Code:

import java.util.Date;

public class Student
{
private String name;
private int age;
private int [] marks_M_P_C;
private String fatherName;
private int rollNumber;
private Date joinDate;

public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public int [] getMarks_M_P_C() {
return marks_M_P_C;
}
public void setMarks_M_P_C(int[] marks_M_P_C) {
this.marks_M_P_C = marks_M_P_C;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}

public double getAvg(){
return (double)(this.getTotal()/3.0);
}

public double getTotal(){
return (double)(this.marks_M_P_C[0]+this.marks_M_P_C[1]+this.marks_M_P_C[2]);
}

public String toString() {
return getClass().getName() +
"[Marks=" + asString(marks_M_P_C) +
",avg=" + getAvg() +
",name=" + name +
",fatherName=" + fatherName +
"rollNumber" + rollNumber +
"age" + age + "]";
}

private String asString(int[] array) {
StringBuffer buffer = new StringBuffer("[");
for (int i=0, n=array.length; i < n; i++) {
if (i != 0) {
buffer.append(",");
}
buffer.append(array[i]);
}
buffer.append("]");
return buffer.toString();
}
}

The below code demonstrate the usage of XmlEncoder to store the object as well as XMLDecoder to Retrieve the object
Code:

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

public class XmlObjTest {
public static void main (String args[]) throws Exception {
Student student = new Student();
student.setMarks_M_P_C(new int[] {146, 52, 53});
student.setName("Hari");
student.setJoinDate(new Date());
student.setRollNumber(1219);
student.setFatherName("Rao");
student.setAge(19);
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("student.xml")));
encoder.writeObject(student);
encoder.close();

System.out.println(student);

XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream("student.xml")));
Student studentR = (Student)decoder.readObject();
decoder.close();
System.out.println(studentR);

}
}

The below show how an Object will be stored

Code: (student.xml)

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_08" class="java.beans.XMLDecoder">
<object class="temp.Student">
<void property="age">
<int>19</int>
</void>
<void property="fatherName">
<string>Rao</string>
</void>
<void property="joinDate">
<object class="java.util.Date">
<long>1189158030890</long>
</object>
</void>
<void property="marks_M_P_C">
<array class="int" length="3">
<void index="0">
<int>146</int>
</void>
<void index="1">
<int>52</int>
</void>
<void index="2">
<int>53</int>
</void>
</array>
</void>
<void
property="name">
<string>Hari</string>
</void>
<void
property="rollNumber">
<int>
1219</int>
</void>
</object>
</java>

Thursday, September 6, 2007

Cookies Handling through Java

What are cookies?
Technically, cookies are arbitrary pieces of data chosen by the Web server and sent to the browser. The browser returns them unchanged to the server, introducing a state (memory of previous events) into otherwise stateless HTTP transactions.

The below code explain how to handle cookies while working with java.net.URL and java.net.URLConnection objects

Code:

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;


public class CookieHandler {

private Map repository;

private static final String SET_COOKIE = "Set-Cookie";
private static final String COOKIE_VALUE_DELIMITER = ";";
private static final String PATH = "path";
private static final String EXPIRES = "expires";
private static final String DATE_FORMAT = "EEE, dd-MMM-yyyy hh:mm:ss z";
private static final String SET_COOKIE_SEPARATOR="; ";
private static final String COOKIE = "Cookie";

private static final char NAME_VALUE_SEPARATOR = '=';
private static final char DOT = '.';

private DateFormat dateFormat;

public CookieHandler() {
repository = new HashMap();
dateFormat = new SimpleDateFormat(DATE_FORMAT);
}

/**
* Retrieves and cookies stored in repository
* @param conn a opend URLConnection
* @throws java.io.IOException Thrown if conn is not open.
*/
public void storeCookies(URLConnection conn) throws IOException {
String domain = getDomainFromHost(conn.getURL().getHost());
Map domainStore;
if (repository.containsKey(domain)) {
domainStore = (Map)repository.get(domain);
} else {
domainStore = new HashMap();
repository.put(domain, domainStore);
}

String headerName=null;
for (int i=1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equalsIgnoreCase(SET_COOKIE)) {
Map cookie = new HashMap();
StringTokenizer st = new StringTokenizer(conn.getHeaderField(i), COOKIE_VALUE_DELIMITER);

if (st.hasMoreTokens()) {
String token = st.nextToken();
String name = token.substring(0, token.indexOf(NAME_VALUE_SEPARATOR));
String value = token.substring(token.indexOf(NAME_VALUE_SEPARATOR) + 1, token.length());
domainStore.put(name, cookie);
cookie.put(name, value);
}

while (st.hasMoreTokens()) {
String token = st.nextToken();
cookie.put(token.substring(0, token.indexOf(NAME_VALUE_SEPARATOR)).toLowerCase(), token.substring(token.indexOf(NAME_VALUE_SEPARATOR) + 1, token.length()));
}
}
}
}
/**
* Prior to opening a URLConnection, calling this method will set all
* unexpired cookies that match the path or subpaths for thi underlying URL
*
* The connection MUST NOT have been opened
* method or an IOException will be thrown.
*
* @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown
* @throws java.io.IOException Thrown if conn has already been opened.
*/
public void setCookies(URLConnection conn) throws IOException {
URL url = conn.getURL();
String domain = getDomainFromHost(url.getHost());
String path = url.getPath();

Map domainStore = (Map)repository.get(domain);
if (domainStore == null) return;
StringBuffer cookieStringBuffer = new StringBuffer();

Iterator cookieNames = domainStore.keySet().iterator();
while(cookieNames.hasNext()) {
String cookieName = (String)cookieNames.next();
Map cookie = (Map)domainStore.get(cookieName);
if (comparePaths((String)cookie.get(PATH), path) && isNotExpired((String)cookie.get(EXPIRES))) {
cookieStringBuffer.append(cookieName);
cookieStringBuffer.append("=");
cookieStringBuffer.append((String)cookie.get(cookieName));
if (cookieNames.hasNext()) cookieStringBuffer.append(SET_COOKIE_SEPARATOR);
}
}
try {
conn.setRequestProperty(COOKIE, cookieStringBuffer.toString());
} catch (java.lang.IllegalStateException ise) {
IOException ioe = new IOException("Illegal State! Cookies cannot be set on a URLConnection that is already connected. Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect().");
throw ioe;
}
}

private String getDomainFromHost(String host) {
if (host.indexOf(DOT) != host.lastIndexOf(DOT)) {
return host.substring(host.indexOf(DOT) + 1);
} else {
return host;
}
}

private boolean isNotExpired(String cookieExpires) {
if (cookieExpires == null)
return true;
Date now = new Date();
try {
return (now.compareTo(dateFormat.parse(cookieExpires))) <= 0; } catch (java.text.ParseException pe) {
pe.printStackTrace(); return false;
}
}

private boolean comparePaths(String cookiePath, String targetPath) {
if (cookiePath == null) {
return true;
}
else if (cookiePath.equals("/")) {
return true;
}
else if
(targetPath.regionMatches(0, cookiePath, 0, cookiePath.length())) {
return true;
}
else {
return false;
}
}
}

Code: (To test)

public static void main(String[] args) {
CookieHandler cookieHandler = new CookieHandler();
try {
URL url = new URL("http://localhost:8080/test/mycookies.jsp");
URLConnection conn = url.openConnection();
conn.connect();
cookieHandler.storeCookies(conn);
System.out.println(cm);
cookieHandler.setCookies(url.openConnection());
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}

The below Code explains how to set and Retrive the cookies

Code: (mycookies.jsp)

<%@ page import="java.util.*"%>
<%
Cookie myCookie = new Cookie("LoginId", "100");
myCookie.setMaxAge(1234);
response.addCookie(myCookie);
out.println("cookies:<br>");
Cookie[] cookies = request.getCookies();
for (int i = 0; i < style="color: rgb(51, 51, 255);">" " + cookies[i].getValue() + "<br>");
}
out.println("<br/><br/>");
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerNameStr = (String)headerNames.nextElement();
Enumeration headerValues = request.getHeaders(headerNameStr);
while(headerValues.hasMoreElements()) {
out.println(headerNameStr + ": " + headerValues.nextElement() + "<br>");
}
}
%>

Wednesday, September 5, 2007

Launch Web Browser through Java

Some Times Java applications need to present the user with an online resource such as a help tutorial or an interactive web site. web browsers such as Netscape or Internet Explorer have additional advanced capabilities such as supporting HTML scripting languages and various multimedia formats. This tutorial describes how to launch the user's default browser from your Java application.

Code:

import java.lang.reflect.Method;

public class BrowserControl{
/**
* Method to Open the Broser with Given URL
* @param url
*/
public static void openUrl(String url){
String os = System.getProperty("os.name");
Runtime runtime=Runtime.getRuntime();
try{
// Block for Windows Platform
if (os.startsWith("Windows")){
String cmd = "rundll32 url.dll,FileProtocolHandler "+ url;
Process p = runtime.exec(cmd);
}
//Block for Mac OS
else if(os.startsWith("Mac OS")){
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class});
openURL.invoke(null, new Object[] {url});
}
//Block for UNIX Platform
else {
String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < style="color: rgb(153, 0, 0);">length && browser == null; count++)
if (runtime.exec(new String[] {"which", browsers[count]}).waitFor() == 0)
browser = browsers[count];
if (browser == null)
throw new Exception("Could not find web browser");
else
runtime.exec(new String[] {browser, url});
}
}catch(Exception x){
System.err.println("Exception occurd while invoking Browser!");
x.printStackTrace();
}
}
}

Test the Code:

public static void main(String[] args){
openUrl("http://javaxden.blogspot.com");
}

Tuesday, September 4, 2007

Mapping XML- JavaBean

XML makes data portable. The Java platform makes code portable. The Java APIs for XML make it easy to use XML. Put these together, and you have the perfect combination: portability of data, portability of code, and ease of use. In fact, with the Java APIs for XML, you can get the benefits of XML with little or no direct use of XML.The portability and extensibility of both Java and XML make them ideal choices for the flexibility and wide availability requirements of Web applications and services.

The BeanXMLMapping component converts a JavaBean to an XML document and vice versa. By using JavaBean introspection, XML parsers, and DOM APIs, you can develop this component with a bean2Xml() method to represent the received bean as an XML document and a xml2Bean() method to instantiate and populate the proper bean according to the XML document received.

The below code shows a possible implementation for the BeanXMLMapping component. This particular implementation usesthe JOX (Java Objects in XML) library.

Download Dependencies:

jox116.jar
dtdparser121.jar

Code:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.wutka.jox.JOXBeanInputStream;
import com.wutka.jox.JOXBeanOutputStream;

public class BeanXMLMapping {

/**
* Method to generate the Bean object for the given xml String
*/
public static Object xml2Bean(String xml, Class className)
{
ByteArrayInputStream xmlData = new ByteArrayInputStream(xml.getBytes());
JOXBeanInputStream joxIn = new JOXBeanInputStream(xmlData);

try{
return (Object) joxIn.readObject(className);
} catch (IOException exc)
{
exc.printStackTrace();
return null;
}
finally{
try{
xmlData.close();
joxIn.close();
} catch (Exception e){
e.printStackTrace();
}
}

}


/**
* Method to generate the XML document String for the received bean
*/
public static String bean2Xml(Object bean)
{
ByteArrayOutputStream xmlData = new ByteArrayOutputStream();
JOXBeanOutputStream joxOut = new JOXBeanOutputStream(xmlData);
try{
joxOut.writeObject(beanName(bean), bean);
return xmlData.toString();
}
catch
(IOException exc){
exc.printStackTrace();
return null;
}
finally{
try{
xmlData.close();
joxOut.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}

/**
* Method to retrieve the Class name for the given bean object
*/
private static String beanName(Object bean){
String fullClassName = bean.getClass().getName();
String classNameTemp = fullClassName.substring(fullClassName.lastIndexOf(".") + 1, fullClassName.length());
return classNameTemp.substring(0, 1)+ classNameTemp.substring(1);
}
}


The BeanXMLMapping class converts a JavaBean to and from an XML document and provides two methods:

bean2Xml(): generates the respective XML document String for the bean instance
xml2Bean(): creates a bean instance for the XML document String


The below Code shows How to use the BeanXMLMapping class to map the bean to xml and vice varsa.

Code:

public class PersonBean {
private String name;
private Integer age;
private Double salary;
private Integer exp;

public PersonBean(){
this.name="test";
this.age=24;
this.salary=5000.0;
this.exp=2;
}

public PersonBean(String name,Integer age,Double salary,Integer exp){
this.name=name;
this.age=age;
this.salary=salary;
this.exp=exp;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getExp() {
return exp;
}
public void setExp(Integer exp) {
this.exp = exp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String toXML(){
return BeanXMLMapping.bean2Xml(this);
}
public static Object fromXML(String xml){
return (PersonBean) BeanXMLMapping.xml2Bean(xml,PersonBean.class);
}
}


Test Code:

public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonBean bean=new PersonBean();
String xmlStr=bean.toXML();
System.out.println(xmlStr);
}
}


The Above Test Code Generates the Output As:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonBean>
<age>24</age>
<exp>2</exp>
<name>test</name>
<salary>5000.0</salary>
</PersonBean>

Thursday, August 30, 2007

Evaluating XML's XPath Expressions

javax.xml.xpath package provides support for executing XPath expressions against a given XML document. The XPath expressions can be compiled for performance reasons, if it is to be reused.

By the way, the XPath APIs in JAXP are designed to be stateless, which means every time you want to evaluate an XPath expression, you also need to pass in the XML document. Often, many XPath expressions are evaluated against a single XML document. In such a case, it would have been better if the XPath APIs in JAXP were made stateful by passing the XML document once. The underlying implementation would then have had a choice of storing the XML source in an optimized fashion (say, a DTM) for faster evaluation of XPath expressions.


Sample XML to evaluate the XPath expressions :

<?xml version="1.0"?>
<employees>
<employee>
<name>e1</name>
</employee>
<employee>
<name>e2</name>
</employee>
</employees>

Code to evaluate the xml:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XpathTest
{
public void parseXml()throws Exception{
//parse an XML to get a DOM to query
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
dbfactory.setNamespaceAware(true);
dbfactory.setXIncludeAware(true);

DocumentBuilder parser = dbfactory.newDocumentBuilder();
Document doc = parser.parse(new File("data.xml"));

//get an XPath processor
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xpathprocessor = xpfactory.newXPath();

//set the namespace context for resolving prefixes of the Qnames
//to NS URI, if the xpath expresion uses Qnames. XPath expression
//would use Qnames if the XML document uses namespaces.
//xpathprocessor.setNamespaceContext(NamespaceContext nsContext);

//create XPath expressions
String xpath1 = "/employees/employee";
XPathExpression employeesXPath = xpathprocessor.compile(xpath1);

String xpath2 = "/employees/employee[1]";
XPathExpression employeeXPath = xpathprocessor.compile(xpath2);

String xpath3 = "/employees/employee[1]/name";
XPathExpression empnameXPath = xpathprocessor.compile(xpath3);

//execute the XPath expressions
System.out.println("XPath1="+xpath1);
NodeList employees = (NodeList)employeesXPath.evaluate(doc, XPathConstants.NODESET);
for (int i=0; i<employees.getLength(); i++) {
System.out.println(employees.item(i).getTextContent());
}

System.out.println("XPath2="+xpath2);
Node employee = (Node)employeeXPath.evaluate(doc, XPathConstants.NODE);
System.out.println(employee.getTextContent());

System..out..println("XPath3="+xpath3);
String empname = empnameXPath.evaluate(doc);
System.out.println(empname);
}
}