Here is a solution that uses the Xerces parser / validator. However, it does use .impl classes. They are not part of the public API and are subject to change. But if you stick to a certain version, you should be fine.
First, the dependency:
<dependency> <groupId>xerces</groupId> <artifactId>xerces</artifactId> <version>2.4.0</version> </dependency>
And here is a small program that works as you described:
import org.apache.xerces.impl.dv.InvalidDatatypeValueException; import org.apache.xerces.impl.dv.xs.DateDV; public class XSDValidator { public static void main(final String[] args) { System.out.println(isXSDDate("2012-09-18")); System.out.println(isXSDDate("Hello World")); } private static boolean isXSDDate(final String string) { try { new DateDV().getActualValue(string); return true; } catch(final InvalidDatatypeValueException e) { return false; } } }
Output:
true false
All you have to do is create methods for each of the data types. You should find all the required classes in the org.apache.xerces.impl.dv.xs package.
Again, this is a kind of abuse of the Xerces library, as these classes are not part of the public API. Therefore, if you find a different, cleaner solution, let us know.
rolve source share