Skip to content

Commit

Permalink
allow for optional specification of schema path/namespace arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeetsukumaran committed Jun 25, 2012
1 parent c803408 commit 0c4fc39
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/validator/ValidateNeXML.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package validator;

import java.io.File;
import java.io.FileReader;

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.InputSource;

public class ValidateNeXML {
public static String againstXSD(final String xmlFilepath) {
String apacheXml = "http://apache.org/xml/properties/schema/";
String namespace = "http://www.nexml.org/2009";
String schemaURL = "http://www.nexml.org/nexml/xsd/nexml.xsd";

public static String againstXSD(final String xmlFilepath, final String xsdFilepath, final String namespace) {
try {
// make sure both paths specify a valid file
// File config_fp = new File(xmlFilepath);
// if (!config_fp.exists())
// return "Unable to open file " + xmlFilepath;
// config_fp = new File(xsdFilepath);
// if (!config_fp.exists())
// return "Unable to open file " + xsdFilepath;
final DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setProperty(apacheXml + "external-schemaLocation", namespace.trim() + " " + schemaURL);
if (namespace == null || namespace.trim().equals(""))
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", xsdFilepath);
else
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", namespace.trim() + " " + xsdFilepath);
parser.parse(new InputSource(new FileReader(xmlFilepath)));
return null;
}
Expand All @@ -23,17 +32,30 @@ public static String againstXSD(final String xmlFilepath) {
return e.getMessage();
}
}

public static void main(final String args[]) {
if ( args.length == 0 ) {
System.out.println("Usage: NexmlValidator <filename>");
System.out.println("Usage: NexmlValidator <filename> [schema] [namespace]");
System.exit(1);
}
final String retval = againstXSD(args[0]);
}
String xmlFilepath = args[0];
String schemaPath;
String namespace;
if (args.length > 1) {
schemaPath = args[1];
} else {
schemaPath = "http://www.nexml.org/nexml/xsd/nexml.xsd";
}
if (args.length > 2) {
namespace = args[2];
} else {
namespace = "http://www.nexml.org/2009";
}
final String retval = againstXSD(xmlFilepath, schemaPath, namespace);
if (retval == null) {
System.exit(0);
}
System.out.println("Validation error: " + retval);
System.exit(1);
}
}
}

0 comments on commit 0c4fc39

Please sign in to comment.