File: STDOM.py

package info (click to toggle)
python-happydoc 2.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,176 kB
  • ctags: 3,347
  • sloc: python: 11,321; makefile: 88; sh: 77
file content (664 lines) | stat: -rw-r--r-- 17,195 bytes parent folder | download | duplicates (2)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""
DOM implementation in StructuredText : Read-Only methods

All standard Zope objects support DOM to a limited extent.
"""
import string


# Node type codes
# ---------------

ELEMENT_NODE                  = 1
ATTRIBUTE_NODE                = 2
TEXT_NODE                     = 3
CDATA_SECTION_NODE            = 4
ENTITY_REFERENCE_NODE         = 5
ENTITY_NODE                   = 6
PROCESSING_INSTRUCTION_NODE   = 7
COMMENT_NODE                  = 8
DOCUMENT_NODE                 = 9
DOCUMENT_TYPE_NODE            = 10
DOCUMENT_FRAGMENT_NODE        = 11
NOTATION_NODE                 = 12

# Exception codes
# ---------------

INDEX_SIZE_ERR                = 1
DOMSTRING_SIZE_ERR            = 2
HIERARCHY_REQUEST_ERR         = 3
WRONG_DOCUMENT_ERR            = 4
INVALID_CHARACTER_ERR         = 5
NO_DATA_ALLOWED_ERR           = 6
NO_MODIFICATION_ALLOWED_ERR   = 7
NOT_FOUND_ERR                 = 8
NOT_SUPPORTED_ERR             = 9
INUSE_ATTRIBUTE_ERR           = 10

# Exceptions
# ----------

class DOMException(Exception):
    pass
class IndexSizeException(DOMException):
    code = INDEX_SIZE_ERR
class DOMStringSizeException(DOMException):
    code = DOMSTRING_SIZE_ERR
class HierarchyRequestException(DOMException):
    code = HIERARCHY_REQUEST_ERR
class WrongDocumentException(DOMException):
    code = WRONG_DOCUMENT_ERR
class InvalidCharacterException(DOMException):
    code = INVALID_CHARACTER_ERR
class NoDataAllowedException(DOMException):
    code = NO_DATA_ALLOWED_ERR
class NoModificationAllowedException(DOMException):
    code = NO_MODIFICATION_ALLOWED_ERR
class NotFoundException(DOMException):
    code = NOT_FOUND_ERR
class NotSupportedException(DOMException):
    code = NOT_SUPPORTED_ERR
class InUseAttributeException(DOMException):
    code = INUSE_ATTRIBUTE_ERR

# Node classes
# ------------

class ParentNode:
   """
   A node that can have children, or, more precisely, that implements
   the child access methods of the DOM.
   """

   def getChildNodes(self, type=type, st=type('')):
      """
      Returns a NodeList that contains all children of this node.
      If there are no children, this is a empty NodeList
      """
      
      r=[]
      for n in self.getChildren():
         if type(n) is st: n=TextNode(n)
         r.append(n.__of__(self))
      
      return  NodeList(r)
   
   def getFirstChild(self, type=type, st=type('')):
      """
      The first child of this node. If there is no such node
      this returns None
      """
      children = self.getChildren()

      if not children:
         return None
         
      n=children[0]

      if type(n) is st:
         n=TextNode(n)
         
      return n.__of__(self)

   def getLastChild(self, type=type, st=type('')):
      """
      The last child of this node.  If there is no such node
      this returns None.
      """
      children = self.getChildren()
      if not children: return None
      n=chidren[-1]
      if type(n) is st: n=TextNode(n)
      return n.__of__(self)

   """
   create aliases for all above functions in the pythony way.
   """
   
   def _get_ChildNodes(self, type=type, st=type('')):
      return self.getChildNodes(type,st)
   
   def _get_FirstChild(self, type=type, st=type('')):
      return self.getFirstChild(type,st)

   def _get_LastChild(self, type=type, st=type('')):
      return self.getLastChild(type,st)
  
class NodeWrapper(ParentNode):
   """
   This is an acquisition-like wrapper that provides parent access for 
   DOM sans circular references!
   """

   def __init__(self, aq_self, aq_parent):
      self.aq_self=aq_self
      self.aq_parent=aq_parent

   def __getattr__(self, name):
      return getattr(self.aq_self, name)
    
   def getParentNode(self):
      """
      The parent of this node.  All nodes except Document
      DocumentFragment and Attr may have a parent
      """
      return self.aq_parent

   def _getDOMIndex(self, children, getattr=getattr):
      i=0
      self=self.aq_self
      for child in children:
         if getattr(child, 'aq_self', child) is self:
            self._DOMIndex=i
            return i
         i=i+1
      return None

   def getPreviousSibling(self,
                          type=type,
                          st=type(''),
                          getattr=getattr,
                          None=None):

      """
      The node immediately preceding this node.  If
      there is no such node, this returns None.
      """
      
      children = self.aq_parent.getChildren()
      if not children:
         return None

      index=getattr(self, '_DOMIndex', None)
      if index is None:
         index=self._getDOMIndex(children)
         if index is None: return None

      index=index-1
      if index < 0: return None
      try: n=children[index]
      except IndexError: return None
      else:
         if type(n) is st:
            n=TextNode(n)
         n._DOMIndex=index
         return n.__of__(self)


   def getNextSibling(self, type=type, st=type('')):
      """
      The node immediately preceding this node.  If
      there is no such node, this returns None.
      """
      children = self.aq_parent.getChildren()
      if not children:
         return None

      index=getattr(self, '_DOMIndex', None)
      if index is None:
         index=self._getDOMIndex(children)
         if index is None:
            return None

      index=index+1
      try: n=children[index]
      except IndexError:
         return None
      else:
         if type(n) is st:
            n=TextNode(n)
         n._DOMIndex=index
         return n.__of__(self)

   def getOwnerDocument(self):
      """
      The Document object associated with this node, if any.
      """
      return self.aq_parent.getOwnerDocument()

   """
   create aliases for all above functions in the pythony way.   
   """
   
   def _get_ParentNode(self):
      return self.getParentNode()

   def _get_DOMIndex(self, children, getattr=getattr):
      return self._getDOMIndex(children,getattr)
      
   def _get_PreviousSibling(self,
                          type=type,
                          st=type(''),
                          getattr=getattr,
                          None=None):

      return self.getPreviousSibling(type,st,getattr,None)
      
   def _get_NextSibling(self, type=type, st=type('')):
      return self.getNextSibling(type,st)
      
   def _get_OwnerDocument(self):
      return self.getOwnerDocument()
      
class Node(ParentNode):
   """
   Node Interface
   """

   # Get a DOM wrapper with a parent link
   def __of__(self, parent):
      return NodeWrapper(self, parent)

   # DOM attributes    
   # --------------
    
   def getNodeName(self):
      """
      The name of this node, depending on its type
      """

   def getNodeValue(self):
      """
      The value of this node, depending on its type
      """
      return None

   def getParentNode(self):
      """
      The parent of this node.  All nodes except Document
      DocumentFragment and Attr may have a parent
      """

   def getChildren(self):
      """
      Get a Python sequence of children
      """
      return ()

   def getPreviousSibling(self,
                          type=type,
                          st=type(''),
                          getattr=getattr,
                          None=None):
      """
      The node immediately preceding this node.  If
      there is no such node, this returns None.
      """

   def getNextSibling(self, type=type, st=type('')):
      """
      The node immediately preceding this node.  If
      there is no such node, this returns None.
      """

   def getAttributes(self):
      """
      Returns a NamedNodeMap containing the attributes
      of this node (if it is an element) or None otherwise.
      """
      return None

   def getOwnerDocument(self):
      """
      The Document object associated with this node, if any.
      """
        
    # DOM Methods    
    # -----------
    
   def hasChildNodes(self):
      """
      Returns true if the node has any children, false
      if it doesn't.
      """
      return len(self.getChildren())

   """
   create aliases for all above functions in the pythony way.
   """

   def _get_NodeName(self):
      return self.getNodeName()
      
   def _get_NodeValue(self):
      return self.getNodeValue()
      
   def _get_ParentNode(self):
      return self.getParentNode()

   def _get_Children(self):
      return self.getChildren()

   def _get_PreviousSibling(self,
                          type=type,
                          st=type(''),
                          getattr=getattr,
                          None=None):
      
      return self.getPreviousSibling(type,st,getattr,None)
      
   def _get_NextSibling(self, type=type, st=type('')):
      return self.getNextSibling()

   def _get_Attributes(self):
      return self.getAttributes()

   def _get_OwnerDocument(self):
      return self.getOwnerDocument()
    
   def _has_ChildNodes(self):
      return self.hasChildNodes()
      
         
class TextNode(Node):

   def __init__(self, str): self._value=str
      
   def getNodeType(self):
      return TEXT_NODE
      
   def getNodeName(self):
      return '#text'

   def getNodeValue(self):
      return self._value
   
   """
   create aliases for all above functions in the pythony way.
   """
   
   def _get_NodeType(self):
      return self.getNodeType()
      
   def _get_NodeName(self):
      return self.getNodeName()

   def _get_NodeValue(self):
      return self.getNodeValue()

class Element(Node):
   """
   Element interface
   """
    
   # Element Attributes
   # ------------------
    
   def getTagName(self):
      """The name of the element"""
      return self.__class__.__name__
    
   def getNodeName(self):
      """The name of this node, depending on its type"""
      return self.__class__.__name__
      
   def getNodeType(self):
      """A code representing the type of the node."""
      return ELEMENT_NODE

   def getNodeValue(self, type=type, st=type('')):
      r=[]
      for c in self.getChildren():
         if type(c) is not st:
            c=c.getNodeValue()
         r.append(c)
      return string.join(r,'')
    
   def getParentNode(self):
      """
      The parent of this node.  All nodes except Document
      DocumentFragment and Attr may have a parent
      """
      
   # Element Methods
   # ---------------
    
   _attributes=()

   def getAttribute(self, name): return getattr(self, name, None)
   def getAttributeNode(self, name):
      if hasattr(self, name):
         return Attr(name, getattr(self, name))

   def getAttributes(self):
      d={}
      for a in self._attributes:
         d[a]=getattr(self, a, '')
      return NamedNodeMap(d)

   def getAttribute(self, name):
      """Retrieves an attribute value by name."""
      return None

   def getAttributeNode(self, name):
      """ Retrieves an Attr node by name or None if
      there is no such attribute. """
      return None

   def getElementsByTagName(self, tagname):
      """
      Returns a NodeList of all the Elements with a given tag
      name in the order in which they would be encountered in a
      preorder traversal of the Document tree.  Parameter: tagname
      The name of the tag to match (* = all tags). Return Value: A new
      NodeList object containing all the matched Elements.
      """
      nodeList = []
      for child in self.getChildren():
         if (child.getNodeType()==ELEMENT_NODE and \
             child.getTagName()==tagname or tagname== '*'):
                
            nodeList.append(child)
               
         if hasattr(child, 'getElementsByTagName'):
            n1       = child.getElementsByTagName(tagname)
            nodeList = nodeList + n1._data
      return NodeList(nodeList)
   
   """
   create aliases for all above functions in the pythony way.
   """

   def _get_TagName(self):
      return self.getTagName()
      
   def _get_NodeName(self):
      return self.getNodeName()
      
   def _get_NodeType(self):
      return self.getNodeType()
      
   def _get_NodeValue(self, type=type, st=type('')):
      return self.getNodeValue(type,st)
      
   def _get_ParentNode(self):
      return self.getParentNode()
      
   def _get_Attribute(self, name):
      return self.getAttribute(name)
   
   def _get_AttributeNode(self, name):
      return self.getAttributeNode(name)
      
   def _get_Attributes(self):
      return self.getAttributes()
      
   def _get_Attribute(self, name):
      return self.getAttribute(name)
      
   def _get_AttributeNode(self, name):
      return self.getAttributeNode(name)
      
   def _get_ElementsByTagName(self, tagname):
      return self.getElementsByTagName(tagname)
      
         
class NodeList:
   """
   NodeList interface - Provides the abstraction of an ordered
   collection of nodes.
    
   Python extensions: can use sequence-style 'len', 'getitem', and
   'for..in' constructs.
   """
    
   def __init__(self,list=None):
      self._data = list or []
    
   def __getitem__(self, index, type=type, st=type('')):
      return self._data[index]

   def __getslice__(self, i, j):
      return self._data[i:j]
    
   def item(self, index):
      """
      Returns the index-th item in the collection
      """
      try:  return self._data[index]    
      except IndexError: return None
         
   def getLength(self):
      """
      The length of the NodeList
      """
      return len(self._data)
    
   __len__=getLength
   
   """
   create aliases for all above functions in the pythony way.
   """
   
   def _get_Length(self):
      return self.getLength()
 
class NamedNodeMap:
   """
   NamedNodeMap interface - Is used to represent collections
   of nodes that can be accessed by name.  NamedNodeMaps are not
   maintained in any particular order.
    
   Python extensions: can use sequence-style 'len', 'getitem', and
   'for..in' constructs, and mapping-style 'getitem'.
   """
    
   def __init__(self, data=None):
      if data is None:
         data = {}
      self._data = data

   def item(self, index):
      """
      Returns the index-th item in the map
      """
      try: return self._data.values()[index]
      except IndexError: return None
        
   def __getitem__(self, key):
      if type(key)==type(1):
         return self._data.values()[key]
      else:
         return self._data[key]
            
   def getLength(self):
      """
      The length of the NodeList
      """
      return len(self._data)
    
   __len__ = getLength
    
   def getNamedItem(self, name):
      """
      Retrieves a node specified by name. Parameters:
      name Name of a node to retrieve. Return Value A Node (of any
      type) with the specified name, or None if the specified name
      did not identify any node in the map.
      """
      if self._data.has_key(name): 
         return self._data[name]
      return None

   """
   create aliases for all above functions in the pythony way.
   """
   def _get_Length(self):
      return self.getLength()
      
   def _get_NamedItem(self, name):
      return self.getNamedItem(name)
      
class Attr(Node):
   """
   Attr interface - The Attr interface represents an attriubte in an
   Element object. Attr objects inherit the Node Interface
   """

   def __init__(self, name, value, specified=1):
      self.name = name
      self.value = value
      self.specified = specified
        
   def getNodeName(self):
      """
      The name of this node, depending on its type
      """
      return self.name

   def getName(self):
      """
      Returns the name of this attribute.
      """
      return self.name
    
   def getNodeValue(self):
      """
      The value of this node, depending on its type
      """
      return self.value

   def getNodeType(self):
      """
      A code representing the type of the node.
      """
      return ATTRIBUTE_NODE

   def getSpecified(self):
      """
      If this attribute was explicitly given a value in the
      original document, this is true; otherwise, it is false.
      """
      return self.specified
        
   """
   create aliases for all above functions in the pythony way.
   """
   
   def _get_NodeName(self):
      return self.getNodeName()

   def _get_Name(self):
      return self.getName()
    
   def _get_NodeValue(self):
      return self.getNodeValue()

   def _get_NodeType(self):
      return self.getNodeType()

   def _get_Specified(self):
      return self.getSpecified()