Ejemplo n.º 1
0
    def evaluate(self, context):
        if self._op == "=":
            true = boolean.true
            false = boolean.false
        else:
            true = boolean.false
            false = boolean.true

        lrt = self._left.evaluate(context)
        rrt = self._right.evaluate(context)
        lType = type(lrt)
        rType = type(rrt)
        if lType == list == rType:
            # Node set to node set
            for right_curr in rrt:
                right_curr = Conversions.StringValue(right_curr)
                for left_curr in lrt:
                    if right_curr == Conversions.StringValue(left_curr):
                        return true
            return false
        elif lType == list or rType == list:
            func = None
            if lType == list:
                set = lrt
                val = rrt
            else:
                set = rrt
                val = lrt
            if type(val) in NumberTypes:
                func = Conversions.NumberValue
            elif boolean.IsBooleanType(val):
                func = Conversions.BooleanValue
            elif type(val) == bytes:
                func = Conversions.StringValue
            else:
                # Deal with e.g. RTFs
                val = Conversions.StringValue(val)
                func = Conversions.StringValue
            for n in set:
                if func(n) == val:
                    return true
            return false

        if boolean.IsBooleanType(lrt) or boolean.IsBooleanType(rrt):
            rt = Conversions.BooleanValue(lrt) == Conversions.BooleanValue(rrt)
        elif lType in NumberTypes or rType in NumberTypes:
            rt = Conversions.NumberValue(lrt) == Conversions.NumberValue(rrt)
        else:
            rt = Conversions.StringValue(lrt) == Conversions.StringValue(rrt)
        if rt:
            # Due to the swapping of true/false, true might evaluate to 0
            # We cannot compact this to 'rt and true or false'
            return true
        return false
Ejemplo n.º 2
0
    def instantiate(self, context, processor, new_level=1):
        origState = context.copy()
        context.setNamespaces(self._nss)

        rec_tpl_params = None
        result = self._expr.evaluate(context)
        test = Conversions.BooleanValue(result)
        if test:
            for (recurse, child) in self._elements:
                if recurse:
                    context, rec_tpl_params = child.instantiate(
                        context, processor, new_level)
                else:
                    context = child.instantiate(context, processor)[0]

        context.set(origState)
        return (context, rec_tpl_params)
Ejemplo n.º 3
0
    def instantiate(self, context, processor, new_level=1):

        origState = context.copyNamespaces()
        context.setNamespaces(self._nss)

        rec_tpl_params = None
        result = self._expr.evaluate(context)
        test = Conversions.BooleanValue(result)
        if int(test):
            for child in self.childNodes:
                if child.namespaceURI == xml.xslt.XSL_NAMESPACE and child.localName in [
                        'call-template', 'if', 'choose'
                ]:
                    context, rec_tpl_params = child.instantiate(
                        context, processor, new_level)
                else:
                    context = child.instantiate(context, processor)[0]

        context.setNamespaces(origState)

        return (context, int(test), rec_tpl_params)
Ejemplo n.º 4
0
 def filter(self, nodeList, context, reverse):
     if self._length:
         state = context.copyNodePosSize()
         for pred in self._predicates:
             size = len(nodeList)
             ctr = 0
             current = nodeList
             nodeList = []
             for node in current:
                 position = (reverse and size - ctr) or (ctr + 1)
                 context.setNodePosSize((node, position, size))
                 res = pred.evaluate(context)
                 if type(res) in NumberTypes:
                     # This must be separate to prevent falling into
                     # the boolean check.
                     if res == position:
                         nodeList.append(node)
                 elif Conversions.BooleanValue(res):
                     nodeList.append(node)
                 ctr = ctr + 1
         context.setNodePosSize(state)
     return nodeList
Ejemplo n.º 5
0
def XsltBooleanValue(object):
    handled, value = XsltStringValue(object)
    if handled:
        return 1, Conversions.BooleanValue(value)
    return 0, None
Ejemplo n.º 6
0
def if_function(context, cond, v1, v2):
    if Conversions.BooleanValue(cond):
        return v1
    else:
        return v2
Ejemplo n.º 7
0
def Not(context, object):
    """Function: <boolean> not(<boolean>)"""
    return (not Conversions.BooleanValue(object)
            and boolean.true) or boolean.false
Ejemplo n.º 8
0
def _Boolean(context, object):
    """Function: <boolean> boolean(<object>)"""
    return Conversions.BooleanValue(object)