1 24 25 package org.objectweb.cjdbc.common.xml; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.StringReader ; 33 import java.util.ArrayList ; 34 35 import org.objectweb.cjdbc.common.i18n.Translate; 36 import org.objectweb.cjdbc.common.util.Constants; 37 import org.xml.sax.ErrorHandler ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.SAXParseException ; 41 import org.xml.sax.XMLReader ; 42 import org.xml.sax.ext.LexicalHandler ; 43 import org.xml.sax.helpers.DefaultHandler ; 44 import org.xml.sax.helpers.XMLReaderFactory ; 45 46 51 public class XmlValidator extends DefaultHandler 52 implements 53 ErrorHandler , 54 LexicalHandler 55 { 56 57 58 private XMLReader parser; 59 private String pathToDtd; 60 private boolean isXmlValid = false; 61 private boolean isDtdValid = false; 62 private String xmlContent; 63 private ArrayList errors; 64 private ArrayList warnings; 65 66 72 public static void main(String [] args) throws Exception 73 { 74 if (args.length < 1 || args.length > 2) 75 { 76 System.out.println("usage: XmlValidator [xmlFile] ([dtd]) "); 77 System.exit(0); 78 } 79 80 String fileName = args[0]; 81 String dtdName = Constants.C_JDBC_DTD_FILE; 82 if (args.length == 2) 83 dtdName = args[1]; 84 else 85 System.out.println("Using default DTD:" + Constants.C_JDBC_DTD_FILE); 86 87 File dtd = null; 88 dtd = new File (ClassLoader.getSystemResource(dtdName).getFile()); 89 File xmlFile = null; 90 try 91 { 92 xmlFile = new File (ClassLoader.getSystemResource(fileName).getFile()); 93 } 94 catch (RuntimeException e) 95 { 96 xmlFile = new File (fileName); 97 } 98 99 if (!dtd.exists()) 100 { 101 System.out.println("Cannot find specified dtd"); 102 System.exit(1); 103 } 104 if (!xmlFile.exists()) 105 { 106 System.out.println("Cannot find specified xml file"); 107 System.exit(1); 108 } 109 110 System.out.println("Validating:\tFile:" + xmlFile.getName() + " with dtd:" 111 + dtd.getName()); 112 113 XmlValidator validator = new XmlValidator(dtd.getAbsolutePath(), 115 new FileReader (xmlFile)); 116 117 if (!validator.isDtdValid()) 119 System.out.println("[FAILED:Dtd is not valid]"); 120 else if (!validator.isXmlValid()) 121 System.out.println("[FAILED:xml is not valid]"); 122 else if (validator.isXmlValid()) 123 System.out.println("[OK]"); 124 125 if (validator.getLastException() != null) 126 { 127 ArrayList errors = validator.getExceptions(); 128 for (int i = 0; i < errors.size(); i++) 129 System.out.println("\t(parsing error):" 130 + ((Exception ) errors.get(i)).getMessage()); 131 } 132 } 133 134 140 public XmlValidator(String pathToDtd, String xml) 141 { 142 validate(pathToDtd, xml); 143 } 144 145 148 public XmlValidator(String pathToDtd, FileReader file) throws IOException 149 { 150 BufferedReader in = new BufferedReader (file); 152 StringBuffer xml = new StringBuffer (); 153 String line; 154 do 155 { 156 line = in.readLine(); 157 if (line != null) 158 xml.append(line.trim()); 159 } 160 while (line != null); 161 xmlContent = xml.toString(); 162 validate(pathToDtd, xmlContent); 163 } 164 165 170 public String getXmlContent() 171 { 172 return xmlContent; 173 } 174 175 181 public void validate(String pathToDtd, String xml) 182 { 183 System.setProperty("org.xml.sax.driver", 184 "org.apache.crimson.parser.XMLReaderImpl"); 185 errors = new ArrayList (); 186 warnings = new ArrayList (); 187 try 188 { 189 this.pathToDtd = pathToDtd; 191 parser = XMLReaderFactory.createXMLReader(); 193 parser.setFeature("http://xml.org/sax/features/validation", true); 195 parser.setErrorHandler(this); 197 parser.setContentHandler(this); 199 parser.setProperty("http://xml.org/sax/properties/lexical-handler", this); 200 parser.setEntityResolver(this); 202 InputSource input = new InputSource (new StringReader (xml)); 203 parser.parse(input); 204 } 205 catch (Exception e) 206 { 207 addError(e); 210 isXmlValid = false; 211 } 212 } 213 214 222 public InputSource resolveEntity(String publicId, String systemId) 223 throws SAXException 224 { 225 226 File dtd = new File (pathToDtd); 227 if (dtd.exists()) 228 { 229 try 230 { 231 FileReader reader = new FileReader (dtd); 232 return new InputSource (reader); 233 } 234 catch (Exception e) 235 { } 237 } 238 239 InputStream stream = XmlValidator.class 240 .getResourceAsStream("/" + pathToDtd); 241 if (stream == null) 242 { 243 SAXException sax = new SAXException (Translate.get( 244 "virtualdatabase.xml.dtd.not.found", pathToDtd)); 245 addError(sax); 246 throw sax; 247 } 248 249 return new InputSource (stream); 250 } 251 252 255 public void error(SAXParseException exception) throws SAXException 256 { 257 addError(exception); 258 } 259 260 263 public void fatalError(SAXParseException exception) throws SAXException 264 { 265 addError(exception); 266 } 267 268 271 public void warning(SAXParseException exception) throws SAXException 272 { 273 warnings.add(exception); 274 } 275 276 279 public void endDocument() throws SAXException 280 { 281 if (errors.size() == 0) 282 this.isXmlValid = true; 283 } 284 285 288 public boolean isValid() 289 { 290 return isXmlValid && isDtdValid; 291 } 292 293 298 public Exception getLastException() 299 { 300 if (errors.size() == 0) 301 return null; 302 else 303 return (Exception ) errors.get(errors.size() - 1); 304 } 305 306 311 public ArrayList getExceptions() 312 { 313 return errors; 314 } 315 316 319 public void comment(char[] ch, int start, int length) throws SAXException 320 { 321 } 322 323 326 public void endCDATA() throws SAXException 327 { 328 } 329 330 333 public void endDTD() throws SAXException 334 { 335 if (errors.size() == 0) 336 { 337 isDtdValid = true; 338 } 339 else 340 { 341 isDtdValid = false; 342 } 343 } 344 345 348 public void endEntity(String name) throws SAXException 349 { 350 } 351 352 355 public void startCDATA() throws SAXException 356 { 357 } 358 359 363 public void startDTD(String name, String publicId, String systemId) 364 throws SAXException 365 { 366 } 367 368 371 public void startEntity(String name) throws SAXException 372 { 373 } 374 375 378 public boolean isDtdValid() 379 { 380 return isDtdValid; 381 } 382 383 386 public void setDtdValid(boolean isDtdValid) 387 { 388 this.isDtdValid = isDtdValid; 389 } 390 391 394 public boolean isXmlValid() 395 { 396 return isXmlValid; 397 } 398 399 402 public void setXmlValid(boolean isXmlValid) 403 { 404 this.isXmlValid = isXmlValid; 405 } 406 407 private void addError(Exception e) 408 { 409 errors.add(e); 410 } 411 412 415 public ArrayList getWarnings() 416 { 417 return warnings; 418 } 419 }
| Popular Tags
|