1.1 --- a/libxml2dom/svg.py Fri Apr 06 18:37:16 2007 +0000
1.2 +++ b/libxml2dom/svg.py Fri Apr 06 18:38:06 2007 +0000
1.3 @@ -28,6 +28,7 @@
1.4 from libxml2dom.macrolib import \
1.5 createDocument as Node_createDocument
1.6 import xml.dom
1.7 +import urllib
1.8 import math
1.9 import re
1.10
1.11 @@ -80,6 +81,9 @@
1.12 else:
1.13 return libxml2dom.Implementation.get_node(self, _node, context_node)
1.14
1.15 + def get_global(self, doc):
1.16 + return SVGGlobal(doc)
1.17 +
1.18 # Convenience functions.
1.19
1.20 def createSVGDocument(self):
1.21 @@ -141,7 +145,23 @@
1.22 nextElementSibling = property(_nextElementSibling)
1.23 previousElementSibling = property(_previousElementSibling)
1.24
1.25 -class SVGGlobal: # Global, EventListenerInitializer2
1.26 +class EventListenerInitializer2:
1.27 +
1.28 + "An event listener initialisation interface."
1.29 +
1.30 + def initializeEventListeners(self, scriptElement):
1.31 + pass
1.32 +
1.33 + def createEventListener(self, handlerElement):
1.34 + pass
1.35 +
1.36 +class Global:
1.37 +
1.38 + "An empty global interface."
1.39 +
1.40 + pass
1.41 +
1.42 +class SVGGlobal(Global, EventListenerInitializer2):
1.43
1.44 "An SVG global."
1.45
1.46 @@ -151,6 +171,10 @@
1.47
1.48 self.document = document
1.49
1.50 + # Listener management.
1.51 +
1.52 + self.listeners = {}
1.53 +
1.54 def createConnection(self):
1.55 raise NotImplementedError, "createConnection"
1.56
1.57 @@ -161,10 +185,16 @@
1.58 raise NotImplementedError, "gotoLocation"
1.59
1.60 def binaryToString(self, octets, encoding):
1.61 - raise NotImplementedError, "binaryToString"
1.62 + try:
1.63 + return unicode(octets, encoding)
1.64 + except UnicodeDecodeError, exc:
1.65 + raise GlobalException(GlobalException.ENCODING_ERR)
1.66
1.67 def stringToBinary(self, data, encoding):
1.68 - raise NotImplementedError, "stringToBinary"
1.69 + try:
1.70 + return data.encode(encoding)
1.71 + except UnicodeEncodeError, exc:
1.72 + raise GlobalException(GlobalException.ENCODING_ERR)
1.73
1.74 def getURL(self, iri, callback):
1.75
1.76 @@ -183,8 +213,27 @@
1.77 finally:
1.78 f.close()
1.79
1.80 - def postURL(self, iri, data, callback, type, encoding):
1.81 - raise NotImplementedError, "postURL"
1.82 + def postURL(self, iri, data, callback, type=None, encoding=None):
1.83 +
1.84 + # NOTE: Not asynchronous.
1.85 + # NOTE: The urlopen function may not support IRIs.
1.86 + # No exceptions are supposed to be raised, which is a bit nasty.
1.87 +
1.88 + opener = urllib.URLopener()
1.89 + opener.addheader("Content-Type", type or "text/plain")
1.90 + if encoding:
1.91 + opener.addheader("Content-Encoding", encoding)
1.92 + f = opener.open(iri, data)
1.93 + try:
1.94 + try:
1.95 + content = f.read()
1.96 + contentType = f.headers["Content-Type"]
1.97 + callback.operationComplete(AsyncURLStatus(1, contentType, content))
1.98 + except:
1.99 + callback.operationComplete(AsyncURLStatus(0, None, None))
1.100 + finally:
1.101 + f.close()
1.102 + opener.close()
1.103
1.104 def parseXML(self, data, contextDoc):
1.105 doc = parseString(data)
1.106 @@ -641,7 +690,7 @@
1.107 namespaces["svg"] = SVG_NAMESPACE
1.108 return libxml2dom.Node.xpath(self, expr, variables, namespaces)
1.109
1.110 -class SVGDocument(libxml2dom._Document, SVGNode, DocumentEvent, EventTarget):
1.111 +class SVGDocument(libxml2dom._Document, SVGNode, EventTarget, DocumentEvent): # NOTE: The latter is from DOM Level 3 Events.
1.112
1.113 "An SVG-specific document node."
1.114
1.115 @@ -653,12 +702,15 @@
1.116 """
1.117
1.118 libxml2dom._Document.__init__(self, node, impl)
1.119 - self.global_ = SVGGlobal(self) # parent
1.120 + self.global_ = self.impl.get_global(self) # parent
1.121
1.122 class SVGElement(SVGNode, EventTarget, TraitAccess, ElementTraversal): # NOTE: SVGNode instead of Element.
1.123
1.124 "An SVG-specific element."
1.125
1.126 + def __init__(self, *args, **kw):
1.127 + SVGNode.__init__(self, *args, **kw)
1.128 +
1.129 def _id(self):
1.130 return self.getAttribute("id")
1.131
1.132 @@ -763,6 +815,27 @@
1.133 currentTranslate = property(_currentTranslate)
1.134 viewport = property(_viewport)
1.135
1.136 +# Event handler initialisation.
1.137 +
1.138 +def initialiseEvents(doc):
1.139 +
1.140 + """
1.141 + See: http://www.w3.org/TR/SVGMobile12/svgudom.html#svg__EventListenerInitializer2
1.142 + """
1.143 +
1.144 + for script in doc.xpath("//svg:script"):
1.145 + doc.global_.initializeEventListeners(script)
1.146 + for handler in doc.xpath("//svg:handler"):
1.147 + listener = doc.global_.createEventListener(handler)
1.148 +
1.149 + # NOTE: May need to have the event type understood and correctly parameterised.
1.150 +
1.151 + handler.parentNode.addEventListener(
1.152 + handler.getAttributeNS(libxml2dom.events.XML_EVENTS_NAMESPACE, "event"),
1.153 + listener,
1.154 + 0
1.155 + )
1.156 +
1.157 # Utility functions.
1.158
1.159 createDocument = libxml2dom.createDocument
1.160 @@ -771,17 +844,25 @@
1.161 def createSVGDocument():
1.162 return default_impl.createSVGDocument()
1.163
1.164 -def parse(stream_or_string, html=0, htmlencoding=None):
1.165 - return libxml2dom.parse(stream_or_string, html, htmlencoding, default_impl)
1.166 +def parse(stream_or_string, html=0, htmlencoding=None, impl=None):
1.167 + doc = libxml2dom.parse(stream_or_string, html, htmlencoding, impl or default_impl)
1.168 + initialiseEvents(doc)
1.169 + return doc
1.170
1.171 -def parseFile(filename, html=0, htmlencoding=None):
1.172 - return libxml2dom.parseFile(filename, html, htmlencoding, default_impl)
1.173 +def parseFile(filename, html=0, htmlencoding=None, impl=None):
1.174 + doc = libxml2dom.parseFile(filename, html, htmlencoding, impl or default_impl)
1.175 + initialiseEvents(doc)
1.176 + return doc
1.177
1.178 -def parseString(s, html=0, htmlencoding=None):
1.179 - return libxml2dom.parseString(s, html, htmlencoding, default_impl)
1.180 +def parseString(s, html=0, htmlencoding=None, impl=None):
1.181 + doc = libxml2dom.parseString(s, html, htmlencoding, impl or default_impl)
1.182 + initialiseEvents(doc)
1.183 + return doc
1.184
1.185 -def parseURI(uri, html=0, htmlencoding=None):
1.186 - return libxml2dom.parseURI(uri, html, htmlencoding, default_impl)
1.187 +def parseURI(uri, html=0, htmlencoding=None, impl=None):
1.188 + doc = libxml2dom.parseURI(uri, html, htmlencoding, impl or default_impl)
1.189 + initialiseEvents(doc)
1.190 + return doc
1.191
1.192 # Single instance of the implementation.
1.193