1 22 23 package org.continuent.sequoia.common.xml; 24 25 import java.io.BufferedReader ; 26 import java.io.File ; 27 import java.io.FileReader ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.StringReader ; 31 import java.util.ArrayList ; 32 33 import org.continuent.sequoia.common.i18n.Translate; 34 import org.continuent.sequoia.controller.core.ControllerConstants; 35 import org.xml.sax.ErrorHandler ; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.SAXParseException ; 39 import org.xml.sax.XMLReader ; 40 import org.xml.sax.ext.LexicalHandler ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 import org.xml.sax.helpers.XMLReaderFactory ; 43 44 49 public class XmlValidator extends DefaultHandler 50 implements 51 ErrorHandler , 52 LexicalHandler 53 { 54 55 56 private XMLReader parser; 57 private String pathToDtd; 58 private boolean isXmlValid = false; 59 private boolean isDtdValid = false; 60 private String xmlContent; 61 private ArrayList errors; 62 private ArrayList warnings; 63 64 70 public static void main(String [] args) throws Exception 71 { 72 if (args.length < 1 || args.length > 2) 73 { 74 System.out.println("usage: XmlValidator [xmlFile] ([dtd]) "); 75 System.exit(0); 76 } 77 78 String fileName = args[0]; 79 String dtdName = ControllerConstants.SEQUOIA_DTD_FILE; 80 if (args.length == 2) 81 dtdName = args[1]; 82 else 83 System.out.println("Using default DTD:" + ControllerConstants.SEQUOIA_DTD_FILE); 84 85 File dtd = null; 86 dtd = new File (ClassLoader.getSystemResource(dtdName).getFile()); 87 File xmlFile = null; 88 try 89 { 90 xmlFile = new File (ClassLoader.getSystemResource(fileName).getFile()); 91 } 92 catch (RuntimeException e) 93 { 94 xmlFile = new File (fileName); 95 } 96 97 if (!dtd.exists()) 98 { 99 System.out.println("Cannot find specified dtd"); 100 System.exit(1); 101 } 102 if (!xmlFile.exists()) 103 { 104 System.out.println("Cannot find specified xml file"); 105 System.exit(1); 106 } 107 108 System.out.println("Validating:\tFile:" + xmlFile.getName() + " with dtd:" 109 + dtd.getName()); 110 111 XmlValidator validator = new XmlValidator(dtd.getAbsolutePath(), 113 new FileReader (xmlFile)); 114 115 if (!validator.isDtdValid()) 117 System.out.println("[FAILED:Dtd is not valid]"); 118 else if (!validator.isXmlValid()) 119 System.out.println("[FAILED:xml is not valid]"); 120 else if (validator.isXmlValid()) 121 System.out.println("[OK]"); 122 123 if (validator.getLastException() != null) 124 { 125 ArrayList errors = validator.getExceptions(); 126 for (int i = 0; i < errors.size(); i++) 127 System.out.println("\t(parsing error):" 128 + ((Exception ) errors.get(i)).getMessage()); 129 } 130 } 131 132 138 public XmlValidator(String pathToDtd, String xml) 139 { 140 validate(pathToDtd, xml); 141 } 142 143 146 public XmlValidator(String pathToDtd, FileReader file) throws IOException 147 { 148 BufferedReader in = new BufferedReader (file); 150 StringBuffer xml = new StringBuffer (); 151 String line; 152 do 153 { 154 line = in.readLine(); 155 if (line != null) 156 xml.append(line.trim()); 157 } 158 while (line != null); 159 xmlContent = xml.toString(); 160 validate(pathToDtd, xmlContent); 161 } 162 163 168 public String getXmlContent() 169 { 170 return xmlContent; 171 } 172 173 179 public void validate(String pathToDtd, String xml) 180 { 181 System.setProperty("org.xml.sax.driver", 182 "org.apache.crimson.parser.XMLReaderImpl"); 183 errors = new ArrayList (); 184 warnings = new ArrayList (); 185 try 186 { 187 this.pathToDtd = pathToDtd; 189 parser = XMLReaderFactory.createXMLReader(); 191 parser.setFeature("http://xml.org/sax/features/validation", true); 193 parser.setErrorHandler(this); 195 parser.setContentHandler(this); 197 parser.setProperty("http://xml.org/sax/properties/lexical-handler", this); 198 parser.setEntityResolver(this); 200 InputSource input = new InputSource (new StringReader (xml)); 201 parser.parse(input); 202 } 203 catch (Exception e) 204 { 205 addError(e); 208 isXmlValid = false; 209 } 210 } 211 212 220 public InputSource resolveEntity(String publicId, String systemId) 221 throws SAXException 222 { 223 224 File dtd = new File (pathToDtd); 225 if (dtd.exists()) 226 { 227 try 228 { 229 FileReader reader = new FileReader (dtd); 230 return new InputSource (reader); 231 } 232 catch (Exception e) 233 { } 235 } 236 237 InputStream stream = XmlValidator.class 238 .getResourceAsStream("/" + pathToDtd); 239 if (stream == null) 240 { 241 SAXException sax = new SAXException (Translate.get( 242 "virtualdatabase.xml.dtd.not.found", ControllerConstants.PRODUCT_NAME, pathToDtd)); addError(sax); 244 throw sax; 245 } 246 247 return new InputSource (stream); 248 } 249 250 253 public void error(SAXParseException exception) throws SAXException 254 { 255 addError(exception); 256 } 257 258 261 public void fatalError(SAXParseException exception) throws SAXException 262 { 263 addError(exception); 264 } 265 266 269 public void warning(SAXParseException exception) throws SAXException 270 { 271 warnings.add(exception); 272 } 273 274 277 public void endDocument() throws SAXException 278 { 279 if (errors.size() == 0) 280 this.isXmlValid = true; 281 } 282 283 286 public boolean isValid() 287 { 288 return isXmlValid && isDtdValid; 289 } 290 291 296 public Exception getLastException() 297 { 298 if (errors.size() == 0) 299 return null; 300 else 301 return (Exception ) errors.get(errors.size() - 1); 302 } 303 304 309 public ArrayList getExceptions() 310 { 311 return errors; 312 } 313 314 317 public void comment(char[] ch, int start, int length) throws SAXException 318 { 319 } 320 321 324 public void endCDATA() throws SAXException 325 { 326 } 327 328 331 public void endDTD() throws SAXException 332 { 333 if (errors.size() == 0) 334 { 335 isDtdValid = true; 336 } 337 else 338 { 339 isDtdValid = false; 340 } 341 } 342 343 346 public void endEntity(String name) throws SAXException 347 { 348 } 349 350 353 public void startCDATA() throws SAXException 354 { 355 } 356 357 361 public void startDTD(String name, String publicId, String systemId) 362 throws SAXException 363 { 364 } 365 366 369 public void startEntity(String name) throws SAXException 370 { 371 } 372 373 376 public boolean isDtdValid() 377 { 378 return isDtdValid; 379 } 380 381 384 public void setDtdValid(boolean isDtdValid) 385 { 386 this.isDtdValid = isDtdValid; 387 } 388 389 392 public boolean isXmlValid() 393 { 394 return isXmlValid; 395 } 396 397 400 public void setXmlValid(boolean isXmlValid) 401 { 402 this.isXmlValid = isXmlValid; 403 } 404 405 private void addError(Exception e) 406 { 407 errors.add(e); 408 } 409 410 413 public ArrayList getWarnings() 414 { 415 return warnings; 416 } 417 } | Popular Tags |