View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.util.xml;
17  
18  /***
19   * @author anou_mana@users.sourceforge.net
20   */
21  import java.io.IOException;
22  
23  import org.xml.sax.SAXException;
24  import org.xml.sax.SAXParseException;
25  import org.xml.sax.XMLReader;
26  import org.xml.sax.helpers.DefaultHandler;
27  import org.xml.sax.helpers.XMLReaderFactory;
28  
29  public class SchemaValidator
30  {
31    public static void main(String[] args)
32    {
33      String parserClass = "org.apache.xerces.parsers.SAXParser";
34      String validationFeature = "http://xml.org/sax/features/validation";
35      String schemaFeature = "http://apache.org/xml/features/validation/schema";
36      try
37      {
38        String x = args[0];
39        XMLReader r = XMLReaderFactory.createXMLReader(parserClass);
40        r.setFeature(validationFeature, true);
41        r.setFeature(schemaFeature, true);
42        r.setErrorHandler(new MyErrorHandler());
43        r.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","C:/Projects/sample/xsd/dictionary.xsd");
44        r.parse(x);
45      }
46      catch (SAXException e)
47      {
48        System.out.println(e.toString());
49      }
50      catch (IOException e)
51      {
52        System.out.println(e.toString());
53      }
54    }
55    private static class MyErrorHandler extends DefaultHandler
56    {
57      public void warning(SAXParseException e) throws SAXException
58      {
59        System.out.println("Warning: ");
60        printInfo(e);
61      }
62      public void error(SAXParseException e) throws SAXException
63      {
64        System.out.println("Error: ");
65        printInfo(e);
66      }
67      public void fatalError(SAXParseException e) throws SAXException
68      {
69        System.out.println("Fattal error: ");
70        printInfo(e);
71      }
72      private void printInfo(SAXParseException e)
73      {
74        System.out.println("   Public ID: " + e.getPublicId());
75        System.out.println("   System ID: " + e.getSystemId());
76        System.out.println("   Line number: " + e.getLineNumber());
77        System.out.println("   Column number: " + e.getColumnNumber());
78        System.out.println("   Message: " + e.getMessage());
79      }
80    }
81  }