KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > TranscoderInputTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.batik.transcoder;
18
19
20 import java.io.File JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import org.w3c.dom.DOMImplementation JavaDoc;
28 import org.w3c.dom.Document JavaDoc;
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 JavaDoc;
40 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
41
42 /**
43  * This test validates that the various configurations of TranscoderInput
44  * are supported by the XMLAbstractTranscoder class.
45  *
46  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
47  * @version $Id: TranscoderInputTest.java,v 1.6 2005/04/01 02:28:16 deweese Exp $
48  */

49 public class TranscoderInputTest extends AbstractTest {
50     public TestReport runImpl() throws Exception JavaDoc {
51         String JavaDoc TEST_URI = (new File JavaDoc("samples/anne.svg")).toURL().toString();
52
53         TestTranscoder t = new TestTranscoder();
54
55         TranscoderOutput out = new TranscoderOutput(new StringWriter JavaDoc());
56
57         // XMLReader
58
{
59             XMLReader JavaDoc 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         // Input Stream
67
{
68             URL JavaDoc uri = new URL JavaDoc(TEST_URI);
69             InputStream JavaDoc 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         // Reader
78
{
79             URL JavaDoc uri = new URL JavaDoc(TEST_URI);
80             InputStream JavaDoc is = uri.openStream();
81             Reader JavaDoc r = new InputStreamReader JavaDoc(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         // Document
89
{
90             String JavaDoc parser = XMLResourceDescriptor.getXMLParserClassName();
91             SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
92             Document JavaDoc 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         // Generic Document
101
{
102             String JavaDoc parser = XMLResourceDescriptor.getXMLParserClassName();
103             DOMImplementation JavaDoc impl =
104                 GenericDOMImplementation.getDOMImplementation();
105             SAXDocumentFactory f = new SAXDocumentFactory(impl, parser);
106             Document JavaDoc 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         // URI only
115
{
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 JavaDoc document,
138                                  String JavaDoc uri,
139                                  TranscoderOutput output) {
140             passed = (document != null);
141         }
142     }
143 }
144
Popular Tags