KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ValidateAllBySchema


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 import java.io.File JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import javax.xml.parsers.SAXParser JavaDoc;
24 import javax.xml.parsers.SAXParserFactory JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.SAXParseException JavaDoc;
27 import org.xml.sax.helpers.DefaultHandler JavaDoc;
28
29 /**
30  * Tries to validate all the example XML metadata files
31  * according to available XML schemas.
32  * @author Jesse Glick
33  */

34 public class ValidateAllBySchema {
35
36     /**
37      * args[0] = comma-separated list of files to validate
38      * args[1] = comma-separated list of available XML Schema files
39      */

40     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
41         File JavaDoc[] xmls = split(args[0]);
42         File JavaDoc[] schemas = split(args[1]);
43         String JavaDoc[] schemaUris = new String JavaDoc[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 JavaDoc f;
49         // #46847: needs to work on both JDK 1.4 and 1.5.
50
try {
51             f = (SAXParserFactory JavaDoc)Class.forName("com.sun.org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
52         } catch (ClassNotFoundException JavaDoc e) {
53             f = (SAXParserFactory JavaDoc)Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
54         }
55         f.setNamespaceAware(true);
56         f.setValidating(true);
57         SAXParser JavaDoc 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 JavaDoc());
67             } catch (SAXParseException JavaDoc 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 JavaDoc {
76         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
77             throw e;
78         }
79         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
80             throw e;
81         }
82         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
83             throw e;
84         }
85     }
86     private static File JavaDoc[] split(String JavaDoc s) {
87         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(s, ",");
88         File JavaDoc[] files = new File JavaDoc[tok.countTokens()];
89         for (int i = 0; i < files.length; i++) {
90             files[i] = new File JavaDoc(tok.nextToken());
91         }
92         return files;
93     }
94 }
95
Popular Tags