1 21 22 23 package nu.xom.tests; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.StringWriter ; 28 29 import nu.xom.Attribute; 30 import nu.xom.Builder; 31 import nu.xom.Comment; 32 import nu.xom.DocType; 33 import nu.xom.Document; 34 import nu.xom.Element; 35 import nu.xom.ParsingException; 36 import nu.xom.ProcessingInstruction; 37 import nu.xom.converters.SAXConverter; 38 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.ContentHandler ; 41 import org.xml.sax.Locator ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.ext.LexicalHandler ; 44 import org.xml.sax.helpers.DefaultHandler ; 45 46 56 public class SAXConverterTest extends XOMTestCase { 57 58 59 public SAXConverterTest(String name) { 60 super(name); 61 } 62 63 64 private DefaultHandler handler; 65 private SAXConverter converter; 66 private Builder builder = new Builder(); 67 68 69 protected void setUp() { 70 handler = new DefaultHandler (); 71 converter = new SAXConverter(handler); 72 } 73 74 75 public void testGetContentHandler() { 76 assertEquals(handler, converter.getContentHandler()); 77 } 78 79 80 public void testSetContentHandler() { 81 82 handler = new DefaultHandler (); 83 converter.setContentHandler(handler); 84 assertEquals(handler, converter.getContentHandler()); 85 86 try { 87 converter.setContentHandler(null); 88 fail("Allowed null ContentHandler"); 89 } 90 catch (NullPointerException success) { 91 } 93 94 } 95 96 97 public void testSetAndGetLexicalHandler() { 98 99 LexicalHandler handler = new XMLWriter(); 100 converter.setLexicalHandler(handler); 101 assertEquals(handler, converter.getLexicalHandler()); 102 103 converter.setLexicalHandler(null); 104 assertNull(converter.getLexicalHandler()); 105 106 } 107 108 109 private void convertAndCompare(Document doc) 110 throws IOException , SAXException , ParsingException { 111 112 StringWriter result = new StringWriter (); 113 XMLWriter handler = new XMLWriter(result); 114 converter.setContentHandler(handler); 115 converter.setLexicalHandler(handler); 116 converter.convert(doc); 117 result.flush(); 118 result.close(); 119 String convertedDoc = result.toString(); 120 Document rebuiltDoc = builder.build(convertedDoc, doc.getBaseURI()); 121 assertEquals(doc, rebuiltDoc); 122 123 } 124 125 126 public void testSimplestDoc() 127 throws IOException , SAXException , ParsingException { 128 Document doc = new Document(new Element("a")); 129 convertAndCompare(doc); 130 } 131 132 133 public void testXMLBaseAttributesAreThrownAway() 134 throws SAXException { 135 136 Element root = new Element("root"); 137 Element child = new Element("child"); 138 Attribute base = new Attribute("xml:base", 139 "http://www.w3.org/XML/1998/namespace", "data"); 140 child.addAttribute(base); 141 root.appendChild(child); 142 Document doc = new Document(root); 143 doc.setBaseURI("http://www.example.com/"); 144 converter.setContentHandler(new BaseChecker()); 145 converter.convert(doc); 146 147 } 148 149 150 private static class BaseChecker extends DefaultHandler { 151 152 private Locator locator; 153 154 public void setDocumentLocator(Locator locator) { 155 this.locator = locator; 156 } 157 158 public void startElement(String localName, String qualifiedName, 159 String namespaceURI, Attributes attributes) 160 throws SAXException { 161 162 if (localName.equals("root")) { 163 assertEquals("http://www.example.com/", locator.getSystemId()); 164 } 165 else if (localName.equals("child")) { 166 assertEquals("http://www.example.com/data", locator.getSystemId()); 167 } 168 169 for (int i=0; i < attributes.getLength(); i++) { 170 String name = attributes.getLocalName(i); 171 String uri = attributes.getURI(i); 172 if ("base".equals(name) 173 && "http://www.w3.org/XML/1998/namespace".equals(uri)) { 174 fail("Passed xml:base attribute into SAXConverter"); 175 } 176 } 177 178 } 179 180 } 181 182 183 184 public void testDocType() 185 throws IOException , SAXException , ParsingException { 186 Document doc = new Document(new Element("a")); 187 doc.setDocType(new DocType("root")); 188 convertAndCompare(doc); 189 } 190 191 192 public void testProcessingInstruction() 193 throws IOException , SAXException , ParsingException { 194 195 Document doc = new Document(new Element("a")); 196 doc.insertChild(new ProcessingInstruction( 197 "xml-stylesheet", "type=\"application/xml\" HREF=\"stylesheet.xsl\""), 0); 198 convertAndCompare(doc); 199 200 } 201 202 203 public void testComment() 204 throws IOException , SAXException , ParsingException { 205 206 Element root = new Element("root"); 207 root.appendChild(" Lots of random text\n\n\n "); 208 Document doc = new Document(root); 209 doc.insertChild(new Comment("some comment data"), 0); 210 root.insertChild(new Comment("some comment data"), 0); 211 doc.appendChild(new Comment("some comment data")); 212 convertAndCompare(doc); 213 214 } 215 216 217 public void testDefaultNamespace() 218 throws IOException , SAXException , ParsingException { 219 Document doc = new Document(new Element("a", "http://www.a.com/")); 220 convertAndCompare(doc); 221 } 222 223 224 public void testTextContent() 225 throws IOException , SAXException , ParsingException { 226 Element root = new Element("root"); 227 root.appendChild(" Lots of random text\n\n\n "); 228 Document doc = new Document(root); 229 convertAndCompare(doc); 230 } 231 232 233 public void testPrefixedNamespace() 234 throws IOException , SAXException , ParsingException { 235 Document doc = new Document(new Element("a:a", "http://www.a.com/")); 236 convertAndCompare(doc); 237 } 238 239 240 public void testAdditionalNamespace() 241 throws IOException , SAXException , ParsingException { 242 243 Element root = new Element("root"); 244 root.addNamespaceDeclaration("xsl", "http://www.w3.org/1999/XSL/Transform"); 245 Document doc = new Document(root); 246 convertAndCompare(doc); 247 248 } 249 250 251 public void testChildElementAddsNamespace() 252 throws IOException , SAXException , ParsingException { 253 254 Element root = new Element("root"); 255 Element child = new Element("pre:child", "http://www.example.org/"); 256 child.addAttribute(new Attribute("xlink:type", "http://www.w3.org/1999/xlink", "simple")); 257 root.appendChild(child); 258 Document doc = new Document(root); 259 convertAndCompare(doc); 260 261 } 262 263 264 public void testAttributesTypes() 265 throws IOException , SAXException , ParsingException { 266 267 Element root = new Element("root"); 268 root.addAttribute(new Attribute("CDATA", "CDATA", Attribute.Type.CDATA)); 269 root.addAttribute(new Attribute("ID", "ID", Attribute.Type.ID)); 270 root.addAttribute(new Attribute("IDREF", "IDREF", Attribute.Type.IDREF)); 271 root.addAttribute(new Attribute("IDRES", "IDREFS", Attribute.Type.IDREFS)); 272 root.addAttribute(new Attribute("NMTOKEN", "NMTOKEN", Attribute.Type.NMTOKEN)); 273 root.addAttribute(new Attribute("NMTOKENS", "NMTOKENS", Attribute.Type.NMTOKENS)); 274 root.addAttribute(new Attribute("UNDECLARED", "UNDECLARED", Attribute.Type.UNDECLARED)); 275 root.addAttribute(new Attribute("ENTITY", "ENTITY", Attribute.Type.ENTITY)); 276 root.addAttribute(new Attribute("ENTITIES", "ENTITIES", Attribute.Type.ENTITIES)); 277 root.addAttribute(new Attribute("NOTATION", "NOTATION", Attribute.Type.NOTATION)); 278 root.addAttribute(new Attribute("ENUMERATION", "ENUMERATION", Attribute.Type.ENUMERATION)); 279 Document doc = new Document(root); 280 convertAndCompare(doc); 281 282 } 283 284 285 public void testAttributes() 286 throws IOException , SAXException , ParsingException { 287 288 Element root = new Element("root"); 289 root.addAttribute(new Attribute("a", "test")); 290 root.addAttribute(new Attribute("xlink:type", 291 "http://www.w3.org/1999/xlink", "simple")); 292 Document doc = new Document(root); 293 convertAndCompare(doc); 294 295 } 296 297 298 public void testExternalDTDSubset() 299 throws IOException , SAXException , ParsingException { 300 301 File input = new File ("data"); 302 input = new File (input, "externalDTDtest.xml"); 303 Document doc = builder.build(input); 304 convertAndCompare(doc); 305 306 } 307 308 309 public void testBigDoc() 310 throws IOException , SAXException , ParsingException { 311 Document doc = builder.build("http://www.cafeconleche.org/"); 312 convertAndCompare(doc); 313 } 314 315 316 public void testNoPrefixMappingEventsForDefaultEmptyNamespace() 317 throws ParsingException, IOException , SAXException { 318 319 String data = "<root/>"; 320 Document doc = builder.build(data, null); 321 ContentHandler handler = new XMLPrefixTester2(); 322 SAXConverter converter = new SAXConverter(handler); 323 converter.convert(doc); 324 325 } 326 327 328 public void testNoPrefixMappingEventsForXMLPrefix() 329 throws ParsingException, IOException , SAXException { 330 331 String data = "<root xml:space='preserve'/>"; 332 Document doc = builder.build(data, null); 333 ContentHandler handler = new XMLPrefixTester(); 334 SAXConverter converter = new SAXConverter(handler); 335 converter.convert(doc); 336 337 } 338 339 340 public void testNoPrefixMappingEventsForXMLPrefixOnElement() 341 throws ParsingException, IOException , SAXException { 342 343 String data = "<xml:root/>"; 344 Document doc = builder.build(data, null); 345 ContentHandler handler = new XMLPrefixTester(); 346 SAXConverter converter = new SAXConverter(handler); 347 converter.convert(doc); 348 349 } 350 351 352 private static class XMLPrefixTester extends DefaultHandler { 353 354 public void startPrefixMapping(String prefix, String uri) { 355 if ("xml".equals(prefix)) { 356 fail("start mapped prefix xml"); 357 } 358 } 359 360 public void endPrefixMapping(String prefix) { 361 if ("xml".equals(prefix)) { 362 fail("end mapped prefix xml"); 363 } 364 } 365 366 } 367 368 369 private static class XMLPrefixTester2 extends DefaultHandler { 370 371 public void startPrefixMapping(String prefix, String uri) { 372 fail("start mapped prefix " + prefix); 373 } 374 375 public void endPrefixMapping(String prefix) { 376 fail("end mapped prefix " + prefix); 377 } 378 379 } 380 381 382 public void testNoRedundantPrefixMappingEvents() 383 throws ParsingException, IOException , SAXException { 384 385 String data = "<root xmlns='http://www.example.org'> <a> <b/> </a> </root>"; 386 Document doc = builder.build(data, null); 387 XMLPrefixMapCounter handler = new XMLPrefixMapCounter(); 388 SAXConverter converter = new SAXConverter(handler); 389 converter.convert(doc); 390 assertEquals(1, handler.getStarts()); 391 assertEquals(1, handler.getEnds()); 392 393 } 394 395 396 private static class XMLPrefixMapCounter extends DefaultHandler { 397 398 private int starts = 0; 399 private int ends = 0; 400 401 public void startPrefixMapping(String prefix, String uri) { 402 starts++; 403 } 404 405 public void endPrefixMapping(String prefix) { 406 ends++; 407 } 408 409 int getStarts() { 410 return starts; 411 } 412 413 int getEnds() { 414 return starts; 415 } 416 417 } 418 419 420 } 421 | Popular Tags |