1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Contains XML text classes.
19 """
20
21 from suds import *
22 from suds.sax import *
23
24
25 -class Text(unicode):
26 """
27 An XML text object used to represent text content.
28 @ivar lang: The (optional) language flag.
29 @type lang: bool
30 @ivar escaped: The (optional) XML special character escaped flag.
31 @type escaped: bool
32 """
33 __slots__ = ('lang', 'escaped',)
34
35 @classmethod
36 - def __valid(cls, *args):
37 return ( len(args) and args[0] is not None )
38
39 - def __new__(cls, *args, **kwargs):
40 if cls.__valid(*args):
41 lang = kwargs.pop('lang', None)
42 escaped = kwargs.pop('escaped', False)
43 result = super(Text, cls).__new__(cls, *args, **kwargs)
44 result.lang = lang
45 result.escaped = escaped
46 else:
47 result = None
48 return result
49
51 """
52 Encode (escape) special XML characters.
53 @return: The text with XML special characters escaped.
54 @rtype: L{Text}
55 """
56 if not self.escaped:
57 post = sax.encoder.encode(self)
58 escaped = ( post != self )
59 return Text(post, lang=self.lang, escaped=escaped)
60 return self
61
63 """
64 Decode (unescape) special XML characters.
65 @return: The text with escaped XML special characters decoded.
66 @rtype: L{Text}
67 """
68 if self.escaped:
69 post = sax.encoder.decode(self)
70 return Text(post, lang=self.lang)
71 return self
72
74 post = self.strip()
75 return Text(post, lang=self.lang, escaped=self.escaped)
76
77 - def __add__(self, other):
78 joined = u''.join((self, other))
79 result = Text(joined, lang=self.lang, escaped=self.escaped)
80 if isinstance(other, Text):
81 result.escaped = ( self.escaped or other.escaped )
82 return result
83
85 s = [self]
86 if self.lang is not None:
87 s.append(' [%s]' % self.lang)
88 if self.escaped:
89 s.append(' <escaped>')
90 return ''.join(s)
91
92 - def __getstate__(self):
93 state = {}
94 for k in self.__slots__:
95 state[k] = getattr(self, k)
96 return state
97
98 - def __setstate__(self, state):
99 for k in self.__slots__:
100 setattr(self, k, state[k])
101
102
103 -class Raw(Text):
104 """
105 Raw text which is not XML escaped.
106 This may include I{string} XML.
107 """
110
113
115 joined = u''.join((self, other))
116 return Raw(joined, lang=self.lang)
117