Last active
September 13, 2017 19:13
-
-
Save swhume/6b9a7ae74adc6f601510df4256e2a4cd to your computer and use it in GitHub Desktop.
Validate Define-XML v2.0 in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package definevalidate; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import javax.xml.XMLConstants; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamSource; | |
import javax.xml.validation.Schema; | |
import javax.xml.validation.SchemaFactory; | |
import javax.xml.validation.Validator; | |
import org.w3c.dom.Document; | |
import org.xml.sax.SAXException; | |
/** | |
* DefineValidate is a quick example program that demonstrates one approach to | |
* running schema validation Define-XML v2.0 in Java. Requires | |
* that redefine in the define-extension.xsd be changed to | |
* <xs:redefine schemaLocation="../odm1-3-2/ODM1-3-2-foundation.xsd"> | |
* @author shume | |
* @version 0.1 | |
*/ | |
public class DefineValidate { | |
// requires the ODM, Define-XML, or extension XML file + the schema file as arguments | |
public static void main(String[] args) { | |
// load the xml file and the schema file for validation | |
HashMap<String, String> fileMap = buildFileMap(args); | |
// validate the XML file using the schema | |
if (validate(fileMap.get("xml"), fileMap.get("xsd"))) { | |
System.out.println(fileMap.get("xml") + " successfully schema validated"); | |
} | |
} | |
// example validation method that works on ODM, Define-XML, and extensions | |
private static Boolean validate(String xmlPathFile, String xsdPathFile) { | |
Boolean isValid = Boolean.FALSE; | |
try { | |
String schemaFactoryProperty = "javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI; | |
System.setProperty(schemaFactoryProperty, "org.apache.xerces.jaxp.validation.XMLSchemaFactory"); | |
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); | |
parserFactory.setNamespaceAware(true); | |
parserFactory.setIgnoringElementContentWhitespace(true); | |
parserFactory.setIgnoringComments(true); | |
// grab a factory for the W3C XML Schema Language and compile the schema | |
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
Schema schema = factory.newSchema(new StreamSource(new File(xsdPathFile))); | |
parserFactory.setSchema(schema); | |
DocumentBuilder parser = parserFactory.newDocumentBuilder(); | |
Validator validator = schema.newValidator(); | |
validator.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); | |
validator.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true); | |
// validate the XML document | |
Document document = parser.parse(new File(xmlPathFile)); | |
validator.validate(new DOMSource(document)); | |
isValid = Boolean.TRUE; | |
} catch (SAXException | IOException | ParserConfigurationException ex) { | |
System.out.println("Validation error: " + ex.getMessage()); | |
} | |
return isValid; | |
} | |
// load the xml and xsd files from cmd-line into a hashmap | |
private static HashMap<String, String> buildFileMap(String[] files) { | |
HashMap<String, String> fileMap = new HashMap(); | |
// ensure that both command-line arguments exist | |
if (files.length != 2) { | |
invalidArguments("Invalid command-line arguments. Command-line arguments are required."); | |
} | |
for (int i=0; i<files.length; i++) { | |
// ensure that both command-line arguments are files | |
checkFileIsFound(files[i]); | |
// grab extension - should use a library, but this works for the demo | |
int dot = files[i].lastIndexOf('.'); | |
if (dot > 0) { | |
String extension = files[i].substring(dot+1); | |
fileMap.put(extension, files[i]); | |
} else { | |
invalidArguments("Invalid command-line arguments: file extensions of xml or xsd expected."); | |
} | |
} | |
checkMapIsValid(fileMap); | |
return fileMap; | |
} | |
// make sure we have and xml and an xsd file | |
private static void checkMapIsValid(HashMap fileMap) { | |
if (fileMap.size() != 2) { | |
invalidArguments("Invalid command-line arguments: file extensions of xml or xsd expected."); | |
} else { | |
if (! (fileMap.containsKey("xml") && fileMap.containsKey("xsd"))) { | |
invalidArguments("Invalid command-line arguments: file extensions of xml or xsd expected."); | |
} | |
} | |
} | |
// make sure the cmd-line arguments are files and that they are found | |
private static void checkFileIsFound(String fileName) { | |
if (fileName.isEmpty() || !(new File(fileName).isFile())) { | |
invalidArguments(fileName + " is not found."); | |
} | |
} | |
// command-line arguments must be valid to continue with the validation | |
private static void invalidArguments(String msg) { | |
System.out.println(msg); | |
usage(); | |
System.exit(0); | |
} | |
// requires an XML and XSD filename and path on the command-line | |
private static void usage() { | |
System.out.println("Usage: include the file name and path for the XML file to validate and " | |
+ "the XSD schema file on the command-line. Example: java -jar DefineValidate.jar " | |
+ "c:\\xml\\define-xml-v2-example.xml " | |
+ "c:\\define-xml-2.0\\schema\\define2-0-0.xsd"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment