1 19 20 import java.io.File ; 21 import java.util.Arrays ; 22 import java.util.StringTokenizer ; 23 import javax.xml.parsers.SAXParser ; 24 import javax.xml.parsers.SAXParserFactory ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.SAXParseException ; 27 import org.xml.sax.helpers.DefaultHandler ; 28 29 34 public class ValidateAllBySchema { 35 36 40 public static void main(String [] args) throws Exception { 41 File [] xmls = split(args[0]); 42 File [] schemas = split(args[1]); 43 String [] schemaUris = new String [schemas.length]; 44 for (int i = 0; i < schemas.length; i++) { 45 schemaUris[i] = schemas[i].toURI().toString(); 46 } 47 System.err.println("Validating against " + Arrays.asList(schemas)); 48 SAXParserFactory f; 49 try { 51 f = (SAXParserFactory )Class.forName("com.sun.org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); 52 } catch (ClassNotFoundException e) { 53 f = (SAXParserFactory )Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); 54 } 55 f.setNamespaceAware(true); 56 f.setValidating(true); 57 SAXParser p = f.newSAXParser(); 58 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 59 "http://www.w3.org/2001/XMLSchema"); 60 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", 61 schemaUris); 62 int exit = 0; 63 for (int i = 0; i < xmls.length; i++) { 64 System.err.println("Parsing " + xmls[i] + "..."); 65 try { 66 p.parse(xmls[i].toURI().toString(), new Handler ()); 67 } catch (SAXParseException e) { 68 System.err.println(e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage()); 69 exit = 1; 70 } 71 } 72 System.err.println("All files validated."); 73 System.exit(exit); 74 } 75 private static final class Handler extends DefaultHandler { 76 public void warning(SAXParseException e) throws SAXException { 77 throw e; 78 } 79 public void error(SAXParseException e) throws SAXException { 80 throw e; 81 } 82 public void fatalError(SAXParseException e) throws SAXException { 83 throw e; 84 } 85 } 86 private static File [] split(String s) { 87 StringTokenizer tok = new StringTokenizer (s, ","); 88 File [] files = new File [tok.countTokens()]; 89 for (int i = 0; i < files.length; i++) { 90 files[i] = new File (tok.nextToken()); 91 } 92 return files; 93 } 94 } 95 | Popular Tags |