1 #!/usr/bin/env python 2 3 """ 4 SVG-specific document support. See: 5 http://www.w3.org/TR/SVGMobile12/python-binding.html 6 7 Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> 8 9 This library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Lesser General Public 11 License as published by the Free Software Foundation; either 12 version 2.1 of the License, or (at your option) any later version. 13 14 This library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Lesser General Public License for more details. 18 19 You should have received a copy of the GNU Lesser General Public 20 License along with this library; if not, write to the Free Software 21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 22 """ 23 24 import libxml2dom 25 from libxml2dom.macrolib import * 26 from libxml2dom.macrolib import \ 27 createDocument as Node_createDocument, \ 28 parseString as Node_parseString, parseURI as Node_parseURI, \ 29 parseFile as Node_parseFile, \ 30 toString as Node_toString, toStream as Node_toStream, \ 31 toFile as Node_toFile 32 33 SVG_NAMESPACE = "http://www.w3.org/2000/svg" 34 35 class SVGImplementation(libxml2dom.Implementation): 36 37 "Contains an SVG-specific implementation." 38 39 # Wrapping of documents. 40 41 def adoptDocument(self, node): 42 return SVGDocument(node, self) 43 44 # Factory functions. 45 46 def get_node(self, _node, context_node): 47 if Node_nodeType(_node) == context_node.ELEMENT_NODE and \ 48 Node_namespaceURI(_node) == SVG_NAMESPACE: 49 50 if Node_localName(_node) == "svg": 51 return SVGSVGElement(_node, self, context_node.ownerDocument) 52 else: 53 return SVGElement(_node, self, context_node.ownerDocument) 54 else: 55 return libxml2dom.Implementation.get_node(self, _node, context_node) 56 57 class SVGElement(libxml2dom.Node): # Element, EventTarget, TraitAccess, ElementTraversal 58 59 "An SVG-specific element." 60 61 def _id(self): 62 return self.getAttribute("id") 63 64 def _setId(self, value): 65 self.setAttribute("id", value) 66 67 id = property(_id, _setId) 68 69 class SVGLocatable: 70 71 "A locatable interface." 72 73 class SVGLocatableElement(SVGElement, SVGLocatable): 74 75 "A locatable element." 76 77 class SVGSVGElement(SVGLocatableElement): # SVGTimedElement 78 79 "An SVG-specific top-level element." 80 81 NAV_AUTO = 1 82 NAV_NEXT = 2 83 NAV_PREV = 3 84 NAV_UP = 4 85 NAV_UP_RIGHT = 5 86 NAV_RIGHT = 6 87 NAV_DOWN_RIGHT = 7 88 NAV_DOWN = 8 89 NAV_DOWN_LEFT = 9 90 NAV_LEFT = 10 91 NAV_UP_LEFT = 11 92 93 class SVGDocument(libxml2dom.Document): 94 95 "An SVG-specific document node." 96 97 # NOTE: Define global (SVGGlobal). 98 99 # Convenience functions. 100 101 def parse(stream_or_string, html=0, htmlencoding=None): 102 return libxml2dom.parse(stream_or_string, html, htmlencoding, default_impl) 103 104 def parseFile(filename, html=0, htmlencoding=None): 105 return libxml2dom.parseFile(filename, html, htmlencoding, default_impl) 106 107 def parseString(s, html=0, htmlencoding=None): 108 return libxml2dom.parseString(s, html, htmlencoding, default_impl) 109 110 def parseURI(uri, html=0, htmlencoding=None): 111 return libxml2dom.parseURI(uri, html, htmlencoding, default_impl) 112 113 # Single instance of the implementation. 114 115 default_impl = SVGImplementation() 116 117 # vim: tabstop=4 expandtab shiftwidth=4