1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Provides XML I{attribute} classes.
19 """
20
21 import suds.sax
22 from logging import getLogger
23 from suds import *
24 from suds.sax import *
25 from suds.sax.text import Text
26
27 log = getLogger(__name__)
28
30 """
31 An XML attribute object.
32 @ivar parent: The node containing this attribute
33 @type parent: L{element.Element}
34 @ivar prefix: The I{optional} namespace prefix.
35 @type prefix: basestring
36 @ivar name: The I{unqualified} name of the attribute
37 @type name: basestring
38 @ivar value: The attribute's value
39 @type value: basestring
40 """
42 """
43 @param name: The attribute's name with I{optional} namespace prefix.
44 @type name: basestring
45 @param value: The attribute's value
46 @type value: basestring
47 """
48 self.parent = None
49 self.prefix, self.name = splitPrefix(name)
50 self.setValue(value)
51
52 - def clone(self, parent=None):
53 """
54 Clone this object.
55 @param parent: The parent for the clone.
56 @type parent: L{element.Element}
57 @return: A copy of this object assigned to the new parent.
58 @rtype: L{Attribute}
59 """
60 a = Attribute(self.qname(), self.value)
61 a.parent = parent
62 return a
63
65 """
66 Get the B{fully} qualified name of this attribute
67 @return: The fully qualified name.
68 @rtype: basestring
69 """
70 if self.prefix is None:
71 return self.name
72 else:
73 return ':'.join((self.prefix, self.name))
74
76 """
77 Set the attributes value
78 @param value: The new value (may be None)
79 @type value: basestring
80 @return: self
81 @rtype: L{Attribute}
82 """
83 if isinstance(value, Text):
84 self.value = value
85 else:
86 self.value = Text(value)
87 return self
88
90 """
91 Get the attributes value with optional default.
92 @param default: An optional value to be return when the
93 attribute's has not been set.
94 @type default: basestring
95 @return: The attribute's value, or I{default}
96 @rtype: L{Text}
97 """
98 if self.hasText():
99 return self.value
100 else:
101 return default
102
104 """
105 Get whether the attribute has I{text} and that it is not an empty
106 (zero length) string.
107 @return: True when has I{text}.
108 @rtype: boolean
109 """
110 return ( self.value is not None and len(self.value) )
111
113 """
114 Get the attributes namespace. This may either be the namespace
115 defined by an optional prefix, or its parent's namespace.
116 @return: The attribute's namespace
117 @rtype: (I{prefix}, I{name})
118 """
119 if self.prefix is None:
120 return Namespace.default
121 else:
122 return self.resolvePrefix(self.prefix)
123
125 """
126 Resolve the specified prefix to a known namespace.
127 @param prefix: A declared prefix
128 @type prefix: basestring
129 @return: The namespace that has been mapped to I{prefix}
130 @rtype: (I{prefix}, I{name})
131 """
132 ns = Namespace.default
133 if self.parent is not None:
134 ns = self.parent.resolvePrefix(prefix)
135 return ns
136
137 - def match(self, name=None, ns=None):
138 """
139 Match by (optional) name and/or (optional) namespace.
140 @param name: The optional attribute tag name.
141 @type name: str
142 @param ns: An optional namespace.
143 @type ns: (I{prefix}, I{name})
144 @return: True if matched.
145 @rtype: boolean
146 """
147 if name is None:
148 byname = True
149 else:
150 byname = ( self.name == name )
151 if ns is None:
152 byns = True
153 else:
154 byns = ( self.namespace()[1] == ns[1] )
155 return ( byname and byns )
156
158 """ equals operator """
159 return rhs is not None and \
160 isinstance(rhs, Attribute) and \
161 self.prefix == rhs.name and \
162 self.name == rhs.name
163
165 """ get a string representation """
166 return \
167 'attr (prefix=%s, name=%s, value=(%s))' %\
168 (self.prefix, self.name, self.value)
169
171 """ get an xml string representation """
172 return unicode(self).encode('utf-8')
173
175 """ get an xml string representation """
176 n = self.qname()
177 if self.hasText():
178 v = self.value.escape()
179 else:
180 v = self.value
181 return u'%s="%s"' % (n, v)
182