Example #1
0
 def _initializeSheet(self, rootNode):
     if rootNode.namespaceURI == XSL_NAMESPACE:
         if rootNode.localName in ['stylesheet', 'transform']:
             if not rootNode.getAttributeNS(EMPTY_NAMESPACE, 'version'):
                 raise XsltException(Error.STYLESHEET_MISSING_VERSION)
             #rootNode.__dict__['extensionNss'] = []
         else:
             raise XsltException(Error.STYLESHEET_ILLEGAL_ROOT,
                                 rootNode.nodeName)
     else:
         vattr = rootNode.getAttributeNodeNS(XSL_NAMESPACE, 'version')
         if not vattr:
             root_nss = GetAllNs(rootNode)
             if filter(lambda x, n=root_nss: n[x] == XSL_NAMESPACE,
                       root_nss.keys()):
                 raise XsltException(Error.STYLESHEET_MISSING_VERSION)
             else:
                 raise XsltException(Error.STYLESHEET_MISSING_VERSION_NOTE1)
         sheet = StylesheetElement(self._ownerDoc, XSL_NAMESPACE,
                                   'transform', vattr.prefix,
                                   self._ssheetUri)
         sheet.setAttributeNS(EMPTY_NAMESPACE, 'version', vattr.value)
         tpl = TemplateElement(self._ownerDoc, XSL_NAMESPACE, 'template',
                               vattr.prefix, self._ssheetUri)
         tpl.setAttributeNS(EMPTY_NAMESPACE, 'match', '/')
         sheet.appendChild(tpl)
         sheet.__dict__['extensionNss'] = []
         self._nodeStack[-1].appendChild(sheet)
         # Ensure the literal element is a child of the template
         # endElement appends to the end of the nodeStack
         self._nodeStack.append(tpl)
     self._firstElement = 0
     return
Example #2
0
 def _initializeSheet(self, rootNode):
     if rootNode.namespaceURI == XSL_NAMESPACE:
         if rootNode.localName in ['stylesheet', 'transform']:
             if not rootNode.getAttributeNS(EMPTY_NAMESPACE, 'version'):
                 raise XsltException(Error.STYLESHEET_MISSING_VERSION)
             #rootNode.__dict__['extensionNss'] = []
         else:
             raise XsltException(Error.STYLESHEET_ILLEGAL_ROOT, rootNode.nodeName)
     else:
         vattr = rootNode.getAttributeNodeNS(XSL_NAMESPACE, 'version')
         if not vattr:
             root_nss = GetAllNs(rootNode)
             if filter(lambda x, n=root_nss: n[x] == XSL_NAMESPACE, root_nss.keys()):
                 raise XsltException(Error.STYLESHEET_MISSING_VERSION)
             else:
                 raise XsltException(Error.STYLESHEET_MISSING_VERSION_NOTE1)
         sheet = StylesheetElement(self._ownerDoc, XSL_NAMESPACE,
                                   'transform', vattr.prefix,
                                   self._ssheetUri)
         sheet.setAttributeNS(EMPTY_NAMESPACE, 'version', vattr.value)
         tpl = TemplateElement(self._ownerDoc, XSL_NAMESPACE, 'template',
                               vattr.prefix, self._ssheetUri)
         tpl.setAttributeNS(EMPTY_NAMESPACE, 'match', '/')
         sheet.appendChild(tpl)
         sheet.__dict__['extensionNss'] = []
         self._nodeStack[-1].appendChild(sheet)
         # Ensure the literal element is a child of the template
         # endElement appends to the end of the nodeStack
         self._nodeStack.append(tpl)
     self._firstElement = 0
     return
Example #3
0
def FromDocument(oldDoc, baseUri='',stylesheetReader = None):
    #FIXME: We really shouldn't mutate the given doc, but this is the easiest way to strip whitespace
    if baseUri and baseUri[-1] == '/':
        modBaseUri = baseUri
    else:
        modBaseUri = baseUri + '/'
    oldDoc.normalize()
    extElements = xslt.g_extElements
    source_root = oldDoc.documentElement
    #Set up a new document for the stylesheet nodes
    if source_root.namespaceURI == XSL_NAMESPACE:
        if source_root.localName not in ['stylesheet', 'transform']:
            raise XsltException(Error.STYLESHEET_ILLEGAL_ROOT, source_root.nodeName)
        result_elem_root = 0
    else:
        result_elem_root = 1
    xsl_doc = createDocument()
    ext_uris = []
    if result_elem_root:
        vattr = source_root.getAttributeNodeNS(XSL_NAMESPACE, 'version')
        if not vattr:
            root_nss = GetAllNs(source_root)
            if filter(lambda x, n=root_nss: n[x] == XSL_NAMESPACE, root_nss.keys()):
                raise XsltException(Error.STYLESHEET_MISSING_VERSION)
            else:
                raise XsltException(Error.STYLESHEET_MISSING_VERSION_NOTE1)

        sheet = StylesheetElement(xsl_doc, XSL_NAMESPACE,
                                  'transform', vattr.prefix,
                                  baseUri)
        sheet.setAttributeNS(EMPTY_NAMESPACE, 'version', vattr.value)
        tpl = TemplateElement(xsl_doc, XSL_NAMESPACE, 'template',
                              vattr.prefix, baseUri)

        tpl.setAttributeNS(EMPTY_NAMESPACE, 'match', '/')
        sheet.appendChild(tpl)
        sheet.__dict__['extensionNss'] = []
        xsl_doc.appendChild(sheet)
        DomConvert(source_root, tpl, xsl_doc, [], extElements, 0)
    else:
        sheet = StylesheetElement(xsl_doc, source_root.prefix, source_root.localName, baseUri=baseUri)
        sty_nss = GetAllNs(source_root)
        for attr in source_root.attributes.values():
            if (attr.namespaceURI, attr.localName) == ('', 'extension-element-prefixes'):
                ext_prefixes = string.splitfields(attr.value)
                for prefix in ext_prefixes:
                    if prefix == '#default': prefix = ''
                    ext_uris.append(sty_nss[prefix])
            sheet.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.value)
        sheet.__dict__['extensionNss'] = ext_uris
        if not sheet.getAttributeNS(EMPTY_NAMESPACE, 'version'):
            raise XsltException(Error.STYLESHEET_MISSING_VERSION)
        xsl_doc.appendChild(sheet)
        for child in source_root.childNodes:
            DomConvert(child, sheet, xsl_doc, ext_uris, extElements, 0)
    #Handle includes
    includes = filter(lambda x: x.nodeType == Node.ELEMENT_NODE and (x.namespaceURI, x.localName) == (XSL_NAMESPACE, 'include'), sheet.childNodes)
    for inc in includes:
        href = inc.getAttributeNS(EMPTY_NAMESPACE,'href')
        if stylesheetReader is None:
            stylesheetReader = StylesheetReader()
        docfrag = stylesheetReader.fromUri(href,baseUri = baseUri, ownerDoc=xsl_doc)
        sty = docfrag.firstChild
        included_nss = GetAllNs(sty)
        for child in sty.childNodes[:]:
            if child.nodeType != Node.ELEMENT_NODE:
                continue
            sheet.insertBefore(child, inc)
            #migrate old nss from stylesheet directly to new child
            for prefix in included_nss.keys():
                if prefix:
                    child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:'+prefix,
                                                 included_nss[prefix])
                else:
                    child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns',
                                         included_nss[prefix])

        sheet.removeChild(inc)
        ReleaseNode(inc)
        #sty.reclaim()
    try:
        sheet.setup()
    except:
        ReleaseNode(sheet.ownerDocument)
        raise
    return sheet
Example #4
0
def FromDocument(oldDoc, baseUri='', stylesheetReader=None):
    #FIXME: We really shouldn't mutate the given doc, but this is the easiest way to strip whitespace
    if baseUri and baseUri[-1] == '/':
        modBaseUri = baseUri
    else:
        modBaseUri = baseUri + '/'
    oldDoc.normalize()
    extElements = xslt.g_extElements
    source_root = oldDoc.documentElement
    #Set up a new document for the stylesheet nodes
    if source_root.namespaceURI == XSL_NAMESPACE:
        if source_root.localName not in ['stylesheet', 'transform']:
            raise XsltException(Error.STYLESHEET_ILLEGAL_ROOT,
                                source_root.nodeName)
        result_elem_root = 0
    else:
        result_elem_root = 1
    xsl_doc = createDocument()
    ext_uris = []
    if result_elem_root:
        vattr = source_root.getAttributeNodeNS(XSL_NAMESPACE, 'version')
        if not vattr:
            root_nss = GetAllNs(source_root)
            if filter(lambda x, n=root_nss: n[x] == XSL_NAMESPACE,
                      root_nss.keys()):
                raise XsltException(Error.STYLESHEET_MISSING_VERSION)
            else:
                raise XsltException(Error.STYLESHEET_MISSING_VERSION_NOTE1)

        sheet = StylesheetElement(xsl_doc, XSL_NAMESPACE, 'transform',
                                  vattr.prefix, baseUri)
        sheet.setAttributeNS(EMPTY_NAMESPACE, 'version', vattr.value)
        tpl = TemplateElement(xsl_doc, XSL_NAMESPACE, 'template', vattr.prefix,
                              baseUri)

        tpl.setAttributeNS(EMPTY_NAMESPACE, 'match', '/')
        sheet.appendChild(tpl)
        sheet.__dict__['extensionNss'] = []
        xsl_doc.appendChild(sheet)
        DomConvert(source_root, tpl, xsl_doc, [], extElements, 0)
    else:
        sheet = StylesheetElement(xsl_doc,
                                  source_root.prefix,
                                  source_root.localName,
                                  baseUri=baseUri)
        sty_nss = GetAllNs(source_root)
        for attr in source_root.attributes.values():
            if (attr.namespaceURI,
                    attr.localName) == ('', 'extension-element-prefixes'):
                ext_prefixes = string.splitfields(attr.value)
                for prefix in ext_prefixes:
                    if prefix == '#default': prefix = ''
                    ext_uris.append(sty_nss[prefix])
            sheet.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.value)
        sheet.__dict__['extensionNss'] = ext_uris
        if not sheet.getAttributeNS(EMPTY_NAMESPACE, 'version'):
            raise XsltException(Error.STYLESHEET_MISSING_VERSION)
        xsl_doc.appendChild(sheet)
        for child in source_root.childNodes:
            DomConvert(child, sheet, xsl_doc, ext_uris, extElements, 0)
    #Handle includes
    includes = filter(
        lambda x: x.nodeType == Node.ELEMENT_NODE and
        (x.namespaceURI, x.localName) == (XSL_NAMESPACE, 'include'),
        sheet.childNodes)
    for inc in includes:
        href = inc.getAttributeNS(EMPTY_NAMESPACE, 'href')
        if stylesheetReader is None:
            stylesheetReader = StylesheetReader()
        docfrag = stylesheetReader.fromUri(href,
                                           baseUri=baseUri,
                                           ownerDoc=xsl_doc)
        sty = docfrag.firstChild
        included_nss = GetAllNs(sty)
        for child in sty.childNodes[:]:
            if child.nodeType != Node.ELEMENT_NODE:
                continue
            sheet.insertBefore(child, inc)
            #migrate old nss from stylesheet directly to new child
            for prefix in included_nss.keys():
                if prefix:
                    child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:' + prefix,
                                         included_nss[prefix])
                else:
                    child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns',
                                         included_nss[prefix])

        sheet.removeChild(inc)
        ReleaseNode(inc)
        #sty.reclaim()
    try:
        sheet.setup()
    except:
        ReleaseNode(sheet.ownerDocument)
        raise
    return sheet