Package suds :: Package sax :: Module text
[hide private]
[frames] | no frames]

Source Code for Module suds.sax.text

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the (LGPL) GNU Lesser General Public License as 
  3  # published by the Free Software Foundation; either version 3 of the  
  4  # License, or (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library Lesser General Public License for more details at 
 10  # ( http://www.gnu.org/licenses/lgpl.html ). 
 11  # 
 12  # You should have received a copy of the GNU Lesser General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # written by: Jeff Ortel ( jortel@redhat.com ) 
 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
50 - def escape(self):
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
62 - def unescape(self):
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
73 - def trim(self):
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
84 - def __repr__(self):
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 """
108 - def escape(self):
109 return self
110
111 - def unescape(self):
112 return self
113
114 - def __add__(self, other):
115 joined = u''.join((self, other)) 116 return Raw(joined, lang=self.lang)
117