1 16 package org.apache.cocoon.xml.dom; 17 18 import org.apache.avalon.framework.logger.LogEnabled; 19 import org.apache.avalon.framework.logger.Logger; 20 import org.apache.avalon.excalibur.pool.Recyclable; 21 22 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 23 import org.apache.batik.dom.svg.SVGDOMImplementation; 24 import org.apache.batik.dom.svg.SVGOMDocument; 25 26 import org.apache.cocoon.xml.XMLConsumer; 27 28 import org.w3c.dom.Document ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.Locator ; 31 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 35 42 public class SVGBuilder extends SAXSVGDocumentFactory implements XMLConsumer, LogEnabled, Recyclable { 43 protected Logger log; 44 45 protected Locator locator; 46 47 private static final String SAX_PARSER 48 = "org.apache.xerces.parsers.SAXParser"; 49 50 53 protected SVGBuilder() { 54 super(SAX_PARSER); 55 } 56 57 62 public void enableLogging(Logger logger) { 63 if (this.log == null) { 64 this.log = logger; 65 } 66 } 67 68 protected Logger getLogger() { 69 return this.log; 70 } 71 72 75 public Document getDocument() { 76 return super.document; 77 } 78 79 84 public void startDocument() throws SAXException { 85 try { 86 String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI; 88 super.document = implementation.createDocument(namespaceURI, "svg", null); 89 super.startDocument(); 90 namespaces.put("svg", SVGDOMImplementation.SVG_NAMESPACE_URI); 93 } catch (SAXException se) { 94 throw se; 95 } catch (Exception ex){ 96 if (getLogger().isDebugEnabled()) { 97 getLogger().debug("Got exception in startDocument, rethrowing", ex); 98 } 99 throw new SAXException ("Exception in startDocument", ex); 100 } 101 } 102 103 public void setDocumentLocator(Locator locator) { 104 this.locator = locator; 105 super.setDocumentLocator(locator); 106 } 107 108 113 public void endDocument() throws SAXException { 114 try { 115 super.endDocument(); 116 117 URL baseURL = null; 119 try { 120 if (this.locator != null) { 121 baseURL = new URL (this.locator.getSystemId()); 122 } else { 123 baseURL = new URL ("http://localhost/"); 124 getLogger().warn("setDocumentLocator was not called, will use http://localhost/ as base URI"); 125 } 126 ((SVGOMDocument)super.document).setURLObject(baseURL); 127 } catch (MalformedURLException e) { 128 getLogger().warn("Unable to set document base URI to " + baseURL + ", will default to http://localhost/", e); 129 ((SVGOMDocument)super.document).setURLObject(new URL ("http://localhost/")); 130 } 131 notify(super.document); 132 } catch (SAXException se) { 133 throw se; 134 } catch (Exception ex){ 135 if (getLogger().isDebugEnabled()) { 136 getLogger().debug("Got exception in endDocument, rethrowing", ex); 137 } 138 throw new SAXException ("Exception in endDocument", ex); 139 } 140 } 141 142 145 protected void notify(Document doc) throws SAXException { 146 } 147 148 public void recycle() { 149 locator = null; 150 } 151 } 152 | Popular Tags |