|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Read the whole "Validation with Java and XML Schema" series:
On the other hand, everyone (and their dog!) is looking to get into XML. Using the Extensible Markup Language seems to be even more popular than hacking the Linux kernel these days and will even make your boss happy. So how do those two fit together? Well, XML, and specifically XML Schema, provides a perfect means of detailing constraints for Java data. And with a simple Java-based framework, you can build those constraints into Java objects and compare application data against them. The end result is a flexible, robust, XML-based framework for all your Java validation needs.
In this article, I'll show you how to parse an XML Schema and build up a set of constraints. Once those constraints are ready for your Java program to use, I'll detail the process of comparing data to them. Finally, your application will be given a means to pass in data and determine which constraint to apply to that data, indicating if that data is valid for that constraint. But before diving in, let me fill you in on what's already happened in this series.
In Part 1, I spent a lot of time talking about validation in general terms. I looked at some common bad practices you tend to find
in validation code, particularly the case of simply hard coding in constraints. Of course, that is not at all portable, so
I also looked at some utility classes, such as Jason Hunter's ParameterParser class from Java Servlet Programming. That class allows the simple conversion from the String format in which servlets receive data to various other Java formats such as ints, floats, and booleans. However, that still did not address other common validation needs such as range checking and specifying only a few allowed
values. Finally, I introduced XML as a possible solution, showing how an XML document is superior to Java's standard property
files.
In Part 2, I introduced the validation framework. Starting with some basic design, I showed the four basic classes you need to code:
Constraint class, which represents constraints for a single type, like the shoeSize type.
Validator class, which provides an interface for allowing developers to pass in data, and find out if the data is valid.
schemaParser class, which parses an XML Schema and creates the Constraint objects for use by the Validator class.
DataConverter helper class, which will convert from XML Schema data types to Java data types, and perform other data type conversions for
you.
In that article, I showed you the Constraint class in its entirety, providing basic methods that allowed setting an allowed range, a data type, and allowed values for
the data. If you were to add additional constraint types, such as pattern matching, you would add them to that class. I also
outlined the Validator class, and left a blank where schema parsing would occur, which I will fill in this article.