XML Validation

An XML Validator

To help you check the syntax of your XML files, we have created an XML validator to syntax-check your XML.


Valid XML Documents

A valid XML document is not the same as a well formed XML document.

The first rule, for a valid XML document, is that it must be well formed (see previous paragraph).

The second rule is that a valid XML document must conform to a document type.

Rules that defines legal elements and attributes for XML documents are often called document definitions, or document schemas.


When to Use a Document Definition?

A document definition is the easiest way to provide a reference to the legal elements and attributes of a document.

A document definition also provides a common reference that many users (developers) can share.

A document definition provides a standardization that makes life easier.


When NOT to Use a Document Definition?

XML does not require a document definition.

When you are experimenting with XML, or when you are working with small XML files, creating document definitions may be a waste of time.

If you develop applications, wait until the specification is stable before you add a document definition. Otherwise, your software might stop working because of validation errors.


Document Definitions

There are different types of document definitions that can be used with XML:

  • The original Document Type Definition (DTD)
  • The newer, and XML based, XML Schema

XML Elements vs Attributes

Usually elements are used to capture the data however attributes can be to capture the metadata (data about data – something that provides additional information about the data) ie the data which might not be used to display but can be used for search or other such features.There is no rule as such that says something should be an attribute or a element. I recommend to avoid it Winking smile

Some of the problems with using attributes are:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.

The following three XML documents contain exactly the same information:

A date attribute is used in the first example:

<note date=”10/01/2008″>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don’t forget me this weekend!</body>
</note>

A date element is used in the second example:

<note>
<date>10/01/2008</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don’t forget me this weekend!</body>
</note>

An expanded date element is used in the third: Preferred :

<note>
<date>
<day>10</day>
<month>01</month>
<year>2008</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don’t forget me this weekend!</body>
</note>

Why dividing code into algorithms and coordinators

All code can be classified into two distinct roles; code that does work (algorithms) and code that coordinates work (coordinators).

The real complexity that gets introduced into a code bases is usually directly related to the creation of classes that group together both of these roles under one roof.

Computer electronics have two roles: doing work and binding together the stuff that does the work.  If you take out the CPU, the memory and all the other components that actually do some sort of work, you’ll be left with coordinators.  Wires and busses that bind together the components in the system.

So now that we understand that code could potentially be divided into two broad categories, the next question of course is why?  And can we even do it?

Let’s address why first.

The biggest benefit to pulling algorithmic code into separate classes from any coordinating code is that it allows the algorithmic code to be free of dependencies.  (Practically all dependencies.)

Once you free this algorithmic code of dependencies you’ll find 3 things immediately happen to that code:

  1. It becomes easier to unit test
  2. It becomes more reusable
  3. Its complexity is reduced

Remote Debug : Java Application using JDWP

Debugging application is always a common topic for all developers and it may cover more than 60%+ of total development time if using traditional way like using debugging message.

After the release of Sun Java Development Kit 1.4.2, you can be benefited by the magic tool called JDWP (Java Debugging Wire Protocol), It provides sophisticated implementation for debugging your JAVA application in a modern way, especially when you want to debug server java application like Tomcat, JBoss, etc..

Okay, you may say you are happy using debug message for debugging remote application and thus let me list the summary of how JDWP can aid the development.

  1. Thread stepping execution: Suspend/Resume particular thread
  2. Breakpoints management: Insert/Remove breakpoint for investigate failure point
  3. Hotswap Bug Fixing: Recompile the classes on-the-fly for bug fixing without redeployment

I thought you may be interested with the features 3, which greatly reduces the re-deployment time for debugging an application.

This articles will focus how the setup for debugging remote java application. Tomcat5.5 and Eclipse Europa will be used for an example.

1. High Level View

Basically it is divided into two entities: Debuggee and Debugger. The Debuggee is the application that you want to debug and for our example it is Tomcat. The Debugger is the vendor of JDI (Java Debugging Interface) : Eclipse.

jpda

 

2. Enable JDWP in the Debuggee (Tomcat)

JDWP can be enabled out of the box by setting the appropriate system properties :

-Xrunjdwp:transport=dt_socket,address=9002,server=y,suspend=n

This enables JVM with JDWP at port 9002 using server mode with any suspension.

Server

(“n” or “y”) If “y,” listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address.

Address

transport address for the connection. If server=n, attempt to attach to a debugger application at this address. If server=y, listen for a connection at this address.

Timeout

If server=y, this specifies the timeout, in milliseconds, to wait for the debugger to attach. If server=n, this specifies the timeout, in milliseconds, to use when attaching to the debugger.

Suspend

If “y,” JVM suspends the execution until a debugger connects to the debuggee JVM. Preferably set this option to “n” else the application will not start unless the debugger attaches the port on which debugee  application is lessoning.

Remote Debug : an insight

remoteDebug

Some times you have to debug a application which is not running from it’s source code in a IDE. So to do it we use this remote debug concept. In the scenario which is shown above the application runs in a remote machine, but when it runs it will broadcast debug information via a socket deployed. The IDE running locally will capture those broadcast debug information through that socket and it will do the debug through the source code.
JAVA language provides this in its JVM. We can use commands like this to run a application in the remote debug mode in java.

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,suspend=n,server=y -jar Name_of_Your_Jar.jar

Then it will broadcast some debug information and will wait until some IDE capture those debug information and start debugging the source code. When Some IDE connects to the Socket specified it will start the debugging process.
Configuring the IDEs will depend on them. The links given below will give you a good understanding about those concepts. Hope you learn some thing from this post. Following links are really interesting to learn about remote debugging.

  • Application to be debugged would attach a socket to itself and then would listen debug instructions on that socket.
  • Debugger would bind itself to that socket and then send instructions on that socket.

What is Remote Debugging :
http://www.javabeat.net/2010/12/what-is-remote-debugging-in-java/
Remote Debugging with Eclipse :
http://www.eclipsezone.com/eclipse/forums/t53459.html
http://java.dzone.com/articles/how-debug-remote-java-applicat
http://www.myeclipseide.com/documentation/quickstarts/remotedebugging/
Remote Debugging with IntelliJ IDEA :
http://www.javaranch.com/journal/200408/DebuggingServer-sideCode.html
Remote Debugging Web Services, Apache Axis 2 :
http://wso2.org/library/225
http://wso2.org/library/3851
http://amilamanoj.blogspot.com/2011/09/running-debugging-apache-axis2-inside.html
http://shameerarathnayaka.blogspot.com/2011/09/remote-debugging-apache-axis2-with.html

Create War using command prompt

WAR stands for Web application ARchive. A Web application is a group of HTML pages, JSP pages, servlets, resources and source file, which can be managed as a single unit. A Web archive (WAR) file is a packaged(zipped/compressed) Web application. WAR files can be used to import a Web application into a Web server.

Here’s how to compress a ‘myWebApp’ web application into a “Web Archive” file named myWebApp.war so that you can easily transport it to another server.
* Open a command prompt and cd to
<Tomcat_home>\webapps\myWebApp
* Use the java archive command ‘jar’ to bundle up your application
jar -cvf myWebApp.war *

or go to one level before your web application folder  the folder you want to package as Web Archieve
you file is somewhere like d:\webapps\myWebApp
d:>cd webapps
d:\webapps:>jar -cvf myWebApp.war myWebApp

In English, “compress everything in this directory into a file named myWebApp.war”
(c=create, v=verbose, f=file)

Now you can copy myWebApp.war to any the webapps directory on a new Tomcat Server. When you save the file to a new server BE SURE not to save it as a ZIP file, choose “all files”. If Tomcat is running on the new server the war file will be automatically installed as soon as you save it. If not, start up Tomcat on the new server to install the application after you have saved it. Do NOT attempt to unzip the war file or do any installation yourself. Tomcat will do everything for you!

When the server is restarted it will automatically uncompress the files and recreate a web application named ‘myWebApp’.

How remote debugging works?

Introduction

Consider a scenario where you can’t run the application in your development environment, e.g. say your application can run only on a server machine (because it is dependent on some third party interface that are not accessible in your development machine) and you have to resolve a problem (bug). What you can do?

The solution is Remote debugging. Remote debugging is debugging an application by connecting the remotely running application with your development environment ( i.e. you can say to connect with code in your IDE).

How remote debugging works?

Remote debugging feature is provided by Java specification itself. Java provides this feature using listener binding mechanism. Basic concept is pretty simple and straightforward:

  • Application to be debugged would attach a socket to itself and then would listen debug instructions on that socket.
  • Debugger would bind itself to that socket and then send instructions on that socket.

 

What is JPDA ?

The debugging features of the JVM are provided via the Java Platform Debugger Architecture (JPDA).

The JPDA itself is composed of the following:

  • Java Virtual Machine Tool Interface (JVM TI) – the native programming interface for tools to use. This interface allows for state inspection and helps in controlling the flow of execution within the debuggee.
  • Java Debug Wire Protocol (JDWP) – used to define the communication between the debugger and debuggee processes.
  • Java Debug Interface (JDI) – this interface allows tool developers to write remote debugger applications.

The diagram listed in the JPDA architecture structure is a good starting point. Additional places to look for would be the guides listed in the JPDA page.

 

Running an java application in debug mode

As I said earlier, for remote debugging, you need to run that application in debug mode. To run an application in debug mode requires to provide specific jvm arguments to “java” command.

JVM arguments for DEBUGGING

For JVMs prior to 1.5

One need to supply two arguments, –Xdebug and -Xrunjdwp. -Xdebug tells JVM to run the application in debug mode, while -Xrunjdwp is used to supply debug parameters (It can also be used in JVM 1.5 or 1.6 as well)

e.g. -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

For JVMs 1.5 and 1.6

Debug library is passed as -agentlib argument, along with debug paramters. Native library name for debugging is jdwp, so one need to supply -agentlib:jdwp along with debug paramters.

e.g. -agenlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n

Detail of debug parameters

  • Name: help

Is Optional : Yes
Default Value: N/A. This parameter doesn’t have any value at all.
Description: It prints all the available options on the console and exits the JVM.
Example: java -agentlib:jdwp=help

 

  • Name: transport

Is Optional: No (It is a required field)
Default Value: No default value specified in specifications. Totally depends upon native lib provided by JVM implementation.
Description: Transport protocol implementation used for communication between debugger and application. Sun JVM ships two implementations, Socket Transport and Shared Memory Transport. Value for Socket Transport is “dt_socket”, while that of Shared Memory Transport is “dt_shmem”.
Shared Memory is available only on Microsoft Windows.

  • Name: server

Is Optional: Yes
Default Value: n
Description: Tells whether JVM will be used as Server or as client in reference to JVM-Debugger communication. If JVM will act as server, then debugger will attach itself to this JVM, otherwise if JVM will act as a client, then it will attach itself to debugger.

  • Name: address

Is Optional: Optional when JVM acts as server and Required when JVM acts as client.
Description: Specifies the socket address.
If JVM is server (i.e. server argument is set to ‘y’): Then JVM listens the debugger at this socket. If address is not specified, then JVM picks any of available socket, starts listening on this socket and prints its address on console.
If JVM is client (i.e. server argument is set to ‘n’): JVM attaches itself to this address to connect itself to debugger.

  • Name: timeout

Is Optional: Yes
Description: As its name signifies, it is the wait time for JVM. If JVM is server, then JVM will wait for “timeout” amount of time for debugger to attach to its socket and if JVM is client, then it will exit if fail to attach to debugger in timeout time.

  • Name: suspend

Is Optional: yes
Default Value: y
Description: Tells JVM to suspend the invocation of application(s) until the debugger and JVM are attached together.

Apart these there are launch, onthrow, onuncaught options as well, details of which can be find at JPDA. But these options are seldom used.

Now, lets take simple example to debug a program using eclipse.

Steps

  • Select from eclipse menu, Run->Debug Configuration, It will open Debug Configuration setup.
  • On Debug configuration window, create a new “Remote Java Application” configuration.
  • Select the project to be debugged and provide a Name.
  • Select connection type, Socket Attach or Socket Listen. Select Socket Attach, if you want to attach the debugger to application, i.e. application is running in Server mode. On the other hand, select Socket Listen, in case you want debugger to listen for JVM to connect, i.e. JVM is running in client mode and debugger is running as server.
  • If JVM to be run in Server mode, then first start JVM in debug mode and then start the newly created debugger,
  • And if Debugger to be run in Server mode, then first start the debugger and then start JVM.

Debugging trick – Conditional break point

One of the most important developer activities is debugging.
Imagine there are 1000 employees in a record and application is giving error for a particular employee lets say with name “prem”. Console error says its some time inside the loop where all the records are iterated. Generally people put a break point  before the error prone line number and watches the value to find out the state. If its linear flow its good but if it is the loop then one has to wait for ( worst case 1000, best case within 10 ) repetition to get to know the root cause.

Here comes the experience of developer and facility of tool IDE which is called as conditional break point. By doing the right click on break point one can the property of conditional break point. you just need to simple give the condition ,as in if clause ->  emp.getName().equals(“prem”). By virtue of this condition the code will break only when the employee name is “prem”. This will save a lot of time to watch and analyze all the names of employees manually to find out the buggy/error prone employee object “prem”.

In such cases previously i use to do System.out.println(), recompile and run app time and again. then before check in the code need to remove systemouts. Now I realize it was such a pain and when better tricks are available 😉

For more details to set conditional break point using Eclipse visit the below link:
http://eclipse.dzone.com/tips/effective-debugging

What is browser plugin ?

Plug-ins are software programs that extend the capabilities of the browser in a specific way. This means that beyond reading a basic web page, plug-ins let you watch movies and animation, hear sound and music, read special Adobe documents, play online games, do 3-D interaction, and use your web browser like a type of interactive software package.
Browser Helper Objects (BHOs) are small programs that can observe and control almost everything
Many times, these plug-ins and BHOs are badly written and crash or slow down the browser.

Why does not these plugin capabilities comes out of the box ?
1. Depends on usage, all basic things comes out of the box, additional features if required can be plugin.
2. Maintaining so many capabilities in the browser and updating all of them at single place is difficult. So plugin and can maintained and upgraded separetely.
3. Of couse the facility to unplug the plugin when it is not required.
4. Putting up all the plugins out of the box will make the browser slow.

What are the important browser plugin i require ?
Although new plug-in software is released every week, there are 11 key plug-ins and add-on software that will serve you 99% of the time:
Adobe Acrobat Reader (for .pdf files)
Java Virtual Machine (JVM to run Java applets)
Microsoft Silverlight (to run rich media, databases, and interactive web pages)
Adobe Flash Player (to run .swf animation movies and YouTube videos)
Adobe Shockwave Player (to run heavy-duty .swf movies)
Real Audio Player (to listen to .ram files)
Apple Quicktime (to see 3d Virtual Reality schematics)
Windows Media Player (to run a variety of movies and music formats)
WinAmp (to play downloaded .mp3 and .wav files, and display artist information)
Antivirus software: because getting infected will ruin anyone’s day online.
Optional browser toolbars, like Google toolbar, Yahoo toolbar, or StumleUpon toolbar