11package validator ;
22
3+ import java .io .File ;
34import java .io .FileReader ;
45
56import org .apache .xerces .parsers .DOMParser ;
67import org .xml .sax .InputSource ;
78
89public class ValidateNeXML {
9- public static String againstXSD (final String xmlFilepath ) {
10- String apacheXml = "http://apache.org/xml/properties/schema/" ;
11- String namespace = "http://www.nexml.org/2009" ;
12- String schemaURL = "http://www.nexml.org/nexml/xsd/nexml.xsd" ;
10+
11+ public static String againstXSD (final String xmlFilepath , final String xsdFilepath , final String namespace ) {
1312 try {
13+ // make sure both paths specify a valid file
14+ // File config_fp = new File(xmlFilepath);
15+ // if (!config_fp.exists())
16+ // return "Unable to open file " + xmlFilepath;
17+ // config_fp = new File(xsdFilepath);
18+ // if (!config_fp.exists())
19+ // return "Unable to open file " + xsdFilepath;
1420 final DOMParser parser = new DOMParser ();
1521 parser .setFeature ("http://xml.org/sax/features/validation" , true );
1622 parser .setFeature ("http://apache.org/xml/features/validation/schema" , true );
17- parser .setProperty (apacheXml + "external-schemaLocation" , namespace .trim () + " " + schemaURL );
23+ if (namespace == null || namespace .trim ().equals ("" ))
24+ parser .setProperty ("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation" , xsdFilepath );
25+ else
26+ parser .setProperty ("http://apache.org/xml/properties/schema/external-schemaLocation" , namespace .trim () + " " + xsdFilepath );
1827 parser .parse (new InputSource (new FileReader (xmlFilepath )));
1928 return null ;
2029 }
@@ -23,17 +32,30 @@ public static String againstXSD(final String xmlFilepath) {
2332 return e .getMessage ();
2433 }
2534 }
26-
35+
2736 public static void main (final String args []) {
2837 if ( args .length == 0 ) {
29- System .out .println ("Usage: NexmlValidator <filename>" );
38+ System .out .println ("Usage: NexmlValidator <filename> [schema] [namespace] " );
3039 System .exit (1 );
31- }
32- final String retval = againstXSD (args [0 ]);
40+ }
41+ String xmlFilepath = args [0 ];
42+ String schemaPath ;
43+ String namespace ;
44+ if (args .length > 1 ) {
45+ schemaPath = args [1 ];
46+ } else {
47+ schemaPath = "http://www.nexml.org/nexml/xsd/nexml.xsd" ;
48+ }
49+ if (args .length > 2 ) {
50+ namespace = args [2 ];
51+ } else {
52+ namespace = "http://www.nexml.org/2009" ;
53+ }
54+ final String retval = againstXSD (xmlFilepath , schemaPath , namespace );
3355 if (retval == null ) {
3456 System .exit (0 );
3557 }
3658 System .out .println ("Validation error: " + retval );
3759 System .exit (1 );
38- }
60+ }
3961}
0 commit comments