KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > conf > XmlValidator


1 // $Id: XmlValidator.java,v 1.5 2004/09/23 16:29:15 belaban Exp $
2

3 package org.jgroups.conf;
4
5 /**
6  * @author Filip Hanik (<a HREF="mailto:filip@filip.net">filip@filip.net)
7  * @version 1.0
8  */

9 //Add these lines to import the JAXP APIs you'll be using:
10
import org.jgroups.util.PrintXMLTree;
11 import org.jgroups.util.Util;
12 import org.w3c.dom.Document JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.SAXParseException JavaDoc;
16
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21 public class XmlValidator
22 {
23
24     private final String JavaDoc mXmlUrl;
25     private final String JavaDoc mDtdUrl;
26     private final boolean mPrintXml;
27     
28     public XmlValidator(String JavaDoc xmlUrl, String JavaDoc dtdUrl)
29     {
30         this(xmlUrl,dtdUrl,true);
31     }
32     
33     public XmlValidator(String JavaDoc xmlUrl, String JavaDoc dtdUrl, boolean printXml)
34     {
35         mXmlUrl = xmlUrl;
36         mDtdUrl = dtdUrl;
37         mPrintXml = printXml;
38     }
39     
40     protected static InputStream JavaDoc getInputStream(String JavaDoc url)
41         throws java.io.IOException JavaDoc
42     {
43         URL JavaDoc inurl = new URL JavaDoc(url);
44         return inurl.openStream();
45     }
46     
47     
48     public void validate() throws Exception JavaDoc
49     {
50         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
51         factory.setValidating(true); //for now
52

53         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
54         //Document document = builder.parse( this.getInputStream( mXmlUrl ),mDtdUrl );
55
builder.setEntityResolver(new JGEntityResolver(mDtdUrl));
56         JGErrorHandler errorhandler = new JGErrorHandler();
57         builder.setErrorHandler(errorhandler);
58         Document JavaDoc document = builder.parse( getInputStream( mXmlUrl ));
59         if ( mPrintXml )
60             PrintXMLTree.print(new java.io.PrintWriter JavaDoc(System.out),document.getDocumentElement());
61         System.out.println("\n\nError Report:");
62         System.out.println(errorhandler.getErrorString());
63         
64     }
65     
66     public static void main(String JavaDoc[] args)
67         throws Exception JavaDoc
68     {
69     
70         if ( args.length < 2 )
71         {
72             System.out.println("Usage: XmlValidator xmlUrl dtdUrl [printXml(true|false)]");
73             System.out.println("Example: XmlValidator file:/usr/local/test.xml file:conf/jgroups-protocol.dtd false");
74             System.exit(1);
75         }
76         boolean printXml = true;
77         if ( args.length > 2 )
78         {
79             try
80             {
81                 printXml = Boolean.valueOf(args[2]).booleanValue();
82             }
83             catch ( Exception JavaDoc x){ x.printStackTrace();}
84         }
85         XmlValidator xmlValidator = new XmlValidator(args[0],args[1],printXml);
86         xmlValidator.validate();
87     }
88     
89     class JGEntityResolver implements org.xml.sax.EntityResolver JavaDoc
90     {
91         private final String JavaDoc mDtdUrl;
92         public JGEntityResolver(String JavaDoc dtdUrl)
93         {
94             mDtdUrl = dtdUrl;
95         }
96         public InputSource JavaDoc resolveEntity(java.lang.String JavaDoc publicId,
97                                          java.lang.String JavaDoc systemId)
98                           throws SAXException JavaDoc,
99                                  java.io.IOException JavaDoc
100         {
101             
102             InputSource JavaDoc source = new InputSource JavaDoc(getInputStream(mDtdUrl));
103             return source;
104         }
105     }
106     
107     class JGErrorHandler implements org.xml.sax.ErrorHandler JavaDoc
108     {
109         private final StringBuffer JavaDoc mErrors = new StringBuffer JavaDoc();
110         int count = 1;
111         public void warning(SAXParseException JavaDoc exception)
112              throws SAXException JavaDoc
113         {
114             mErrors.append('\n').append(count++).append(". WARNING: ");
115             mErrors.append(Util.getStackTrace(exception));
116             mErrors.append('\n');
117         }
118         
119         public void error(SAXParseException JavaDoc exception)
120              throws SAXException JavaDoc
121         {
122             mErrors.append('\n').append(count++).append(". ERROR: ");
123             mErrors.append(Util.getStackTrace(exception));
124             mErrors.append('\n');
125         }
126         
127         public void fatalError(SAXParseException JavaDoc exception)
128              throws SAXException JavaDoc
129         {
130             mErrors.append('\n').append(count++).append(". FATAL ERROR: ");
131             mErrors.append(Util.getStackTrace(exception));
132             mErrors.append('\n');
133         }
134         
135         public String JavaDoc getErrorString()
136         {
137             if ( mErrors.length() == 0 ) return "No errors reported."; else return mErrors.toString();
138         }
139     
140     }
141 }
142
Popular Tags