1 package xdoclet.retest.test; 2 3 import junit.framework.Test; 4 import junit.framework.TestSuite; 5 import org.w3c.dom.Document ; 6 import org.w3c.dom.Node ; 7 import org.xml.sax.SAXParseException ; 8 import org.xml.sax.SAXException ; 9 import org.xml.sax.EntityResolver ; 10 import org.xml.sax.InputSource ; 11 import org.xml.sax.ErrorHandler ; 12 import xdoclet.XDocletException; 13 14 import javax.xml.parsers.DocumentBuilderFactory ; 15 import javax.xml.parsers.DocumentBuilder ; 16 import javax.xml.parsers.ParserConfigurationException ; 17 import java.io.IOException ; 18 import java.io.File ; 19 import java.io.FileNotFoundException ; 20 import java.util.Hashtable ; 21 22 27 public class XmlRegressionTestCase 28 extends XDocletRegressionTestCase 29 { 30 31 public XmlRegressionTestCase(String name) 32 { 33 super(name); 34 } 35 36 public XmlRegressionTestCase(String name, String cn) 37 { 38 super(name,cn); 39 } 40 41 public Test getSuite() 42 { 43 TestSuite suite= new TestSuite(); 44 return suite; 45 } 46 47 protected static Document readDocument(String uri) 48 throws XDocletException,FileNotFoundException 49 { 50 Document doc; 51 try{ 52 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 53 dbf.setValidating(false); 54 DocumentBuilder db = dbf.newDocumentBuilder(); 55 LocalResolver resolver = new LocalResolver(); 56 LocalErrorHandler error = new LocalErrorHandler("",resolver); 57 db.setEntityResolver(resolver); 58 db.setErrorHandler(error); 59 doc = db.parse(uri); 60 return doc; 61 }catch (ParserConfigurationException e){ 62 e.printStackTrace(); 63 throw new XDocletException("Parser Config Error"); 64 }catch (SAXParseException e){ 65 e.printStackTrace(); 66 throw new XDocletException("Parsing Error in "+uri+" at line "+e.getLineNumber()); 67 }catch (SAXException e){ 68 e.printStackTrace(); 69 throw new XDocletException("Parsing Error in "+uri); 70 }catch (IOException e){ 71 if (e instanceof FileNotFoundException ) 72 throw (FileNotFoundException )e; 73 e.printStackTrace(); 74 throw new XDocletException("IO Error in "+uri); 75 } 76 } 77 78 protected Node readReferenceNode(String file) 79 throws XDocletException,FileNotFoundException 80 { 81 return readDocument(refXmlBase + File.separator + getClassName() + File.separator + file); 82 } 83 84 protected Node readGeneratedNode(String file) 85 throws XDocletException,FileNotFoundException 86 { 87 return readDocument(genXmlBase + File.separator + getClassName() + File.separator + file); 88 } 89 90 91 private static class LocalResolver implements EntityResolver 92 { 93 private Hashtable dtds = new Hashtable (); 94 private boolean hasDTD = false; 95 96 public LocalResolver() 97 { 98 registerDTD("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN", "../../dtds/ejb11-jar.dtd"); 99 registerDTD("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN", "../../dtds/ejb20-jar.dtd"); 100 registerDTD("-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN", "../../dtds/application_1_2.dtd"); 101 registerDTD("-//Sun Microsystems, Inc.//DTD Connector 1.0//EN", "../../dtds/connector_1_0.dtd"); 102 registerDTD("-//JBoss//DTD JAWS 2.4//EN", "../../dtds/jaws_2_4.dtd"); 103 registerDTD("-//JBoss//DTD JAWS 3.0//EN", "../../dtds/jaws_3_0.dtd"); 104 registerDTD("-//JBoss//DTD JBOSS 2.4//EN","../../dtds/jboss_2_4.dtd"); 105 registerDTD("-//JBoss//DTD JBOSS 3.0//EN","../../dtds/jboss_3_0.dtd"); 106 } 107 108 113 public void registerDTD(String publicId, String dtdFileName) 114 { 115 dtds.put(publicId, dtdFileName); 116 } 117 118 125 public InputSource resolveEntity(String publicId, String systemId) 126 { 127 hasDTD = false; 128 String dtd = (String )dtds.get(publicId); 129 130 if (dtd != null) 131 { 132 hasDTD = true; 133 try 134 { 135 InputSource aInputSource = new InputSource (dtd); 136 return aInputSource; 137 } catch( Exception ex ) 138 { 139 } 141 } 142 return null; 143 } 144 145 149 public boolean hasDTD() 150 { 151 return hasDTD; 152 } 153 154 } 155 156 private static class LocalErrorHandler implements ErrorHandler 157 { 158 private String theFileName; 159 private LocalResolver localResolver; 160 161 public LocalErrorHandler( String inFileName, LocalResolver localResolver ) 162 { 163 this.theFileName = inFileName; 164 this.localResolver = localResolver; 165 } 166 167 public void error(SAXParseException exception) 168 { 169 if ( localResolver.hasDTD() ) 170 { 171 System.err.println("File " 172 + theFileName 173 + " process error. Line: " 174 + String.valueOf(exception.getLineNumber()) 175 + ". Error message: " 176 + exception.getMessage() 177 ); 178 } 179 } 180 181 public void fatalError(SAXParseException exception) 182 { 183 if ( localResolver.hasDTD() ) 184 { 185 System.err.println("File " 186 + theFileName 187 + " process fatal error. Line: " 188 + String.valueOf(exception.getLineNumber()) 189 + ". Error message: " 190 + exception.getMessage() 191 ); 192 } 193 } 194 195 public void warning(SAXParseException exception) 196 { 197 if ( localResolver.hasDTD() ) 198 { 199 System.err.println("File " 200 + theFileName 201 + " process warning. Line: " 202 + String.valueOf(exception.getLineNumber()) 203 + ". Error message: " 204 + exception.getMessage() 205 ); 206 } 207 } 208 } 209 210 } 211 | Popular Tags |