Thursday, January 29, 2009

Validating XML against XSD in Java

To validate XML documents against XSD in java...

You can use the DocumentBuilderFactory or SAXParserFactory from

        String JAXP_SCHEMA_SOURCE = "";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        String schemaSource = "Path to your XSD";
        Source schemaSourceSrc = new StreamSource(schemaSource);        // has various constructors to pass xsd as a file,url,stream and so on ...
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaSourceSrc);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(" your xml file ");

This will validate your xml file against the xsd. Many examples in the web show similar snippets...  More details are on the link below from sun


Note: If your XSD document contains regular expressions or data type restrictions then they are also validated.

XML Schema Regular Expressions

The regular expressions used in the XML Schema are of a different flavour other than the ones used regularly in Java. Not all pattern matching techniques apply directly in XML Schema.

For example to validate the decimal places I use the following pattern "\-{0,1}[0-9]*(\.{1}[0-9]{1,2})?" without the codes, what this pattern says is that the negative symbol is optional and if provided only one character of the negative symbol should be given. The asterisk denotes that any number of numerals may be provided, followed by a opening bracket and a pattern and closing bracket followed by a "question" mark symbol. The question mark symbol means that the pattern in the brackets is optional. In effect this pattern can accept whole numbers and optional decimal numbers.

Once you have defined that the node type is decimal and applied that above pattern. Then you can write a simple java class that reads the XML document and also applies the XSD to validate that XML document. And if the XML document contains invalid characters for that node it throws an excpetion saying that the value is not supported by that pattern.

Calculate Date for a given week in an year.

How to calculate the date for a given week number in an year.

For E.G. If you have the week number "23" to transalate that to a date like "July-17-2009" I used this technique.

Take the week number multiply that by 7 to get the number of days since the beginning of the year and then construct the date.

In MySql - there is a date function called "makedate" which takes the year and the number of days as an parameter

"makedate(year(logintime),week(logintime) * 7)" This will return a date like "2009-07-21"

In DB2 to do the same thing.

"(DATE(((week_iso(a.logintime)-1)* 7)+ 2) + (year(a.logintime) -1) years)"

Take precuation to assume your week beginning with respect to your business logic, some businesses the week begins on sunday and some for monday and so on...

In MySql there are system variables to set the default start of week...

Wednesday, January 14, 2009

How to analyze queries in my-sql and configure an index.

If you have a query in My-Sql and if it is running slow you can always add the explain keyword in front of the SQL to analyze it.

For example executing the following query "explain select * from employee where employee_id='001'; " would give this output.

Note the column where it says "possible_keys" and "keys" in those columns if those columns are showing up as null, then this means that there are no indexes being used.

The next step would be is to create an index and execute the explain query again. If you see the index that you just created then this means the query would use the index and run faster. There are a lot of tradeoffs on when to create indexes so use it with caution... This cool article explains this in depth...

Tuesday, January 13, 2009

Eclipse Scrapbook

In eclipse you can write few lines of codes without having to write the entire class declaration and main method to execute the few lines. This feature is called scrapbook if you have a java enabled project create a new file with the extension as ".jpage".

The next step would be is to write a few lines of code in that page...

  1. String message="Hello %1$s";
  2. message = message.format(message,"Jane Doe");
  3. System.out.println(message);

The step after that would be is to select the lines of code you want to execute and access the right click menu

Select the execute option and the just the selected lines of code alone would be executed and outputted in the console. I use this feature to quickly test a few lines of code…

Java String formatting with "printf" like commands

There is a cool new way to create String templates in java and use the String.format() static method to substitute values dynamically.

For example the code snippet

  1. String message="Hello %1$s";
  2. message = message.format(message,"Jane Doe");
  3. System.out.println(message);

The output is:

> Hello Jane Doe

This gives you an idea of how to create string templates and replace them by using printf like syntax. Here is the sun link that will give you more on this style of formatting. in our project we are using this on the message properties and substituting values for more meaningfull validation messages.

Here is one more example with multiple indexes.

  1. String x = "test who let the stars out '%2$s' ";
  2. String y = "me";
  3. String z = "you";
  4. String message = String.format(x,new String[]{y,z});
  5. System.out.println(message);

The index "2" in the line number 1 refers to the second array element in the string array that is passed to the format utility in line number 4.

Much more complicated example are available in the sun link. I tried the above examples in Java 1.5.

Web development with Firefox.

I am listing some of the Firefox addons that I have found to be very useful for web development.


  1. Firebug – Allows you to inspect the browser DOM, edit CSS on the fly and to find more information about what style is being applied to a particular table and to identify nested tables simply hover over them to find out all the details! Awesome!!!
  2. Screengrab – With this you can highlight a portion of the webpage and save it to clipboard or a file, simply cool!! I use it a lot!!!
  3. MeasureIt -  This tool will allow you to measure the width and height in pixels of anything/any area in your webpage
  4. ColorZilla  - You can select the color from an area and it will give you hex codes and RGB colors … highly useful!!!
  5. WebDeveloper -  Awesome tool you can do most of the stuff that you do with firebug with this.


I also like fireftp for ftp work and Pencil for developing mockups; but the idea that you can do most of the work from inside your browser without having to manage too many applications is really cool!