root/trunk/proj/w3c/xmlNamespaceMap.py

Revision 471, 4.9 kB (checked in by shane, 4 years ago)

Fixed reference test failing. Misc cleanup

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ Copyright (C) 2002-2004  TechGame Networks, LLC.
4 ##~
5 ##~ This library is free software; you can redistribute it and/or
6 ##~ modify it under the terms of the BSD style License as found in the
7 ##~ LICENSE file included with this distribution.
8 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9
10 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #~ Imports
12 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13
14 from xml.sax.saxutils import quoteattr as _xmlquoteattr
15
16 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 #~ Definitions
18 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19
20 class xmlprefix(str):
21     """A class change to distinguish xml prefixes from a namespace"""
22
23 def isXMLPrefix(obj):
24     return isinstance(obj, xmlprefix)
25
26 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27
28 class XMLNamespaceMap(object):
29     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30     #~ Constants / Variables / Etc.
31     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32
33     default_xmlnsmap = {}
34
35     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36     #~ Special Methods
37     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38
39     def __init__(self, nextmap=None, default_xmlnsmap=None):
40         self.nextmap = nextmap
41         if default_xmlnsmap: self.xmlnsmap = default_xmlnsmap
42         else: self.xmlnsmap = self.default_xmlnsmap.copy()
43
44     def __iter__(self):
45         return self.iterxmlns(False)
46
47     def __contains__(self, key):
48         return key in self.xmlnsmap
49
50     def __len__(self):
51         return len(self.xmlnsmap)
52
53     def __getitem__(self, key):
54         return self.xmlnsmap[key]
55
56     def __setitem__(self, key, value):
57         raise NotImplementedError, "Use setxmlns instead."
58
59     def __delitem__(self, key):
60         del self.xmlnsmap[self.xmlnsmap[key]]
61         del self.xmlnsmap[key]
62
63     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64     #~ Public Methods
65     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66
67     def next(self):
68         return self.nextmap
69
70     def copy(self, klass=None):
71         klass = klass or self.__class__
72         return klass(self.next(), default_xmlnsmap=self.xmlnsmap.copy())
73
74     def newChain(self, chainlevel=0, klass=None):
75         klass = klass or self.__class__
76         if chainlevel == 0:
77             return klass(self)
78         elif chainlevel < 0 and self.next():
79             return self.next().newChain(chainlevel+1, klass)
80         else:
81             return klass()
82
83     def get(self, key, default=None):
84         return self.xmlnsmap.get(key, default)
85
86     def setxmlns(self, prefix, xmlns):
87         """Adds an xml namespace mapping entry"""
88         prefix = xmlprefix(prefix or '')
89         try:
90             key = self.xmlnsmap[prefix]
91             del self.xmlnsmap[key]
92         except KeyError: pass
93         try:
94             key = self.xmlnsmap[xmlns]
95             del self.xmlnsmap[key]
96         except KeyError: pass
97         self.xmlnsmap[prefix] = xmlns
98         self.xmlnsmap[xmlns] = prefix
99
100     def _xmlns(self, prefix):
101         return self.xmlnsmap[prefix]
102
103     def xmlns(self, prefix, include_next=True):
104         """Returns the xmlns string, given the prefix string"""
105         try:
106             return self._xmlns(prefix)
107         except KeyError:
108             if include_next and self.next() is not None:
109                 return self.next().xmlns(prefix)
110             else:
111                 return None
112
113     def _prefix(self, xmlns):
114         return str(self.xmlnsmap[xmlns])
115
116     def prefix(self, xmlns, include_next=True):
117         """Returns the prefix string, given the xmlns"""
118         try:
119             return self._prefix(xmlns)
120         except KeyError:
121             if include_next and self.next() is not None:
122                 return self.next().xmlns(xmlns)
123             else:
124                 return None
125
126     def iterPrefix(self, include_next=True):
127         for key, prefix in self.xmlnsmap.iteritems():
128             if not isXMLPrefix(key):
129                 yield prefix, key
130
131         for prefix, ns in include_next and self.next() or ():
132             if prefix not in self.xmlnsmap:
133                 yield prefix, ns
134
135     def iterxmlns(self, include_next=True):
136         for key, prefix in self.xmlnsmap.iteritems():
137             if not isXMLPrefix(key):
138                 yield key, prefix
139
140         for prefix, ns in include_next and self.next() or ():
141             if prefix not in self.xmlnsmap:
142                 yield ns, prefix
143
144     def toXML(self, include_next=False):
145         result = ['']
146         for prefix, xmlns in self.iterPrefix(include_next):
147             xmlns = _xmlquoteattr(xmlns or '')
148             if prefix: result.append('xmlns:%s=%s' % (prefix, xmlns))
149             else: result.append('xmlns=%s' % (xmlns,))
150         return ' '.join(result)
151
Note: See TracBrowser for help on using the browser.