1 18 19 package org.apache.batik.transcoder; 20 21 import java.io.IOException ; 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 ; 30 import org.w3c.dom.DOMImplementation ; 31 import org.w3c.dom.Document ; 32 33 52 public abstract class XMLAbstractTranscoder extends AbstractTranscoder { 53 54 57 protected XMLAbstractTranscoder() { 58 hints.put(KEY_XML_PARSER_VALIDATING, Boolean.FALSE); 59 } 60 61 71 public void transcode(TranscoderInput input, TranscoderOutput output) 72 throws TranscoderException { 73 74 Document document = null; 75 String uri = input.getURI(); 76 if (input.getDocument() != null) { 77 document = input.getDocument(); 78 } else { 79 String parserClassname = 80 (String )hints.get(KEY_XML_PARSER_CLASSNAME); 81 String namespaceURI = 82 (String )hints.get(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI); 83 String documentElement = 84 (String )hints.get(KEY_DOCUMENT_ELEMENT); 85 DOMImplementation domImpl = 86 (DOMImplementation )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 DocumentFactory f = createDocumentFactory(domImpl, parserClassname); 108 boolean b = 109 ((Boolean )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 ex) { 133 handler.fatalError(new TranscoderException(ex)); 134 } catch (IOException ex) { 135 ex.printStackTrace(); 136 handler.fatalError(new TranscoderException(ex)); 137 } 138 } 139 if (document != null) { 141 try { 142 transcode(document, uri, output); 143 } catch(TranscoderException ex) { 144 handler.fatalError(ex); 146 return; 147 } 148 } 149 } 150 151 160 protected DocumentFactory createDocumentFactory(DOMImplementation domImpl, 161 String parserClassname) { 162 return new SAXDocumentFactory(domImpl, parserClassname); 163 } 164 165 173 protected abstract void transcode(Document document, 174 String uri, 175 TranscoderOutput output) 176 throws TranscoderException; 177 178 182 202 public static final TranscodingHints.Key KEY_XML_PARSER_CLASSNAME 203 = new StringKey(); 204 205 225 public static final TranscodingHints.Key KEY_XML_PARSER_VALIDATING 226 = new BooleanKey(); 227 228 249 public static final TranscodingHints.Key KEY_DOCUMENT_ELEMENT 250 = new StringKey(); 251 252 274 public static final TranscodingHints.Key KEY_DOCUMENT_ELEMENT_NAMESPACE_URI 275 = new StringKey(); 276 277 298 public static final TranscodingHints.Key KEY_DOM_IMPLEMENTATION 299 = new DOMImplementationKey(); 300 } 301 302 303 | Popular Tags |