Ejemplo n.º 1
0
class ReplaceElement(XsltElement):
    """
    f:replace performs a search and replace on a string, placing the results
    in the output.  The content is treated as a template.  The string value
    of the output from this template is the replacement string.
    All instances of the string given by the 'substring' attribute
    are replaced with the replacement string.
    """
    legalAttrs = {
        'string':
        AttributeInfo.StringExpression(
            description=
            "The string to be processed.  If not given, the string value of the context node is used."
        ),
        'substring':
        AttributeInfo.StringExpression(
            required=1, description="The sub-string to be replaced."),
    }

    def instantiate(self, context, processor):
        context.processorNss = self.namespaces
        context.currentInstruction = self

        if self._string:
            value = self._string.evaluate(context)
        else:
            value = context.node

        string_ = Conversions.StringValue(value)
        substring = Conversions.StringValue(self._substring.evaluate(context))
        writer = processor.writer
        for chunk in string_.split(substring):
            writer.text(chunk)
            for child in self.children:
                child.instantiate(context, processor)

        return
Ejemplo n.º 2
0
class ValueOfElement(XsltElement):
    category = CategoryTypes.INSTRUCTION
    content = ContentInfo.Empty
    legalAttrs = {
        'select': AttributeInfo.StringExpression(required=1),
        'disable-output-escaping': AttributeInfo.YesNo(default='no'),
    }

    def instantiate(self, context, processor):
        context.processorNss = self.namespaces
        context.currentInstruction = self

        text = Conversions.StringValue(self._select.evaluate(context))
        if text:
            if self._disable_output_escaping:
                processor.writers[-1].text(text, escapeOutput=False)
            else:
                processor.writers[-1].text(text)

        return
Ejemplo n.º 3
0
class SortElement(XsltElement):
    category = None
    content = ContentInfo.Empty
    legalAttrs = {
        'select' : AttributeInfo.StringExpression(default='.'),
        'lang' : AttributeInfo.NMTokenAvt(),
        # We don't support any additional data-types, hence no
        # AttributeInfo.QNameButNotNCName()
        'data-type' : AttributeInfo.ChoiceAvt(['text', 'number'],
                                              default='text'),
        'order' : AttributeInfo.ChoiceAvt(['ascending', 'descending'],
                                          default='ascending'),
        'case-order' : AttributeInfo.ChoiceAvt(['upper-first', 'lower-first']),
        }

    doesSetup = 1

    def setup(self):
        # optimize for constant AVT attribute values (i.e., no {})
        if self._data_type.isConstant() and self._order.isConstant() and \
           self._case_order.isConstant():
            self._comparer = self.makeComparer(self._order.evaluate(None),
                                               self._data_type.evaluate(None),
                                               self._case_order.evaluate(None),
                                               )
        else:
            self._comparer = None
        return

    def makeComparer(self, order, data_type, case_order):
        #if order not in ['ascending', 'descending']:
        #    raise XsltException(Error.ILLEGAL_SORT_ORDER_VALUE)
        #if data_type not in ['text', 'number']:
        #    raise XsltException(Error.ILLEGAL_SORT_DATA_TYPE_VALUE)
        #if case_order and case_order not in ['upper-first', 'lower-first']:
        #    raise XsltException(Error.ILLEGAL_SORT_CASE_ORDER_VALUE)

        if data_type == 'number':
            comparer = FloatCompare
        else:
            if case_order == 'lower-first':
                comparer = LowerFirstCompare
            elif case_order == 'upper-first':
                comparer = UpperFirstCompare
            else:
                # use default for this locale
                comparer = cmp

        if order == 'descending':
            comparer = Descending(comparer)

        return comparer

    def getComparer(self, context):
        if self._comparer: return self._comparer

        data_type = self._data_type.evaluate(context)
        order = self._order.evaluate(context)
        case_order = self._case_order and self._case_order.evaluate(context)
        return self.makeComparer(order, data_type, case_order)

    def evaluate(self, context):
        return Conversions.StringValue(self._select.evaluate(context))