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