1 16 17 package org.apache.batik.transcoder; 18 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.Reader ; 24 import java.io.StringWriter ; 25 import java.net.URL ; 26 27 import org.w3c.dom.DOMImplementation ; 28 import org.w3c.dom.Document ; 29 30 import org.apache.batik.dom.GenericDOMImplementation; 31 import org.apache.batik.dom.svg.SVGDOMImplementation; 32 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 33 import org.apache.batik.dom.util.SAXDocumentFactory; 34 import org.apache.batik.test.AbstractTest; 35 import org.apache.batik.test.TestReport; 36 import org.apache.batik.util.SVGConstants; 37 import org.apache.batik.util.XMLResourceDescriptor; 38 39 import org.xml.sax.XMLReader ; 40 import org.xml.sax.helpers.XMLReaderFactory ; 41 42 49 public class TranscoderInputTest extends AbstractTest { 50 public TestReport runImpl() throws Exception { 51 String TEST_URI = (new File ("samples/anne.svg")).toURL().toString(); 52 53 TestTranscoder t = new TestTranscoder(); 54 55 TranscoderOutput out = new TranscoderOutput(new StringWriter ()); 56 57 { 59 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 60 TranscoderInput ti = new TranscoderInput(xmlReader); 61 ti.setURI(TEST_URI); 62 t.transcode(ti, out); 63 assertTrue(t.passed); 64 } 65 66 { 68 URL uri = new URL (TEST_URI); 69 InputStream is = uri.openStream(); 70 TranscoderInput ti = new TranscoderInput(is); 71 ti.setURI(TEST_URI); 72 t = new TestTranscoder(); 73 t.transcode(ti, out); 74 assertTrue(t.passed); 75 } 76 77 { 79 URL uri = new URL (TEST_URI); 80 InputStream is = uri.openStream(); 81 Reader r = new InputStreamReader (is); 82 TranscoderInput ti = new TranscoderInput(r); 83 ti.setURI(TEST_URI); 84 t = new TestTranscoder(); 85 t.transcode(ti, out); 86 assertTrue(t.passed); 87 } 88 { 90 String parser = XMLResourceDescriptor.getXMLParserClassName(); 91 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); 92 Document doc = f.createDocument(TEST_URI); 93 TranscoderInput ti = new TranscoderInput(doc); 94 ti.setURI(TEST_URI); 95 t = new TestTranscoder(); 96 t.transcode(ti, out); 97 assertTrue(t.passed); 98 } 99 100 { 102 String parser = XMLResourceDescriptor.getXMLParserClassName(); 103 DOMImplementation impl = 104 GenericDOMImplementation.getDOMImplementation(); 105 SAXDocumentFactory f = new SAXDocumentFactory(impl, parser); 106 Document doc = f.createDocument(TEST_URI); 107 TranscoderInput ti = new TranscoderInput(doc); 108 ti.setURI(TEST_URI); 109 t = new TestTranscoder(); 110 t.transcode(ti, out); 111 assertTrue(t.passed); 112 } 113 114 { 116 TranscoderInput ti = new TranscoderInput(TEST_URI); 117 t = new TestTranscoder(); 118 t.transcode(ti, out); 119 assertTrue(t.passed); 120 } 121 122 return reportSuccess(); 123 } 124 125 static class TestTranscoder extends XMLAbstractTranscoder { 126 boolean passed = false; 127 128 public TestTranscoder() { 129 addTranscodingHint(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, 130 SVGConstants.SVG_NAMESPACE_URI); 131 addTranscodingHint(KEY_DOCUMENT_ELEMENT, 132 SVGConstants.SVG_SVG_TAG); 133 addTranscodingHint(KEY_DOM_IMPLEMENTATION, 134 SVGDOMImplementation.getDOMImplementation()); 135 } 136 137 protected void transcode(Document document, 138 String uri, 139 TranscoderOutput output) { 140 passed = (document != null); 141 } 142 } 143 } 144 | Popular Tags |