KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 package org.apache.batik.transcoder;
20
21 import java.io.IOException JavaDoc;
22
23 import org.apache.batik.dom.util.DocumentFactory;
24 import org.apache.batik.dom.util.SAXDocumentFactory;
25 import org.apache.batik.transcoder.keys.BooleanKey;
26 import org.apache.batik.transcoder.keys.DOMImplementationKey;
27 import org.apache.batik.transcoder.keys.StringKey;
28 import org.apache.batik.util.XMLResourceDescriptor;
29 import org.w3c.dom.DOMException JavaDoc;
30 import org.w3c.dom.DOMImplementation JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32
33 /**
34  * This class may be the base class of all transcoders which take an
35  * XML document as input and which need to build a DOM tree. In order
36  * to take advantage of this class, you have to specify the following
37  * transcoding hints:
38  *
39  * <ul>
40  * <li><tt>KEY_DOM_IMPLEMENTATION</tt>: the DOM Implementation to use
41  *
42  * <li><tt>KEY_DOCUMENT_ELEMENT_NAMESPACE_URI</tt>: the namespace URI of the
43  * document to create
44  *
45  * <li><tt>KEY_DOCUMENT_ELEMENT</tt>: the qualified name of the document type
46  * to create
47  * </ul>
48  *
49  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
50  * @version $Id: XMLAbstractTranscoder.java,v 1.14 2004/10/30 18:38:06 deweese Exp $
51  */

52 public abstract class XMLAbstractTranscoder extends AbstractTranscoder {
53
54     /**
55      * Constructs a new <tt>XMLAbstractTranscoder</tt>.
56      */

57     protected XMLAbstractTranscoder() {
58     hints.put(KEY_XML_PARSER_VALIDATING, Boolean.FALSE);
59     }
60
61     /**
62      * Transcodes the specified XML input in the specified output. All
63      * <tt>TranscoderException</tt> exceptions not catched previously
64      * are tagged as fatal errors (ie. call the <tt>fatalError</tt>
65      * method of the <tt>ErrorHandler</tt>).
66      *
67      * @param input the XML input to transcode
68      * @param output the ouput where to transcode
69      * @exception TranscoderException if an error occured while transcoding
70      */

71     public void transcode(TranscoderInput input, TranscoderOutput output)
72             throws TranscoderException {
73
74         Document JavaDoc document = null;
75         String JavaDoc uri = input.getURI();
76         if (input.getDocument() != null) {
77             document = input.getDocument();
78         } else {
79             String JavaDoc parserClassname =
80                 (String JavaDoc)hints.get(KEY_XML_PARSER_CLASSNAME);
81             String JavaDoc namespaceURI =
82                 (String JavaDoc)hints.get(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI);
83             String JavaDoc documentElement =
84                 (String JavaDoc)hints.get(KEY_DOCUMENT_ELEMENT);
85             DOMImplementation JavaDoc domImpl =
86                 (DOMImplementation JavaDoc)hints.get(KEY_DOM_IMPLEMENTATION);
87         
88             if (parserClassname == null) {
89                 parserClassname = XMLResourceDescriptor.getXMLParserClassName();
90             }
91             if (domImpl == null) {
92                 handler.fatalError(new TranscoderException(
93                     "Unspecified transcoding hints: KEY_DOM_IMPLEMENTATION"));
94                 return;
95             }
96             if (namespaceURI == null) {
97                 handler.fatalError(new TranscoderException(
98                 "Unspecified transcoding hints: KEY_DOCUMENT_ELEMENT_NAMESPACE_URI"));
99                 return;
100             }
101             if (documentElement == null) {
102                 handler.fatalError(new TranscoderException(
103                     "Unspecified transcoding hints: KEY_DOCUMENT_ELEMENT"));
104                 return;
105             }
106             // parse the XML document
107
DocumentFactory f = createDocumentFactory(domImpl, parserClassname);
108         boolean b =
109         ((Boolean JavaDoc)hints.get(KEY_XML_PARSER_VALIDATING)).booleanValue();
110         f.setValidating(b);
111             try {
112                 if (input.getInputStream() != null) {
113                     document = f.createDocument(namespaceURI,
114                                                 documentElement,
115                                                 input.getURI(),
116                                                 input.getInputStream());
117                 } else if (input.getReader() != null) {
118                     document = f.createDocument(namespaceURI,
119                                                 documentElement,
120                                                 input.getURI(),
121                                                 input.getReader());
122                 } else if (input.getXMLReader() != null) {
123                     document = f.createDocument(namespaceURI,
124                                                 documentElement,
125                                                 input.getURI(),
126                                                 input.getXMLReader());
127                 } else if (uri != null) {
128                     document = f.createDocument(namespaceURI,
129                                                 documentElement,
130                                                 uri);
131                 }
132             } catch (DOMException JavaDoc ex) {
133                 handler.fatalError(new TranscoderException(ex));
134             } catch (IOException JavaDoc ex) {
135                 ex.printStackTrace();
136                 handler.fatalError(new TranscoderException(ex));
137             }
138         }
139         // call the dedicated transcode method
140
if (document != null) {
141             try {
142                 transcode(document, uri, output);
143             } catch(TranscoderException ex) {
144                 // at this time, all TranscoderExceptions are fatal errors
145
handler.fatalError(ex);
146                 return;
147             }
148         }
149     }
150
151     /**
152      * Creates the <tt>DocumentFactory</tt> used to create the DOM
153      * tree. Override this method if you have to use another
154      * implementation of the <tt>DocumentFactory</tt> (ie. for SVG,
155      * you have to use the <tt>SAXSVGDocumentFactory</tt>).
156      *
157      * @param domImpl the DOM Implementation to use
158      * @param parserClassname the XML parser classname
159      */

160     protected DocumentFactory createDocumentFactory(DOMImplementation JavaDoc domImpl,
161                                                     String JavaDoc parserClassname) {
162     return new SAXDocumentFactory(domImpl, parserClassname);
163     }
164
165     /**
166      * Transcodes the specified Document in the specified output.
167      *
168      * @param document the document to transcode
169      * @param uri the uri of the document or null if any
170      * @param output the ouput where to transcode
171      * @exception TranscoderException if an error occured while transcoding
172      */

173     protected abstract void transcode(Document JavaDoc document,
174                                       String JavaDoc uri,
175                                       TranscoderOutput output)
176             throws TranscoderException;
177
178     // --------------------------------------------------------------------
179
// Keys definition
180
// --------------------------------------------------------------------
181

182     /**
183      * XML parser classname key.
184      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
185      * <TR>
186      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
187      * <TD VALIGN="TOP">KEY_XML_PARSER_CLASSNAME</TD></TR>
188      * <TR>
189      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
190      * <TD VALIGN="TOP">String</TD></TR>
191      * <TR>
192      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
193      * <TD VALIGN="TOP">null</TD></TR>
194      * <TR>
195      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
196      * <TD VALIGN="TOP">Yes</TD></TR>
197      * <TR>
198      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
199      * <TD VALIGN="TOP">Specify the XML parser classname to use.</TD></TR>
200      * </TABLE>
201      */

202     public static final TranscodingHints.Key KEY_XML_PARSER_CLASSNAME
203         = new StringKey();
204
205     /**
206      * The validation mode of the XML parser.
207      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
208      * <TR>
209      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
210      * <TD VALIGN="TOP">KEY_XML_PARSER_VALIDATING</TD></TR>
211      * <TR>
212      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
213      * <TD VALIGN="TOP">Boolean</TD></TR>
214      * <TR>
215      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
216      * <TD VALIGN="TOP">false</TD></TR>
217      * <TR>
218      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
219      * <TD VALIGN="TOP">No</TD></TR>
220      * <TR>
221      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
222      * <TD VALIGN="TOP">Specify the validation mode of the XML parser.</TD></TR>
223      * </TABLE>
224      */

225     public static final TranscodingHints.Key KEY_XML_PARSER_VALIDATING
226         = new BooleanKey();
227
228     /**
229      * Document element key.
230      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
231      * <TR>
232      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
233      * <TD VALIGN="TOP">KEY_DOCUMENT_ELEMENT</TD></TR>
234      * <TR>
235      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
236      * <TD VALIGN="TOP">String</TD></TR>
237      * <TR>
238      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
239      * <TD VALIGN="TOP">null</TD></TR>
240      * <TR>
241      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
242      * <TD VALIGN="TOP">Yes</TD></TR>
243      * <TR>
244      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
245      * <TD VALIGN="TOP">Specify the qualified name of the document
246      * type to be created.</TD></TR>
247      * </TABLE>
248      */

249     public static final TranscodingHints.Key KEY_DOCUMENT_ELEMENT
250         = new StringKey();
251
252     /**
253      * Document element namespace URI key.
254      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
255      * <TR>
256      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
257      * <TD VALIGN="TOP">KEY_DOCUMENT_ELEMENT_NAMESPACE_URI</TD></TR>
258      * <TR>
259      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
260      * <TD VALIGN="TOP">String</TD></TR>
261      * <TR>
262      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
263      * <TD VALIGN="TOP">null</TD></TR>
264      * <TR>
265      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
266      * <TD VALIGN="TOP">Yes</TD></TR>
267      * <TR>
268      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
269      *
270      * <TD VALIGN="TOP">Specify the namespace URI of the document
271      * element.</TD></TR>
272      * </TABLE>
273      */

274     public static final TranscodingHints.Key KEY_DOCUMENT_ELEMENT_NAMESPACE_URI
275         = new StringKey();
276
277     /**
278      * DOM Implementation key.
279      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
280      * <TR>
281      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
282      * <TD VALIGN="TOP">KEY_DOM_IMPLEMENTATION</TD></TR>
283      * <TR>
284      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
285      * <TD VALIGN="TOP">String</TD></TR>
286      * <TR>
287      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
288      * <TD VALIGN="TOP">null</TD></TR>
289      * <TR>
290      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
291      * <TD VALIGN="TOP">Yes</TD></TR>
292      * <TR>
293      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
294      *
295      * <TD VALIGN="TOP">Specify the DOM Implementation to use.</TD></TR>
296      * </TABLE>
297      */

298     public static final TranscodingHints.Key KEY_DOM_IMPLEMENTATION
299         = new DOMImplementationKey();
300 }
301
302
303
Popular Tags