001/* 002 * XmlValidator.java 003 * $Header$ 004 * Copyright (c) 2002 Tom B. Gutwin P.Eng. 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License 008 * as published by the Free Software Foundation; either version 2 009 * of the License, or any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 019 */ 020/* 021 * 022 */ 023package ca.bc.webarts.tools; 024 025import java.io.IOException; 026import java.net.MalformedURLException; 027import java.net.URL; 028import java.util.Hashtable; 029 030import org.dom4j.Document; 031import org.dom4j.DocumentException; 032import org.dom4j.Element; 033import org.dom4j.io.OutputFormat; 034import org.dom4j.io.SAXReader; 035import org.dom4j.io.XMLWriter; 036import org.dom4j.util.XMLErrorHandler; 037 038import org.xml.sax.SAXException; 039 040 041/** 042 * Reads in an XML file and indicates if it is valid based on its specified 043 * XML Schema. 044 * This class uses dom4j. 045 * <PRE> 046 * Syntax: java ca.bc.webarts.tools.XmlValidator <xml URL> <xml schema URL> 047 * </pre> 048 * 049 * @author tgutwin 050 * @created July 10, 2002 051 */ 052public class XmlValidator 053{ 054 055 /** 056 * The main program for the XmlValidator class 057 * 058 * @param argv The command line arguments 059 */ 060 public static void main(String[] argv) 061 { 062 063 // Load in the commandline parmaters for macro substitution 064 System.out.println("Found " + argv.length + " args"); 065 if (argv.length != 2) 066 { 067 // odd number of args 068 System.out.println("Wrong number of args... <xml URL> <xml schema URL>"); 069 System.exit(1); 070 } 071 072 // On with the testing 073 StringBuffer msg = new StringBuffer("Validating:\n"); 074 if (validateDoc(argv[0], argv[1])) 075 { 076 msg.append(" Document is valid.\n"); 077 } 078 else 079 { 080 msg.append(" Document is NOT valid.\n"); 081 } 082 083 System.out.print(msg.toString()); 084 System.exit(0); 085 } 086 087 088 /** 089 * Validates the URL against the common schema. 090 * 091 * @param xmlStr Description of the Parameter 092 * @param schemaStr Description of the Parameter 093 * @return true or false ... duh. 094 */ 095 private static boolean validateDoc(String xmlStr, String schemaStr) 096 { 097 boolean retVal = true; 098 int stage = 0; 099 SAXReader reader = new SAXReader(true); 100 //reader.setValidation(true); 101 stage++; 102 103 try 104 { 105 URL xmlUrl = new URL(xmlStr); 106 URL schemaUrl = new URL(schemaStr); 107 // specify the schema to use 108 stage++; 109 reader.setProperty( 110 "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 111 schemaStr 112 ); 113 stage++; 114 115 // add an error handler which turns any errors into XML 116 XMLErrorHandler errorHandler = new XMLErrorHandler(); 117 stage++; 118 reader.setErrorHandler(errorHandler); 119 stage++; 120 121 // now lets parse the document 122 Document document = reader.read(xmlUrl); 123 stage++; 124 125 // now lets output the errors as XML 126 Element errors = errorHandler.getErrors(); 127 if (errors != null && errors.nodeCount() > 0) 128 { 129 XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); 130 writer.write(errors); 131 retVal = false; 132 } 133 } 134 catch (MalformedURLException badUrlEx) 135 { 136 System.out.println("Crapped out because URLs were useless: stage="+stage); 137 retVal = false; 138 } 139 catch (SAXException saxEx) 140 { 141 System.out.println("Crapped out during validation test, trying to turn validation on: stage="+stage); 142 saxEx.printStackTrace(); 143 retVal = false; 144 } 145 catch (Exception ex) 146 { 147 System.out.println("Crapped out during validation test: stage="+stage); 148 ex.printStackTrace(); 149 retVal = false; 150 } 151 return retVal; 152 } 153} 154