1 3 package org.jgroups.conf; 4 5 9 import org.jgroups.util.PrintXMLTree; 11 import org.jgroups.util.Util; 12 import org.w3c.dom.Document ; 13 import org.xml.sax.InputSource ; 14 import org.xml.sax.SAXException ; 15 import org.xml.sax.SAXParseException ; 16 17 import javax.xml.parsers.DocumentBuilder ; 18 import javax.xml.parsers.DocumentBuilderFactory ; 19 import java.io.InputStream ; 20 import java.net.URL ; 21 public class XmlValidator 22 { 23 24 private final String mXmlUrl; 25 private final String mDtdUrl; 26 private final boolean mPrintXml; 27 28 public XmlValidator(String xmlUrl, String dtdUrl) 29 { 30 this(xmlUrl,dtdUrl,true); 31 } 32 33 public XmlValidator(String xmlUrl, String dtdUrl, boolean printXml) 34 { 35 mXmlUrl = xmlUrl; 36 mDtdUrl = dtdUrl; 37 mPrintXml = printXml; 38 } 39 40 protected static InputStream getInputStream(String url) 41 throws java.io.IOException 42 { 43 URL inurl = new URL (url); 44 return inurl.openStream(); 45 } 46 47 48 public void validate() throws Exception 49 { 50 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 51 factory.setValidating(true); 53 DocumentBuilder builder = factory.newDocumentBuilder(); 54 builder.setEntityResolver(new JGEntityResolver(mDtdUrl)); 56 JGErrorHandler errorhandler = new JGErrorHandler(); 57 builder.setErrorHandler(errorhandler); 58 Document document = builder.parse( getInputStream( mXmlUrl )); 59 if ( mPrintXml ) 60 PrintXMLTree.print(new java.io.PrintWriter (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 [] args) 67 throws Exception 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 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 90 { 91 private final String mDtdUrl; 92 public JGEntityResolver(String dtdUrl) 93 { 94 mDtdUrl = dtdUrl; 95 } 96 public InputSource resolveEntity(java.lang.String publicId, 97 java.lang.String systemId) 98 throws SAXException , 99 java.io.IOException 100 { 101 102 InputSource source = new InputSource (getInputStream(mDtdUrl)); 103 return source; 104 } 105 } 106 107 class JGErrorHandler implements org.xml.sax.ErrorHandler 108 { 109 private final StringBuffer mErrors = new StringBuffer (); 110 int count = 1; 111 public void warning(SAXParseException exception) 112 throws SAXException 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 exception) 120 throws SAXException 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 exception) 128 throws SAXException 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 getErrorString() 136 { 137 if ( mErrors.length() == 0 ) return "No errors reported."; else return mErrors.toString(); 138 } 139 140 } 141 } 142 | Popular Tags |