Beispiel #1
0
    def _xu_attribute(self, context, node, preserveSpace):
        name = node.getAttributeNS(EMPTY_NAMESPACE, 'name')
        if not name:
            raise XUpdateException(XUpdateException.NO_NAME)
        _name = self.parseAVT(name)

        namespace = node.getAttributeNS(EMPTY_NAMESPACE, 'namespace')
        _namespace = self.parseAVT(namespace)

        oldNss = context.processorNss
        context.processorNss = Domlette.GetAllNs(node)
        name = _name.evaluate(context)
        namespace = _namespace and _namespace.evaluate(context)

        (prefix, local) = SplitQName(name)
        if not namespace:
            if prefix:
                namespace = context.processorNss[prefix]
            else:
                namespace = EMPTY_NAMESPACE
        self.pushStringResult()
        try:
            for child in node.childNodes:
                self.visit(context, child, preserveSpace)
        finally:
            result = self.popResult()

        self.writers[-1].attribute(name, result, namespace)
        context.processorNss = oldNss
        return
Beispiel #2
0
 def attribute(self, name, value, namespace=EMPTY_NAMESPACE):
     self.__currElement.attrs[name] = value
     if namespace:
         (prefix, local) = SplitQName(name)
         if prefix:
             self.__currElement.attrs[u"xmlns:"+prefix] = namespace
     return
Beispiel #3
0
    def _xu_element(self, context, element, preserveSpace):
        name = element.getAttributeNS(EMPTY_NAMESPACE, 'name')
        if not name:
            raise XUpdateException(XUpdateException.NO_NAME)
        _name = self.parseAVT(name)

        namespace = element.getAttributeNS(EMPTY_NAMESPACE, 'namespace')
        _namespace = self.parseAVT(namespace)

        oldNss = context.processorNss
        context.processorNss = Domlette.GetAllNs(element)
        name = _name.evaluate(context)

        namespace = _namespace and _namespace.evaluate(context)

        (prefix, local) = SplitQName(name)
        if not namespace:
            if prefix:
                namespace = context.processorNss[prefix]
            else:
                namespace = EMPTY_NAMESPACE

        self.writers[-1].startElement(name, namespace)
        for child in element.childNodes:
            self.visit(context, child, preserveSpace)
        self.writers[-1].endElement(name, namespace)
        context.processorNss = oldNss
        return
 def prepare(self, element, value):
     if value is None:
         if self.default is None:
             return None
         value = self.default
     elif not IsQName(value):
         raise XsltException(Error.INVALID_QNAME_ATTR, value)
     return SplitQName(value)
Beispiel #5
0
 def startElement(self, name, namespace=EMPTY_NAMESPACE, extraNss=None):
     self._completeTextNode()
     new_element = self._ownerDoc.createElementNS(namespace, name)
     self._nodeStack.append(new_element)
     extraNss = extraNss or {}
     prefix, localName = SplitQName(name)
     for prefix in extraNss.keys():
         if prefix:
             new_element.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:' + prefix,
                                        extraNss[prefix])
         else:
             new_element.setAttributeNS(XMLNS_NAMESPACE, 'xmlns',
                                        extraNss[None] or u'')
     return
Beispiel #6
0
    def attribute(self, name, value, namespace=EMPTY_NAMESPACE):
        """
        add an attribute to an element

        name - the qualified name of the attribute
        value - the attribute value: must be Unicode
        namespace - must be Unicode or Ft.Xml.EMPTY_NAMESPACE (the default)

        Strives for "sanity".  For brilliant definition thereof, c.f. Joe English
        http://lists.xml.org/archives/xml-dev/200204/msg00170.html
        Uses terminology from that article
        See also discussions starting
        http://lists.fourthought.com/pipermail/4suite-dev/2003-March/001294.html
        http://lists.fourthought.com/pipermail/4suite-dev/2003-March/001283.html

        Note: attribute output is computed as invoked.
        This means that the ugly case

        attribute(u"foo", u"bar", "http://some-ns/")
        attribute(u"x:foo", u"baz", "http://some-ns/")

        will result in the ugly
          xmlns:org.4suite.4xslt.ns0="http://some-ns/"
          org.4suite.4xslt.ns0:foo="baz"

        The user can easily correct this by reversing the
        order of the calls
        """
        if not self._elementName:
            if self._inFirstElement:
                raise XsltException(Error.ATTRIBUTE_ADDED_TOO_LATE)
            else:
                raise XsltException(Error.ATTRIBUTE_ADDED_TO_NON_ELEMENT)
        (prefix, local) = SplitQName(name)
        if namespace != EMPTY_NAMESPACE:
            new_name = self._updateNamespace(prefix,
                                             namespace,
                                             local,
                                             forcePrefix=1)
            if new_name: name = new_name
        else:
            name = local

        self._attributes[name] = value
        return
Beispiel #7
0
def ParsedFunctionCallExpr(name, args):
    """
    Returns an object representing a function call expression
    (XPath 1.0 grammar production 16: FunctionCall)
    """
    name = XmlStrStrip(name)
    key = SplitQName(name)
    if not args:
        return FunctionCall(name, key, args)
    count = len(args)
    if count == 1:
        return FunctionCall1(name, key, args)
    elif count == 2:
        return FunctionCall2(name, key, args)
    elif count == 3:
        return FunctionCall3(name, key, args)
    else:
        return FunctionCallN(name, key, args)
    def prepare(self, element, value):
        if value is None:
            if self.default is None:
                return None
            value = self.default
        elif not IsQName(value):
            raise XsltException(Error.INVALID_QNAME_ATTR, value)

        prefix, local = SplitQName(value)
        if prefix:
            try:
                namespace = element.namespaces[prefix]
            except KeyError:
                raise XsltRuntimeException(Error.UNDEFINED_PREFIX,
                                           element, prefix)
        else:
            namespace = EMPTY_NAMESPACE
        return (namespace, local)
Beispiel #9
0
    def _xu_rename(self, context, element, preserveSpace):
        select = element.getAttributeNS(EMPTY_NAMESPACE, u'select')
        if not select:
            raise XUpdateException(XUpdateException.NO_SELECT)

        oldNss = context.processorNss
        context.processorNss = Domlette.GetAllNs(element)

        nodeset = self.evaluateExpression(context, select)
        if not nodeset:
            raise XUpdateException(XUpdateException.INVALID_SELECT,
                                   expr=select)

        new_name = XmlStrStrip(element.firstChild.data)
        (prefix, local) = SplitQName(new_name)
        if prefix:
            namespace = context.processorNss[prefix]
        else:
            namespace = EMPTY_NAMESPACE

        for refnode in nodeset:
            if refnode.nodeType == Node.ATTRIBUTE_NODE:
                parent = refnode.ownerElement
                parent.removeAttributeNode(refnode)
                parent.setAttributeNS(namespace, new_name, refnode.value)
            else:
                parent = refnode.parentNode
                if parent is None:
                    parent = refnode.ownerDocument
                new_elem = refnode.ownerDocument.createElementNS(namespace, new_name)
                parent.replaceChild(new_elem, refnode)
                # Copy any existing attributes to the newly created element
                if refnode.attributes:
                    for attr in refnode.attributes.values():
                        new_elem.setAttributeNodeNS(attr)
                # Now copy any children as well
                while refnode.firstChild:
                    new_elem.appendChild(refnode.firstChild)

        context.processorNss = oldNss
        return
Beispiel #10
0
    def startElement(self, tagName, namespace=EMPTY_NAMESPACE, extraNss=None):
        self._completeLastElement()

        if not self._inFirstElement:
            self._printer.doctype(tagName, self._outputParams.doctypePublic,
                                  self._outputParams.doctypeSystem)
            self._inFirstElement = True

        self._elementName = tagName
        self._elementUri = namespace
        (prefix, local) = SplitQName(tagName)

        # Update in-scope namespaces
        if extraNss:
            namespaces = extraNss.copy()
            namespaces.update(self._namespaces[-1])
        else:
            namespaces = self._namespaces[-1].copy()
        namespaces[prefix] = namespace
        self._namespaces.append(namespaces)
        return
def Map(context, funcname, *nodesets):
    """
    Apply the function serially over the given node sets.
    In iteration i, the function is passed N parameters
    where N is the number of argument node sets.  Each
    parameter is a node set of size 1, whose node is
    the ith node of the corresponding argument node set.
    The return value is a node set consisting of a series
    of result-tree nodes, each of which is a text node
    whose value is the string value of the result of the
    ith function invocation.
    Warning: this function uses the implied ordering of the node set
    Based on its implementation as a Python list.  But in reality
    There is no reliable ordering of XPath node sets.
    Therefore this function is not recommended for use with
    more than one node set parameter.
    """
    (prefix, local) = SplitQName(funcname)
    uri = context.processorNss.get(prefix)
    if prefix and not uri:
        raise XsltRuntimeException(Error.UNDEFINED_PREFIX,
                                   context.currentInstruction, prefix)
    expanded = (prefix and uri or '', local)
    func = context.functions.get(expanded)
    if not func:
        raise Exception('Dynamically invoked function %s not found.' %
                        funcname)

    flist = [func] * len(nodesets)
    lf = lambda x, f, *args: apply(f, args)
    retlist = apply(map, (lf, flist) + nodesets)

    processor = context.processor
    processor.pushResultTree(context.currentInstruction.baseUri)
    try:
        for ret in retlist:
            processor.writers[-1].text(Conversions.StringValue(ret))
    finally:
        rtf = processor.popResult()
    return rtf.childNodes
Beispiel #12
0
    def _xu_variable(self, context, element, preserveSpace):
        name = element.getAttributeNS(EMPTY_NAMESPACE, 'name')
        if not name:
            raise XUpdateException(XUpdateException.NO_NAME)

        select = element.getAttributeNS(EMPTY_NAMESPACE, u'select')
        if select:
            oldNss = context.processorNss
            context.processorNss = Domlette.GetAllNs(element)
            result = self.evaluateExpression(context, select)
            context.processorNss = oldNss
        else:
            result = Conversions.StringValue(element)

        (prefix, local) = SplitQName(name)
        if prefix:
            namespace = context.processorNss[prefix]
        else:
            namespace = EMPTY_NAMESPACE

        context.varBindings[(namespace, local)] = result
        return
Beispiel #13
0
def ExpandQName(qname, refNode=None, namespaces=None):
    """
    Expand the given QName in the context of the given node,
    or in the given namespace dictionary.

    Returns a 2-tuple consisting of the namespace URI and local name.
    """
    nss = {}
    if refNode:
        nss = GetAllNs(refNode)
    elif namespaces:
        nss = namespaces
    (prefix, local) = SplitQName(qname)
    #We're not to use the default namespace
    if prefix:
        try:
            split_name = (nss[prefix], local)
        except KeyError:
            from Ft.Xml.XPath import RuntimeException
            raise RuntimeException(RuntimeException.UNDEFINED_PREFIX, prefix)
    else:
        split_name = (EMPTY_NAMESPACE, local)
    return split_name
Beispiel #14
0
 def startElement(self, name, namespace=EMPTY_NAMESPACE, extraNss=None):
     extraNss = extraNss or {}
     attrs = {}
     if self.__firstElementFlag:
         if not self.__outputParams.method:
             if not namespace and name.upper() == 'HTML':
                 self.__outputParams.method = 'html'
             else:
                 self.__outputParams.method = 'xml'
         if self.__outputParams.method == 'xml':
             self.__initSax2Doc(name)
         else:
             self.__initHtmlSaxDoc(name)
         self.__firstElementFlag = False
     self.__completeTextNode()
     if self.__currElement:
         self.__saxHandler.startElement(self.__currElement.name, self.__currElement.attrs)
         self.__currElement = None
     self.__currElement = ElementData(name, attrs, extraNss)
     if self.__outputParams.method == 'xml':
         if namespace:
             (prefix, local) = SplitQName(name)
             if prefix:
                 self.__currElement.attrs["xmlns:"+prefix] = namespace
             else:
                 self.__currElement.attrs["xmlns"] = namespace
         for prefix in extraNss.keys():
             if prefix:
                 new_element.setAttributeNS(XMLNS_NAMESPACE,
                                            u'xmlns:'+prefix,
                                            extraNss[prefix])
             else:
                 new_element.setAttributeNS(XMLNS_NAMESPACE,
                                            u'xmlns',
                                            extraNss[''])
     return
Beispiel #15
0
 def __init__(self,name):
     self._name = name
     self._key = SplitQName(name[1:])
     return
Beispiel #16
0
 def startElement(self, tagName, namespace=EMPTY_NAMESPACE, extraNss=None):
     XmlWriter.startElement(self, tagName, namespace, extraNss)
     (prefix, local) = SplitQName(tagName)
     cdata = (namespace, local) in self._cdataSectionElements
     self._useCdataSection.append(cdata)
     return