KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > util > xml > SchemaValidator


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 JavaDoc;
22
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.SAXParseException JavaDoc;
25 import org.xml.sax.XMLReader JavaDoc;
26 import org.xml.sax.helpers.DefaultHandler JavaDoc;
27 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
28
29 public class SchemaValidator
30 {
31   public static void main(String JavaDoc[] args)
32   {
33     String JavaDoc parserClass = "org.apache.xerces.parsers.SAXParser";
34     String JavaDoc validationFeature = "http://xml.org/sax/features/validation";
35     String JavaDoc schemaFeature = "http://apache.org/xml/features/validation/schema";
36     try
37     {
38       String JavaDoc x = args[0];
39       XMLReader JavaDoc 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 JavaDoc e)
47     {
48       System.out.println(e.toString());
49     }
50     catch (IOException JavaDoc e)
51     {
52       System.out.println(e.toString());
53     }
54   }
55   private static class MyErrorHandler extends DefaultHandler JavaDoc
56   {
57     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc
58     {
59       System.out.println("Warning: ");
60       printInfo(e);
61     }
62     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc
63     {
64       System.out.println("Error: ");
65       printInfo(e);
66     }
67     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc
68     {
69       System.out.println("Fattal error: ");
70       printInfo(e);
71     }
72     private void printInfo(SAXParseException JavaDoc 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 }
82
Popular Tags