Functions.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Xml » Xslt » Exslt » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » XML » 4Suite 
4Suite » 4Suite XML 1.0.2 » Ft » Xml » Xslt » Exslt » Functions.py
"""
EXSLT 2.0 - Functions (http://www.exslt.org/func/index.html)
WWW: http://4suite.org/XSLT        e-mail: support@4suite.org

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

from Ft.Xml import XPath
from Ft.Xml.Xslt import XSL_NAMESPACE,XsltElement
from Ft.Xml.Xslt import XsltRuntimeException
from Ft.Xml.Xslt import ContentInfo,AttributeInfo
from Ft.Xml.Xslt.Exslt.MessageSource import Error
from Ft.Xml.Xslt.XPathExtensions import RtfExpr

EXSL_FUNCTIONS_NS = 'http://exslt.org/functions'

class FunctionElement(XsltElement):

    content = ContentInfo.Seq(
        ContentInfo.Rep(ContentInfo.QName(XSL_NAMESPACE, 'xsl:param')),
        ContentInfo.Template,
        )

    legalAttrs = {
        'name' : AttributeInfo.QNameButNotNCName(required=1),
        }

    doesPrime = True

    def prime(self, processor, context):
        context.addFunction(self._name, self)
        return

    def __call__(self, context, *args):
        processor = context.processor

        # Save context state as XPath is side-effect free
        ctx_state = context.copy()
        ctx_namespaces = context.processorNss
        ctx_instruction = context.currentInstruction

        context.processorNss = self.namespaces
        context.currentInstruction = self

        # Set the parameter list
        counter = 0
        self.result = u''
        for child in self.children:
            if child.expandedName == (XSL_NAMESPACE, 'param'):
                if counter < len(args):
                    context.varBindings[child._name] = args[counter]
                else:
                    # default
                    child.instantiate(context, processor)
                counter = counter + 1
            else:
                child.instantiate(context, processor)

        # Restore context state
        context.currentInstruction = ctx_instruction
        context.processorNss = ctx_namespaces
        context.set(ctx_state)
        
        return self.result


class ResultElement(XsltElement):
    """
    When an func:result element is instantiated, during the
    instantiation of a func:function element, the function returns
    with its value.
    """

    content = ContentInfo.Template
    legalAttrs = {
        'select' : AttributeInfo.Expression(),
        }

    doesSetup = doesPrime = True

    def setup(self):
        if not self._select:
            self._select = RtfExpr(self.children)
        return

    def prime(self, processor, context):
        self._function = None
        current = self.parent
        while current:
            # this loop will stop when it hits the top of the tree
            if current.expandedName == (EXSL_FUNCTIONS_NS, 'function'):
                self._function = current
                break
            current = current.parent

        if not self._function:
            raise XsltRuntimeException(ExsltError.RESULT_NOT_IN_FUNCTION, self)

        if not self.isLastChild():
            siblings = self.parent.children
            for node in siblings[siblings.index(self)+1:]:
                if node.expandedName != (XSL_NAMESPACE, 'fallback'):
                    raise XsltRuntimeException(ExsltError.ILLEGAL_RESULT_SIBLINGS,
                                               self)
        return

    def instantiate(self, context, processor):
        context.processorNss = self.namespaces
        context.currentInstruction = self
        self._function.result = self._select.evaluate(context)
        return


class ScriptElement(XsltElement):
    """
    NOT YET IMPLEMENTED

    The top-level func:script element provides an implementation of
    extension functions in a particular namespace.
    """
    pass


ExtNamespaces = {
    EXSL_FUNCTIONS_NS : 'func',
    }

ExtFunctions = {}

ExtElements = {
    (EXSL_FUNCTIONS_NS, 'function'): FunctionElement,
    (EXSL_FUNCTIONS_NS, 'result'): ResultElement,
    #(EXSL_FUNCTIONS_NS, 'script'): ScriptElement,
    }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.