1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
########################################################################
#
# File Name: CharacterData.py
#
# Documentation: http://docs.4suite.com/4DOM/CharacterData.py.html
#
"""
WWW: http://4suite.com/4DOM e-mail: support@4suite.com
Copyright (c) 2000 Fourthought Inc, USA. All Rights Reserved.
See http://4suite.com/COPYRIGHT for license and copyright information
"""
from xml.dom import Node
from DOMImplementation import implementation
from FtNode import FtNode
from ext import IsDOMString
from xml.dom import IndexSizeErr
from xml.dom import SyntaxErr
class CharacterData(FtNode):
def __init__(self, ownerDocument, data):
FtNode.__init__(self, ownerDocument)
self.__dict__['__nodeValue'] = data
self._length = len(data)
### Attribute Methods ###
def _get_data(self):
return self.__dict__['__nodeValue']
def _set_data(self, data):
if not IsDOMString(data):
raise SyntaxErr()
old_value = self.__dict__['__nodeValue']
self.__dict__['__nodeValue'] = data
self._length = len(data)
self._4dom_fireMutationEvent('DOMCharacterDataModified',
prevValue=old_value,
newValue=data)
def _get_length(self):
return self._length
### Methods ###
def appendData(self, arg):
if len(arg):
self._set_data(self.__dict__['__nodeValue'] + arg)
self._4dom_fireMutationEvent('DOMSubtreeModified')
return
def deleteData(self, offset, count):
if count < 0 or offset < 0 or offset > self._length:
raise IndexSizeErr()
data = self.__dict__['__nodeValue']
data = data[:int(offset)] + data[int(offset+count):]
self._set_data(data)
self._4dom_fireMutationEvent('DOMSubtreeModified')
return
def insertData(self, offset, arg):
if offset < 0 or offset > self._length:
raise IndexSizeErr()
if not IsDOMString(arg):
raise SyntaxErr()
data = self.__dict__['__nodeValue']
data = data[:int(offset)] + arg + data[int(offset):]
self._set_data(data)
self._4dom_fireMutationEvent('DOMSubtreeModified')
return
def replaceData(self, offset, count, arg):
if not IsDOMString(arg):
raise SyntaxErr()
if count < 0 or offset < 0 or offset > self._length:
raise IndexSizeErr()
data = self.__dict__['__nodeValue']
data = data[:int(offset)] + arg + data[int(offset+count):]
self._set_data(data)
self._4dom_fireMutationEvent('DOMSubtreeModified')
return
def substringData(self, offset, count):
if count < 0 or offset < 0 or offset > self._length:
raise IndexSizeErr()
return self.data[int(offset):int(offset+count)]
### Helper Functions For Cloning ###
def _4dom_clone(self, owner):
return self.__class__(owner, self.data)
def __getinitargs__(self):
return (self.ownerDocument,
self.data
)
### Overridden Methods ###
def __repr__(self):
# Trim to a managable size
if len(self.data) > 20:
data = self.data[:20] + '...'
else:
data = self.data
# Escape unprintable chars
import string
for ws in ['\t','\n','\r']:
data = string.replace(data, ws, '\\0x%x' % ord(ws))
return "<%s Node at %x: %s>" % (
self.__class__.__name__,
id(self),
repr(data))
### Attribute Access Mappings ###
_readComputedAttrs = FtNode._readComputedAttrs.copy()
_readComputedAttrs.update({
'length':_get_length,
'data':_get_data
})
_writeComputedAttrs = FtNode._writeComputedAttrs.copy()
_writeComputedAttrs.update({
'data':_set_data
})
# Create the read-only list of attributes
_readOnlyAttrs = filter(lambda k,m=_writeComputedAttrs: not m.has_key(k),
FtNode._readOnlyAttrs + _readComputedAttrs.keys())
|