001: /* Copyright 2003-2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.io.StringWriter;
027:
028: import nu.xom.Attribute;
029: import nu.xom.Builder;
030: import nu.xom.Comment;
031: import nu.xom.DocType;
032: import nu.xom.Document;
033: import nu.xom.Element;
034: import nu.xom.ParsingException;
035: import nu.xom.ProcessingInstruction;
036: import nu.xom.converters.SAXConverter;
037:
038: import org.xml.sax.Attributes;
039: import org.xml.sax.ContentHandler;
040: import org.xml.sax.Locator;
041: import org.xml.sax.SAXException;
042: import org.xml.sax.ext.LexicalHandler;
043: import org.xml.sax.helpers.DefaultHandler;
044:
045: /**
046: * <p>
047: * Basic tests for conversion from XOM trees
048: * to SAX ContentHandlers.
049: * </p>
050: *
051: * @author Elliotte Rusty Harold
052: * @version 1.1b4
053: *
054: */
055: public class SAXConverterTest extends XOMTestCase {
056:
057: public SAXConverterTest(String name) {
058: super (name);
059: }
060:
061: private DefaultHandler handler;
062: private SAXConverter converter;
063: private Builder builder = new Builder();
064:
065: protected void setUp() {
066: handler = new DefaultHandler();
067: converter = new SAXConverter(handler);
068: }
069:
070: public void testGetContentHandler() {
071: assertEquals(handler, converter.getContentHandler());
072: }
073:
074: public void testSetContentHandler() {
075:
076: handler = new DefaultHandler();
077: converter.setContentHandler(handler);
078: assertEquals(handler, converter.getContentHandler());
079:
080: try {
081: converter.setContentHandler(null);
082: fail("Allowed null ContentHandler");
083: } catch (NullPointerException success) {
084: // success
085: }
086:
087: }
088:
089: public void testSetAndGetLexicalHandler() {
090:
091: LexicalHandler handler = new XMLWriter();
092: converter.setLexicalHandler(handler);
093: assertEquals(handler, converter.getLexicalHandler());
094:
095: converter.setLexicalHandler(null);
096: assertNull(converter.getLexicalHandler());
097:
098: }
099:
100: private void convertAndCompare(Document doc) throws IOException,
101: SAXException, ParsingException {
102:
103: StringWriter result = new StringWriter();
104: XMLWriter handler = new XMLWriter(result);
105: converter.setContentHandler(handler);
106: converter.setLexicalHandler(handler);
107: converter.convert(doc);
108: result.flush();
109: result.close();
110: String convertedDoc = result.toString();
111: Document rebuiltDoc = builder.build(convertedDoc, doc
112: .getBaseURI());
113: assertEquals(doc, rebuiltDoc);
114:
115: }
116:
117: public void testSimplestDoc() throws IOException, SAXException,
118: ParsingException {
119: Document doc = new Document(new Element("a"));
120: convertAndCompare(doc);
121: }
122:
123: public void testXMLBaseAttributesAreThrownAway()
124: throws SAXException {
125:
126: Element root = new Element("root");
127: Element child = new Element("child");
128: Attribute base = new Attribute("xml:base",
129: "http://www.w3.org/XML/1998/namespace", "data");
130: child.addAttribute(base);
131: root.appendChild(child);
132: Document doc = new Document(root);
133: doc.setBaseURI("http://www.example.com/");
134: converter.setContentHandler(new BaseChecker());
135: converter.convert(doc);
136:
137: }
138:
139: private static class BaseChecker extends DefaultHandler {
140:
141: private Locator locator;
142:
143: public void setDocumentLocator(Locator locator) {
144: this .locator = locator;
145: }
146:
147: public void startElement(String localName,
148: String qualifiedName, String namespaceURI,
149: Attributes attributes) throws SAXException {
150:
151: if (localName.equals("root")) {
152: assertEquals("http://www.example.com/", locator
153: .getSystemId());
154: } else if (localName.equals("child")) {
155: assertEquals("http://www.example.com/data", locator
156: .getSystemId());
157: }
158:
159: for (int i = 0; i < attributes.getLength(); i++) {
160: String name = attributes.getLocalName(i);
161: String uri = attributes.getURI(i);
162: if ("base".equals(name)
163: && "http://www.w3.org/XML/1998/namespace"
164: .equals(uri)) {
165: fail("Passed xml:base attribute into SAXConverter");
166: }
167: }
168:
169: }
170:
171: }
172:
173: public void testDocType() throws IOException, SAXException,
174: ParsingException {
175: Document doc = new Document(new Element("a"));
176: doc.setDocType(new DocType("root"));
177: convertAndCompare(doc);
178: }
179:
180: public void testProcessingInstruction() throws IOException,
181: SAXException, ParsingException {
182:
183: Document doc = new Document(new Element("a"));
184: doc.insertChild(new ProcessingInstruction("xml-stylesheet",
185: "type=\"application/xml\" href=\"stylesheet.xsl\""), 0);
186: convertAndCompare(doc);
187:
188: }
189:
190: public void testComment() throws IOException, SAXException,
191: ParsingException {
192:
193: Element root = new Element("root");
194: root.appendChild(" Lots of random text\n\n\n ");
195: Document doc = new Document(root);
196: doc.insertChild(new Comment("some comment data"), 0);
197: root.insertChild(new Comment("some comment data"), 0);
198: doc.appendChild(new Comment("some comment data"));
199: convertAndCompare(doc);
200:
201: }
202:
203: public void testDefaultNamespace() throws IOException,
204: SAXException, ParsingException {
205: Document doc = new Document(new Element("a",
206: "http://www.a.com/"));
207: convertAndCompare(doc);
208: }
209:
210: public void testTextContent() throws IOException, SAXException,
211: ParsingException {
212: Element root = new Element("root");
213: root.appendChild(" Lots of random text\n\n\n ");
214: Document doc = new Document(root);
215: convertAndCompare(doc);
216: }
217:
218: public void testPrefixedNamespace() throws IOException,
219: SAXException, ParsingException {
220: Document doc = new Document(new Element("a:a",
221: "http://www.a.com/"));
222: convertAndCompare(doc);
223: }
224:
225: public void testAdditionalNamespace() throws IOException,
226: SAXException, ParsingException {
227:
228: Element root = new Element("root");
229: root.addNamespaceDeclaration("xsl",
230: "http://www.w3.org/1999/XSL/Transform");
231: Document doc = new Document(root);
232: convertAndCompare(doc);
233:
234: }
235:
236: public void testPrefixAndAdditionalNamespace() throws IOException,
237: SAXException, ParsingException {
238:
239: Element root = new Element("xsl:root",
240: "http://www.w3.org/1999/XSL/Transform");
241: root.addNamespaceDeclaration("xsl",
242: "http://www.w3.org/1999/XSL/Transform");
243: Document doc = new Document(root);
244: convertAndCompare(doc);
245:
246: }
247:
248: public void testPrefixAndAdditionalNamespaceFromParser()
249: throws IOException, SAXException, ParsingException {
250: Document doc = builder
251: .build(
252: "<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'/>",
253: null);
254: convertAndCompare(doc);
255: }
256:
257: public void testChildElementAddsNamespace() throws IOException,
258: SAXException, ParsingException {
259:
260: Element root = new Element("root");
261: Element child = new Element("pre:child",
262: "http://www.example.org/");
263: child.addAttribute(new Attribute("xlink:type",
264: "http://www.w3.org/1999/xlink", "simple"));
265: root.appendChild(child);
266: Document doc = new Document(root);
267: convertAndCompare(doc);
268:
269: }
270:
271: public void testAttributesTypes() throws IOException, SAXException,
272: ParsingException {
273:
274: Element root = new Element("root");
275: root.addAttribute(new Attribute("CDATA", "CDATA",
276: Attribute.Type.CDATA));
277: root.addAttribute(new Attribute("ID", "ID", Attribute.Type.ID));
278: root.addAttribute(new Attribute("IDREF", "IDREF",
279: Attribute.Type.IDREF));
280: root.addAttribute(new Attribute("IDRES", "IDREFS",
281: Attribute.Type.IDREFS));
282: root.addAttribute(new Attribute("NMTOKEN", "NMTOKEN",
283: Attribute.Type.NMTOKEN));
284: root.addAttribute(new Attribute("NMTOKENS", "NMTOKENS",
285: Attribute.Type.NMTOKENS));
286: root.addAttribute(new Attribute("UNDECLARED", "UNDECLARED",
287: Attribute.Type.UNDECLARED));
288: root.addAttribute(new Attribute("ENTITY", "ENTITY",
289: Attribute.Type.ENTITY));
290: root.addAttribute(new Attribute("ENTITIES", "ENTITIES",
291: Attribute.Type.ENTITIES));
292: root.addAttribute(new Attribute("NOTATION", "NOTATION",
293: Attribute.Type.NOTATION));
294: root.addAttribute(new Attribute("ENUMERATION", "ENUMERATION",
295: Attribute.Type.ENUMERATION));
296: Document doc = new Document(root);
297: convertAndCompare(doc);
298:
299: }
300:
301: public void testAttributes() throws IOException, SAXException,
302: ParsingException {
303:
304: Element root = new Element("root");
305: root.addAttribute(new Attribute("a", "test"));
306: root.addAttribute(new Attribute("xlink:type",
307: "http://www.w3.org/1999/xlink", "simple"));
308: Document doc = new Document(root);
309: convertAndCompare(doc);
310:
311: }
312:
313: public void testExternalDTDSubset() throws IOException,
314: SAXException, ParsingException {
315:
316: File input = new File("data");
317: input = new File(input, "externalDTDtest.xml");
318: Document doc = builder.build(input);
319: convertAndCompare(doc);
320:
321: }
322:
323: public void testBigDoc() throws IOException, SAXException,
324: ParsingException {
325: Document doc = builder.build("http://www.cafeconleche.org/");
326: convertAndCompare(doc);
327: }
328:
329: public void testNoPrefixMappingEventsForDefaultEmptyNamespace()
330: throws ParsingException, IOException, SAXException {
331:
332: String data = "<root/>";
333: Document doc = builder.build(data, null);
334: ContentHandler handler = new XMLPrefixTester2();
335: SAXConverter converter = new SAXConverter(handler);
336: converter.convert(doc);
337:
338: }
339:
340: public void testNoPrefixMappingEventsForXMLPrefix()
341: throws ParsingException, IOException, SAXException {
342:
343: String data = "<root xml:space='preserve'/>";
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: public void testNoPrefixMappingEventsForXMLPrefixOnElement()
352: throws ParsingException, IOException, SAXException {
353:
354: String data = "<xml:root/>";
355: Document doc = builder.build(data, null);
356: ContentHandler handler = new XMLPrefixTester();
357: SAXConverter converter = new SAXConverter(handler);
358: converter.convert(doc);
359:
360: }
361:
362: private static class XMLPrefixTester extends DefaultHandler {
363:
364: public void startPrefixMapping(String prefix, String uri) {
365: if ("xml".equals(prefix)) {
366: fail("start mapped prefix xml");
367: }
368: }
369:
370: public void endPrefixMapping(String prefix) {
371: if ("xml".equals(prefix)) {
372: fail("end mapped prefix xml");
373: }
374: }
375:
376: }
377:
378: private static class XMLPrefixTester2 extends DefaultHandler {
379:
380: public void startPrefixMapping(String prefix, String uri) {
381: fail("start mapped prefix " + prefix);
382: }
383:
384: public void endPrefixMapping(String prefix) {
385: fail("end mapped prefix " + prefix);
386: }
387:
388: }
389:
390: public void testNoRedundantPrefixMappingEventsForDefaultNamespace()
391: throws ParsingException, IOException, SAXException {
392:
393: String data = "<root xmlns='http://www.example.org'> <a> <b/> </a> </root>";
394: Document doc = builder.build(data, null);
395: XMLPrefixMapCounter handler = new XMLPrefixMapCounter();
396: SAXConverter converter = new SAXConverter(handler);
397: converter.convert(doc);
398: assertEquals(1, handler.getStarts());
399: assertEquals(1, handler.getEnds());
400:
401: }
402:
403: public void testNoRedundantPrefixMappingEventsForPrefixedNamespace()
404: throws ParsingException, IOException, SAXException {
405:
406: String data = "<a:root xmlns:a='http://www.example.org' />";
407: Document doc = builder.build(data, null);
408: XMLPrefixMapCounter handler = new XMLPrefixMapCounter();
409: SAXConverter converter = new SAXConverter(handler);
410: converter.convert(doc);
411: assertEquals(1, handler.getStarts());
412: assertEquals(1, handler.getEnds());
413:
414: }
415:
416: public void testChildNamespace() throws ParsingException,
417: IOException, SAXException {
418:
419: String data = "<a><pre:b xmlns:pre='http://www.example.com'/></a>";
420: Document doc = builder.build(data, null);
421: XMLPrefixMapCounter handler = new XMLPrefixMapCounter();
422: SAXConverter converter = new SAXConverter(handler);
423: converter.convert(doc);
424: assertEquals(1, handler.getStarts());
425: assertEquals(1, handler.getEnds());
426:
427: }
428:
429: private static class XMLPrefixMapCounter extends DefaultHandler {
430:
431: private int starts = 0;
432: private int ends = 0;
433:
434: public void startPrefixMapping(String prefix, String uri) {
435: starts++;
436: }
437:
438: public void endPrefixMapping(String prefix) {
439: ends++;
440: }
441:
442: int getStarts() {
443: return starts;
444: }
445:
446: int getEnds() {
447: return ends;
448: }
449:
450: }
451:
452: }
|