KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > SAXConverterTest


1 /* Copyright 2003, 2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22
23 package nu.xom.tests;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.StringWriter JavaDoc;
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 JavaDoc;
40 import org.xml.sax.ContentHandler JavaDoc;
41 import org.xml.sax.Locator JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.ext.LexicalHandler JavaDoc;
44 import org.xml.sax.helpers.DefaultHandler JavaDoc;
45
46 /**
47  * <p>
48  * Basic tests for conversion from XOM trees
49  * to SAX ContentHandlers.
50  * </p>
51  *
52  * @author Elliotte Rusty Harold
53  * @version 1.0
54  *
55  */

56 public class SAXConverterTest extends XOMTestCase {
57
58     
59     public SAXConverterTest(String JavaDoc name) {
60         super(name);
61     }
62
63     
64     private DefaultHandler JavaDoc handler;
65     private SAXConverter converter;
66     private Builder builder = new Builder();
67     
68     
69     protected void setUp() {
70         handler = new DefaultHandler JavaDoc();
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 JavaDoc();
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 JavaDoc success) {
91             // success
92
}
93         
94     }
95
96     
97     public void testSetAndGetLexicalHandler() {
98         
99         LexicalHandler JavaDoc 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 JavaDoc, SAXException JavaDoc, ParsingException {
111         
112         StringWriter JavaDoc result = new StringWriter JavaDoc();
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 JavaDoc 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 JavaDoc, SAXException JavaDoc, ParsingException {
128         Document doc = new Document(new Element("a"));
129         convertAndCompare(doc);
130     }
131     
132     
133     public void testXMLBaseAttributesAreThrownAway()
134       throws SAXException JavaDoc {
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 JavaDoc {
151         
152         private Locator JavaDoc locator;
153         
154         public void setDocumentLocator(Locator JavaDoc locator) {
155             this.locator = locator;
156         }
157         
158         public void startElement(String JavaDoc localName, String JavaDoc qualifiedName,
159           String JavaDoc namespaceURI, Attributes JavaDoc attributes)
160           throws SAXException JavaDoc {
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 JavaDoc name = attributes.getLocalName(i);
171                 String JavaDoc 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, 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 JavaDoc, SAXException JavaDoc, ParsingException {
300
301         File JavaDoc input = new File JavaDoc("data");
302         input = new File JavaDoc(input, "externalDTDtest.xml");
303         Document doc = builder.build(input);
304         convertAndCompare(doc);
305         
306     }
307    
308     
309     public void testBigDoc()
310       throws IOException JavaDoc, SAXException JavaDoc, ParsingException {
311         Document doc = builder.build("http://www.cafeconleche.org/");
312         convertAndCompare(doc);
313     }
314
315     
316     public void testNoPrefixMappingEventsForDefaultEmptyNamespace()
317       throws ParsingException, IOException JavaDoc, SAXException JavaDoc {
318      
319         String JavaDoc data = "<root/>";
320         Document doc = builder.build(data, null);
321         ContentHandler JavaDoc 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 JavaDoc, SAXException JavaDoc {
330      
331         String JavaDoc data = "<root xml:space='preserve'/>";
332         Document doc = builder.build(data, null);
333         ContentHandler JavaDoc 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 JavaDoc, SAXException JavaDoc {
342      
343         String JavaDoc data = "<xml:root/>";
344         Document doc = builder.build(data, null);
345         ContentHandler JavaDoc handler = new XMLPrefixTester();
346         SAXConverter converter = new SAXConverter(handler);
347         converter.convert(doc);
348         
349     }
350     
351  
352     private static class XMLPrefixTester extends DefaultHandler JavaDoc {
353         
354         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
355             if ("xml".equals(prefix)) {
356                 fail("start mapped prefix xml");
357             }
358         }
359         
360         public void endPrefixMapping(String JavaDoc 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 JavaDoc {
370         
371         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
372             fail("start mapped prefix " + prefix);
373         }
374         
375         public void endPrefixMapping(String JavaDoc prefix) {
376             fail("end mapped prefix " + prefix);
377         }
378         
379     }
380     
381     
382     public void testNoRedundantPrefixMappingEvents()
383       throws ParsingException, IOException JavaDoc, SAXException JavaDoc {
384      
385         String JavaDoc 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 JavaDoc {
397         
398         private int starts = 0;
399         private int ends = 0;
400         
401         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
402             starts++;
403         }
404         
405         public void endPrefixMapping(String JavaDoc 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