File: ParsedAbbreviatedAbsoluteLocationPath.py

package info (click to toggle)
python-xml 0.8.4-10.1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,972 kB
  • ctags: 10,628
  • sloc: python: 46,730; ansic: 14,354; xml: 968; makefile: 201; sh: 20
file content (68 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download | duplicates (3)
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
########################################################################
#
# File Name:   ParsedAbbreviatedAbsoluteLocationPath.py
#
#
"""
A parsed token for an abreviated absolute location path.
WWW: http://4suite.org/XPATH        e-mail: support@4suite.org

Copyright (c) 2000-2001 Fourthought Inc, USA.   All Rights Reserved.
See  http://4suite.org/COPYRIGHT  for license and copyright information
"""

from xml.xpath import ParsedNodeTest
from xml.xpath import ParsedPredicateList
from xml.xpath import ParsedStep
from xml.xpath import ParsedAxisSpecifier

LOOKAHEAD_OPTIMIZERS = {
    }

class ParsedAbbreviatedAbsoluteLocationPath:
    def __init__(self,rel):
        self._rel = rel
        nt = ParsedNodeTest.ParsedNodeTest('node', '')
        ppl = ParsedPredicateList.ParsedPredicateList([])
        as = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
        self._step = ParsedStep.ParsedStep(as, nt, ppl)
        return

    def evaluate(self, context):
        origState = context.copyNodePosSize()

        root = context.node.ownerDocument or context.node
        context.setNodePosSize((root,1,1))
        rt = self._step.select(context)
        res = []
        l = len(rt)
        
        sub_rt = []
        for ctr in range(l):
            n = rt[ctr]
            context.setNodePosSize((n,ctr+1,l))
            sub_rt.extend(self._rel.select(context))

        if sub_rt and hasattr(sub_rt[0], 'ownerElement'):
            result = sub_rt
        else:
            result = filter(lambda x, compare=sub_rt: x in compare, rt)

        context.setNodePosSize(origState)
        return result
    select = evaluate
    
    def pprint(self, indent=''):
        print indent + str(self)
        self._step.pprint(indent + '  ')
        self._rel.pprint(indent + '  ')

    def __str__(self):
        return '<AbbreviatedAbsoluteLocationPath at %x: %s>' % (
            id(self),
            repr(self)
            )

    def __repr__(self):
        return '/%s/%s' % (repr(self._step), repr(self._rel))