1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: Validate.java 470245 2006-11-02 06:34:33Z minchau $
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
/* Use JAXP SAXParser to parse 1 .xml file or all the .xml files in a directory.
* Takes 1 or 2 command-line arguments:
* Argument 1 (required) is a file name or directory name.
* Argument 2 (optional) is a log file name. If ommitted, messages are written to screen.
*/
public class Validate
{
static int numXMLFiles = 0;
static int numValidFiles = 0;
static int numInvalidFiles = 0;
static int numFilesMissingDoctype = 0;
static int numMalformedFiles = 0;
static boolean useLogFile = false;
static StringBuffer buff = new StringBuffer();
public static void main(String[] args)
throws FileNotFoundException, IOException, ParserConfigurationException, SAXException
{
if (args.length == 0 || args.length > 2)
{
System.out.println("\nEnter 'java validate -help' for information about running Validate");
return;
}
if (args[0].toLowerCase().equals("-help"))
{
String sep = "\n====================================================\n";
String a = "Validate uses Xerces to parse the xml files in the directory you specify or the individual xml file you specify. The parser validates each document (checks that it conforms to its DOCTYPE).\n";
String b = "Each xml file should contain a DOCTYPE declaration.\n\n";
String c = "Validate takes 1 or 2 arguments:\n";
String d = " Argument 1 specifies a directory or an individual xml file.\n";
String e = " Argument 2 specifies a log file. If you include this argument, Validate appends messages to this file. If you do not, Validate writes messages to the screen.\n";
System.out.println(sep+a+b+c+d+e+sep);
return;
}
try
{
Validate v = new Validate();
v.validate(args);
}
catch (Exception e)
{
e.printStackTrace();
}
}
void validate(String[] args)
throws FileNotFoundException, IOException, ParserConfigurationException, SAXException
{
File dir = new File(args[0]);
// User may include a 2nd argument for the log file.
useLogFile = (args.length == 2);
if (dir.isFile()) // Just checking one file.
{
parse(null,args[0]);
}
else if (dir.isDirectory()) // Checking the contents of a directory.
{
// Only interested in .xml files.
XMLFileFilter filter = new XMLFileFilter();
String [] files = dir.list(filter);
for (int i = 0; i <files.length; i++)
{
parse(dir.toString(),files[i]); // All the work is done here.
if (!useLogFile)
// Write messages to screen after parsing each file.
{
System.out.print(buff.toString());
buff = new StringBuffer();
}
}
}
else // Command-line argument is no good!
{
System.out.println(args[0] + " not found!");
return;
}
// Provide user with a summary.
buff.append("================SUMMARY=============================\n");
if (numXMLFiles > 1)
buff.append("Parsed " + numXMLFiles + " .xml files in " + args[0] + ".\n");
if (numValidFiles > 1)
buff.append( numValidFiles + " files are valid.\n");
else if (numValidFiles == 1)
buff.append( numValidFiles + " file is valid.\n");
if (numInvalidFiles > 1)
buff.append(numInvalidFiles + " files are not valid.\n");
else if (numInvalidFiles == 1)
buff.append( numInvalidFiles + " file is not valid.\n");
if (numMalformedFiles > 1)
buff.append(numMalformedFiles + " files are not well-formed.\n");
else if (numMalformedFiles == 1)
buff.append( numMalformedFiles + " file is not well-formed.\n");
if (numFilesMissingDoctype > 1)
buff.append(numFilesMissingDoctype + " files do not contain a DOCTYPE declaration.\n");
else if (numFilesMissingDoctype == 1)
buff.append(numFilesMissingDoctype + " file does not contain a DOCTYPE declaration.\n");
if (!useLogFile)
System.out.print(buff.toString());
else
{
// If log file exists, append.
FileWriter writer = new FileWriter(args[1], true);
writer.write(new java.util.Date().toString()+ "\n");
writer.write(buff.toString());
writer.close();
System.out.println("Done with validation. See " + args[1] + ".");
}
}
// Parse each XML file.
void parse(String dir, String filename)
throws FileNotFoundException, IOException, ParserConfigurationException, SAXException
{
try
{
File f = new File(dir, filename);
StringBuffer errorBuff = new StringBuffer();
InputSource input = new InputSource(new FileInputStream(f));
// Set systemID so parser can find the dtd with a relative URL in the source document.
input.setSystemId(f.toString());
SAXParserFactory spfact = SAXParserFactory.newInstance();
spfact.setValidating(true);
spfact.setNamespaceAware(true);
SAXParser parser = spfact.newSAXParser();
XMLReader reader = parser.getXMLReader();
//Instantiate inner-class error and lexical handler.
Handler handler = new Handler(filename, errorBuff);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
parser.parse(input, handler);
if (handler.containsDTD && !handler.errorOrWarning) // valid
{
buff.append("VALID " + filename +"\n");
numValidFiles++;
}
else if (handler.containsDTD) // not valid
{
buff.append ("NOT VALID " + filename + "\n");
buff.append(errorBuff.toString());
numInvalidFiles++;
}
else // no DOCTYPE to use for validation
{
buff.append("NO DOCTYPE DECLARATION " + filename + "\n");
numFilesMissingDoctype++;
}
}
catch (Exception e) // Serious problem!
{
buff.append("NOT WELL-FORMED " + filename + ". " + e.getMessage() + "\n");
numMalformedFiles++;
}
finally
{
numXMLFiles++;
}
}
// Inner classes
// Only interested in parsing .xml files.
class XMLFileFilter implements FilenameFilter
{
public boolean accept(File dir, String fileName)
{
return fileName.toLowerCase().endsWith(".xml") && new File(dir.toString(),fileName).isFile();
}
}
// Catch any errors or warnings, and verify presence of doctype statement.
class Handler extends DefaultHandler implements LexicalHandler
{
boolean errorOrWarning;
boolean containsDTD;
String sourceFile;
StringBuffer errorBuff;
Handler(String sourceFile, StringBuffer errorBuff)
{
super();
this.sourceFile = sourceFile;
this.errorBuff = errorBuff;
errorOrWarning = false;
containsDTD = false;
}
public void error(SAXParseException exc)
{
errorBuff.append(sourceFile + " Error: " + exc.getMessage()+ "\n");
errorOrWarning = true;
}
public void warning(SAXParseException exc)
{
errorBuff.append(sourceFile + " Warning:" + exc.getMessage()+ "\n");
errorOrWarning = true;
}
// LexicalHandler methods; all no-op except startDTD().
// Set containsDTD to true when startDTD event occurs.
public void startDTD (String name, String publicId, String systemId)
throws SAXException
{
containsDTD = true;
}
public void endDTD () throws SAXException
{}
public void startEntity (String name) throws SAXException
{}
public void endEntity (String name) throws SAXException
{}
public void startCDATA () throws SAXException
{}
public void endCDATA () throws SAXException
{}
public void comment (char ch[], int start, int length) throws SAXException
{}
}
}
|