Popular Posts

Wednesday, June 24, 2009

XML Data File 2 Java Bean Class

This time I am creating a J2EE application which requires back and forth retrieval of data values from a XML data file. I decide to write a separate class file which will only read the XML file and load its values into the memory. The usual way will be to use a XML parser but I decided to go with JOX Java Library.

JOX Introduction:

JOX matches an XML document to the fields of a java bean and will use a DTD when writing an XML document if one is available. Because JOX identifies the Java Bean at runtime, you can transmit XML into different classes.

JOX limitations:

  1. You must use Java Bean’s getter/setter methods because JOX uses introspection to understand the property names.
  2. XML tag names must match bean property names.
  3. JOX converts XML data to the type of the bean property.
  4. Without a DTD, JOX uses bean property names as XML tag names.

Note: you can also use JOX to write a bean to a DOM object so that it can be used for XSLT processor later on.

JOX example:

Now I will walk you through a live a example of How I used JOX for my party invitation generator application. First take a look at the XML data file which is being created by my application when a user created a party:

   1:  <?xml version="1.0" ?>
   2:  - <partyInvitation>
   3:    <date>2009.May.11 12:48 PM</date> 
   4:    <hostname>testtest</hostname> 
   5:    <password>testtest</password> 
   6:    <inviteMessage>test</inviteMessage> 
   7:    <guestname>test</guestname> 
   8:    <guestemail>test@gmu.edu</guestemail> 
   9:    <gueststatus>No Status</gueststatus> 
  10:    <guestid>0</guestid> 
  11:    <guestname>test2</guestname> 
  12:    <guestemail>test2@gmu.edu</guestemail> 
  13:    <gueststatus>No Status</gueststatus> 
  14:    <guestid>1</guestid> 
  15:    <RiazG00444898 /> 
  16:    </partyInvitation>


As you can see on line 5 the password field is not encrypted in the file which is not a good approach but because of the testing limitations of this application I have to save it in a simple text form. At least at the bear minimum you can generate the hash code of the field before saving it to the file.

For the purpose of this article I will retrieve host name, guest emails and id values as these three belongs to different data types. Host name will always be a string, guest emails will contain more then one value so ArrayList will be use here and id will be integer type. And also I handled ArrayList values differently so that's why I am discussing it here. So lets get started by creating a Java Bean class with respective property names.

   1:  public class PartyDataBean {
   2:   
   3:      private static String hostname;
   4:      private static int guestid;
   5:      private static ArrayList<String> guestemail = new ArrayList<String>();
   6:      
   7:      public void sethostname(String name) {
   8:          hostname = name;
   9:      }
  10:   
  11:      
  12:     public void setguestemail(String email) {
  13:   
  14:          guestemail.add(email);
  15:      }
  16:   
  17:     
  18:      public void setguestid(int n) {
  19:   
  20:          guestid = n;
  21:      }
  22:   
  23:      public String gethostname() {
  24:          return hostname;
  25:      }
  26:   
  27:      
  28:      public String getguestemail() {
  29:          return guestemail.toString();
  30:      }
  31:   
  32:      public int getguest() {
  33:   
  34:          return guestid;
  35:      }
  36:  }

As you can see from from line 29 I am returning the all the values of the array without any loop which will return the values in this format: [test@gmu.edu,test2@gmu.edu] so you need to tokenize this string if you want to display these two values separately. The other possible way can be with a loop but then you are making more one function call which can effect your applications respond time as in large applications you have more then one value to retrieve.

Packages Needed:

import com.wutka.jox.JOXBeanInputStream;
import java.io.FileInputStream;

You need the above two packages in order to use JOX. The necessary JOX library files can be downloaded from http://sourceforge.net/projects/jox and make sure you put jox116.jar file into classpath so that the java code can find corresponding classes at compile and run time.

JOXBeanInputStream is An InputStream filter that reads XML into a bean. When you read an XML document, you must supply either a class or an object instance. The input stream will attempt to match XML tags to bean attributes in the class/object you supply.If you supply a class, the input stream will automatically create a new object instance to hold the data.

The stream understands the basic Java data types and their object equivalents, plus strings and dates. Anything else must be a bean. It can also read arrays of any of the supported types or of beans if it tries to read a bean with an indexed property.

If there are XML fields that don't match the bean, it will ignore them. If the data types are not compatible, you will get an exception.

Useful functions:

You only need to provide the XML data file path to JOXBeanInputStream object and then just read the data bean at runtime by providing the class file of that data bean to JOXBeanInputStream object. But before that make sure that the XML file does exit otherwise you will get an exception. Here is how I did it with the help of these functions give below:

public Boolean FileExits(String filename) {
   File file = new File(username);
   boolean exists = file.exists();
   if (!exists) {
   // It returns false if File or directory does not exist
    return false;
   } else {
    // It returns true if File or directory exists
    return true;
   }
 }
public Boolean LoadFile(String filepath) {
  try {
   FileInputStream fIn = new FileInputStream(filepath);
   JOXBeanInputStream joIn = new JOXBeanInputStream(fIn);
   testBean = (PartyDataBean) joIn.readObject(PartyDataBean.class);
    return true;
    } catch (Exception ex) {
    ex.printStackTrace();
    }
      return false;
}
 

It is a good practice if you make sure and checks the resource does exit before pointing to it.

That’s all for today and thanks for reading. Let me know if anyone needs more details on JOX. Any comments or suggestions are always welcomed.

Tuesday, April 21, 2009

Google before the Internet

It’s fun to imagine what Google would have been like before the Internet.

Google Queries in 1960:

google_circa_1960_src

Years later when color printing and mail system improved:

google

In the past people used to call their local library help desk to get answers to their questions but now It’s only a click away. Truly a well deserved improvement.

Tuesday, April 7, 2009

Make sure, It is secure

email1

Take a look at the above email, which I received a couple of days ago. It's from my bank's online account services stating that I may have made some changes to my account and they need to verify it. Also, the message header looks authenticated

image

So, now they want me to click on the link (provided in this email) and log on to the online account service and verify those changes. I clicked on the link :

http://www.gloriagales.com/chaseonline.chase.com/index.html

the site appeared and it's exactly the same. Except two things

i) The web address looks fishy to me as it is suppose to https://www.chase.com/Chase.html instead of the above.

ii) The web server is not secure, Are you wondering how I know it?. Closely compare both the web addresses and most of the times its exactly the same expect the little ‘s’ which comes right after the http part. Always make sure this ’s’ is present whenever you are sending your confidential information over the internet. This ‘s’ means the web server is secure and all information between you and the server is encrypted .

Most of all it is easy to remember instead of comparing the web address.

Wednesday, March 18, 2009

Some Precautions before coding JavaScript

Before you add JavaScript to your site you should make sure that it will be compatible with all your clients browsers. That’s hard as you are not aware who may access your site from any part of world using any browser version which may be or may not be compatible with JavaScript. Here are two tips which turns out to be really helpful for me.

1. Always use “<!-- -->” tags inside the Script tags. This will turn your scripting language code into comments if and only if client browser is not compatible with JavaScript code. This will eventually hide displaying the code on the client’s browser if scripting is disabled. Here is how to use it

<script language="JAVASCRIPT" type="text/javascript"> <!-- hide code from old browsers that doesn’t support JavaScript

JavaScript code statements

--> </script>

2. Use no scripts tags for worst case scenario. The contents of this tag will visible only if the scripting is disabled. Here it is how to use it :

<NOSCRIPT> Your browser understands JavaScript. However, the feature is turned off. Please turn it on to enjoy the full capability of this page. </NOSCRIPT>

Hope this will help next time you are coding JavaScript.