1 16 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileWriter ; 24 import java.io.FilenameFilter ; 25 import java.io.IOException ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.ext.LexicalHandler ; 36 import org.xml.sax.helpers.DefaultHandler ; 37 38 43 public class Validate 44 { 45 static int numXMLFiles = 0; 46 static int numValidFiles = 0; 47 static int numInvalidFiles = 0; 48 static int numFilesMissingDoctype = 0; 49 static int numMalformedFiles = 0; 50 static boolean useLogFile = false; 51 static StringBuffer buff = new StringBuffer (); 52 53 public static void main(String [] args) 54 throws FileNotFoundException , IOException , ParserConfigurationException , SAXException  55 { 56 if (args.length == 0 || args.length > 2) 57 { 58 System.out.println("\nEnter 'java validate -help' for information about running Validate"); 59 return; 60 } 61 if (args[0].toLowerCase().equals("-help")) 62 { 63 String sep = "\n====================================================\n"; 64 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"; 65 String b = "Each xml file should contain a DOCTYPE declaration.\n\n"; 66 String c = "Validate takes 1 or 2 arguments:\n"; 67 String d = " Argument 1 specifies a directory or an individual xml file.\n"; 68 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"; 69 System.out.println(sep+a+b+c+d+e+sep); 70 return; 71 } 72 try 73 { 74 Validate v = new Validate(); 75 v.validate(args); 76 } 77 catch (Exception e) 78 { 79 e.printStackTrace(); 80 } 81 } 82 83 void validate(String [] args) 84 throws FileNotFoundException , IOException , ParserConfigurationException , SAXException  85 { 86 File dir = new File (args[0]); 87 88 useLogFile = (args.length == 2); 90 91 if (dir.isFile()) { 93 parse(null,args[0]); 94 } 95 else if (dir.isDirectory()) { 97 XMLFileFilter filter = new XMLFileFilter(); 99 String [] files = dir.list(filter); 100 for (int i = 0; i <files.length; i++) 101 { 102 parse(dir.toString(),files[i]); 104 if (!useLogFile) 105 { 107 System.out.print(buff.toString()); 108 buff = new StringBuffer (); 109 } 110 } 111 } 112 else { 114 System.out.println(args[0] + " not found!"); 115 return; 116 } 117 buff.append("================SUMMARY=============================\n"); 119 if (numXMLFiles > 1) 120 buff.append("Parsed " + numXMLFiles + " .xml files in " + args[0] + ".\n"); 121 if (numValidFiles > 1) 122 buff.append( numValidFiles + " files are valid.\n"); 123 else if (numValidFiles == 1) 124 buff.append( numValidFiles + " file is valid.\n"); 125 if (numInvalidFiles > 1) 126 buff.append(numInvalidFiles + " files are not valid.\n"); 127 else if (numInvalidFiles == 1) 128 buff.append( numInvalidFiles + " file is not valid.\n"); 129 if (numMalformedFiles > 1) 130 buff.append(numMalformedFiles + " files are not well-formed.\n"); 131 else if (numMalformedFiles == 1) 132 buff.append( numMalformedFiles + " file is not well-formed.\n"); 133 if (numFilesMissingDoctype > 1) 134 buff.append(numFilesMissingDoctype + " files do not contain a DOCTYPE declaration.\n"); 135 else if (numFilesMissingDoctype == 1) 136 buff.append(numFilesMissingDoctype + " file does not contain a DOCTYPE declaration.\n"); 137 138 if (!useLogFile) 139 System.out.print(buff.toString()); 140 else 141 { 142 FileWriter writer = new FileWriter (args[1], true); 144 writer.write(new java.util.Date ().toString()+ "\n"); 145 writer.write(buff.toString()); 146 writer.close(); 147 System.out.println("Done with validation. See " + args[1] + "."); 148 } 149 } 150 151 void parse(String dir, String filename) 153 throws FileNotFoundException , IOException , ParserConfigurationException , SAXException  154 { 155 try 156 { 157 File f = new File (dir, filename); 158 StringBuffer errorBuff = new StringBuffer (); 159 InputSource input = new InputSource (new FileInputStream (f)); 160 input.setSystemId(f.toString()); 162 SAXParserFactory spfact = SAXParserFactory.newInstance(); 163 164 spfact.setValidating(true); 165 spfact.setNamespaceAware(true); 166 167 SAXParser parser = spfact.newSAXParser(); 168 XMLReader reader = parser.getXMLReader(); 169 170 Handler handler = new Handler (filename, errorBuff); 172 reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 173 parser.parse(input, handler); 174 175 if (handler.containsDTD && !handler.errorOrWarning) { 177 buff.append("VALID " + filename +"\n"); 178 numValidFiles++; 179 } 180 else if (handler.containsDTD) { 182 buff.append ("NOT VALID " + filename + "\n"); 183 buff.append(errorBuff.toString()); 184 numInvalidFiles++; 185 } 186 else { 188 buff.append("NO DOCTYPE DECLARATION " + filename + "\n"); 189 numFilesMissingDoctype++; 190 } 191 } 192 catch (Exception e) { 194 buff.append("NOT WELL-FORMED " + filename + ". " + e.getMessage() + "\n"); 195 numMalformedFiles++; 196 } 197 finally 198 { 199 numXMLFiles++; 200 } 201 } 202 204 class XMLFileFilter implements FilenameFilter  206 { 207 public boolean accept(File dir, String fileName) 208 { 209 return fileName.toLowerCase().endsWith(".xml") && new File (dir.toString(),fileName).isFile(); 210 } 211 } 212 213 class Handler extends DefaultHandler implements LexicalHandler  215 { 216 boolean errorOrWarning; 217 boolean containsDTD; 218 String sourceFile; 219 StringBuffer errorBuff; 220 221 Handler(String sourceFile, StringBuffer errorBuff) 222 { 223 super(); 224 this.sourceFile = sourceFile; 225 this.errorBuff = errorBuff; 226 errorOrWarning = false; 227 containsDTD = false; 228 } 229 230 public void error(SAXParseException exc) 231 { 232 errorBuff.append(sourceFile + " Error: " + exc.getMessage()+ "\n"); 233 errorOrWarning = true; 234 } 235 public void warning(SAXParseException exc) 236 { 237 errorBuff.append(sourceFile + " Warning:" + exc.getMessage()+ "\n"); 238 errorOrWarning = true; 239 } 240 241 243 public void startDTD (String name, String publicId, String systemId) 245 throws SAXException  246 { 247 containsDTD = true; 248 } 249 250 public void endDTD () throws SAXException  251 {} 252 253 public void startEntity (String name) throws SAXException  254 {} 255 256 public void endEntity (String name) throws SAXException  257 {} 258 259 public void startCDATA () throws SAXException  260 {} 261 262 public void endCDATA () throws SAXException  263 {} 264 265 public void comment (char ch[], int start, int length) throws SAXException  266 {} 267 } 268 } 269
| Popular Tags
|