1 package tests.org.w3c.dom; 2 3 import java.net.MalformedURLException; 4 import java.net.URL; 5 6 import javax.xml.parsers.DocumentBuilder; 7 8 import org.w3c.dom.Document; 9 import org.xml.sax.SAXException; 10 import org.xml.sax.SAXParseException; 11 12 import junit.framework.TestCase; 13 14 public class DOMTestCase extends TestCase { 15 16 public Document load(String docURI, DocumentBuilder builder) { 17 Document doc = load(resolveURI(docURI), builder); 18 return doc; 19 } 20 21 public Document load(URL url, DocumentBuilder builder) { 22 Document doc = null; 23 Exception parseException = null; 24 try { 25 LoadErrorHandler errorHandler = new LoadErrorHandler(); 26 builder.setErrorHandler(errorHandler); 27 doc = builder.parse(url.openStream()); 28 parseException = errorHandler.getFirstException(); 29 } catch (Exception ex) { 30 parseException = ex; 31 } 32 builder.setErrorHandler(null); 33 if (parseException != null) { 34 // fail("Unexpected exception " + parseException.getMessage()); 35 throw new RuntimeException("Unexpected exception " + parseException.getMessage(), parseException); 36 } 37 return doc; 38 } 39 40 public void preload(String contentType, String docURI, 41 boolean willBeModified) { 42 if ("text/html".equals(contentType) 43 || "application/xhtml+xml".equals(contentType)) { 44 if (docURI.startsWith("staff") 45 || docURI.equals("datatype_normalization")) { 46 47 } 48 } 49 } 50 51 private URL resolveURI(String baseURI) { 52 String docURI = baseURI + ".xml"; 53 54 URL resolvedURI = null; 55 try { 56 resolvedURI = new URL(docURI); 57 if (resolvedURI.getProtocol() != null) { 58 return resolvedURI; 59 } 60 } catch (MalformedURLException ex) { 61 // fail("Unexpected exception " + ex.getMessage()); 62 } 63 // 64 // build a URL for a test file in the JAR 65 // 66 resolvedURI = getClass().getResource("/" + docURI); 67 if (resolvedURI == null) { 68 // 69 // see if it is an absolute URI 70 // 71 int firstSlash = docURI.indexOf('/'); 72 try { 73 if (firstSlash == 0 74 || (firstSlash >= 1 && docURI.charAt(firstSlash - 1) == ':')) { 75 resolvedURI = new URL(docURI); 76 } else { 77 // 78 // try the files/level?/spec directory 79 // 80 String filename = getClass().getPackage().getName(); 81 filename = "tests/" 82 + filename.substring(14).replace('.', '/') 83 + "/files/" + docURI; 84 resolvedURI = new java.io.File(filename).toURL(); 85 } 86 } catch (MalformedURLException ex) { 87 fail("Unexpected exception " + ex.getMessage()); 88 } 89 } 90 91 if (resolvedURI == null) { 92 fail("resolvedURI is null "); 93 } 94 return resolvedURI; 95 } 96 97 98 public String getContentType() { 99 return "xml"; 100 } 101 102 public void assertURIEquals(String assertID, String scheme, String path, 103 String host, String file, String name, String query, 104 String fragment, Boolean isAbsolute, String actual) { 105 // 106 // URI must be non-null 107 assertNotNull(assertID, actual); 108 109 String uri = actual; 110 111 int lastPound = actual.lastIndexOf("#"); 112 String actualFragment = ""; 113 if (lastPound != -1) { 114 // 115 // substring before pound 116 // 117 uri = actual.substring(0, lastPound); 118 actualFragment = actual.substring(lastPound + 1); 119 } 120 if (fragment != null) { 121 assertEquals(assertID, fragment, actualFragment); 122 123 } 124 int lastQuestion = uri.lastIndexOf("?"); 125 String actualQuery = ""; 126 if (lastQuestion != -1) { 127 // 128 // substring before pound 129 // 130 uri = actual.substring(0, lastQuestion); 131 actualQuery = actual.substring(lastQuestion + 1); 132 } 133 if (query != null) { 134 assertEquals(assertID, query, actualQuery); 135 136 } 137 int firstColon = uri.indexOf(":"); 138 int firstSlash = uri.indexOf("/"); 139 String actualPath = uri; 140 String actualScheme = ""; 141 if (firstColon != -1 && firstColon < firstSlash) { 142 actualScheme = uri.substring(0, firstColon); 143 actualPath = uri.substring(firstColon + 1); 144 } 145 146 if (scheme != null) { 147 assertEquals(assertID, scheme, actualScheme); 148 } 149 150 if (path != null) { 151 assertEquals(assertID, path, actualPath); 152 } 153 154 if (host != null) { 155 String actualHost = ""; 156 if (actualPath.startsWith("//")) { 157 int termSlash = actualPath.indexOf("/", 2); 158 actualHost = actualPath.substring(0, termSlash); 159 } 160 assertEquals(assertID, host, actualHost); 161 } 162 163 String actualFile = actualPath; 164 if (file != null || name != null) { 165 int finalSlash = actualPath.lastIndexOf("/"); 166 if (finalSlash != -1) { 167 actualFile = actualPath.substring(finalSlash + 1); 168 } 169 if (file != null) { 170 assertEquals(assertID, file, actualFile); 171 } 172 } 173 174 if (name != null) { 175 String actualName = actualFile; 176 int finalPeriod = actualFile.lastIndexOf("."); 177 if (finalPeriod != -1) { 178 actualName = actualFile.substring(0, finalPeriod); 179 } 180 assertEquals(assertID, name, actualName); 181 } 182 183 if (isAbsolute != null) { 184 // 185 // Jar URL's will have any actual path like file:/c:/somedrive... 186 assertEquals(assertID, isAbsolute.booleanValue(), actualPath 187 .startsWith("/") 188 || actualPath.startsWith("file:/")); 189 } 190 } 191 192 193 private class LoadErrorHandler implements org.xml.sax.ErrorHandler { 194 private SAXException parseException; 195 196 private int errorCount; 197 198 private int warningCount; 199 200 public LoadErrorHandler() { 201 parseException = null; 202 errorCount = 0; 203 warningCount = 0; 204 } 205 206 public void error(SAXParseException ex) { 207 errorCount++; 208 if (parseException == null) { 209 parseException = ex; 210 } 211 } 212 213 public void warning(SAXParseException ex) { 214 warningCount++; 215 } 216 217 public void fatalError(SAXParseException ex) { 218 if (parseException == null) { 219 parseException = ex; 220 } 221 } 222 223 public SAXException getFirstException() { 224 return parseException; 225 } 226 } 227 } 228