__init__.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Xml » Lib » 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 » Lib » __init__.py
########################################################################
# $Header: /var/local/cvsroot/4Suite/Ft/Xml/Lib/__init__.py,v 1.8 2006/01/06 00:41:57 jclark Exp $
"""
Module providing XML support utilities (including serialization and tree comparison)

Copyright 2004 Fourthought, Inc. (USA).
Detailed license and copyright information: http://4suite.org/COPYRIGHT
Project home, documentation, distributions: http://4suite.org/
"""

import os
from xml.dom import Node

from Ft.Xml import XML_NAMESPACE
from Ft.Lib import Uri

__all__ = ['Language', 'BaseUri']


def Language(node):
    """
    This function returns the language property of the given instance of
    xml.dom.Node, based on xml:lang attributes present on the node or
    its ancestors. If no xml:lang attribute is present, returns None.

    The language code, if any, is returned as an uppercase string.
    """
    while node:
        if node.nodeType == Node.DOCUMENT_NODE:
            break
        elif hasattr(node, 'hasAttributeNS') and node.hasAttributeNS(XML_NAMESPACE, 'lang'):
            # found an xml:lang
            value = node.getAttributeNS(XML_NAMESPACE, 'lang')
            if value:
                #Remove suffix if there is one (why?)
                #index = value.find('-')
                #if index != -1:
                #    value = value[:index]
                value = value.upper()
            # u'' -> None because XML 1.0 3rd Edition says xml:lang=""
            # undefines the language. RDF/XML Syntax (Revised) agrees.
            return value or None
        else:
            # go on to next ancestor
            node = node.nodeType == Node.ATTRIBUTE_NODE and \
                node.ownerElement or node.parentNode

    # reached a Document node without finding an xml:lang
    return None


def BaseUri(node, fallback=None):
    """
    `BaseUri` is an implementation of the `node.baseURI` attribute that
    should be attached to DOM Level 3 nodes, but which is currently broken
    in 4Suite XML.  Where you would use `node.baseURI` according to DOM
    Level 3, use `BaseUri(node)` (this function) instead.

    `BaseUri` returns the absolute base URI for a given `node` in a Domlette
    tree, or `None` if no such *absolute* base URI exists.

    If `fallback` is specified, then it is used to construct a base URI when
    everything else fails.
    """

    baseUriPart = ''
    xmlBaseAncestorList = node.xpath('ancestor-or-self::*[@xml:base][1]')
    if len(xmlBaseAncestorList) > 0:
        node = xmlBaseAncestorList[0]
        baseUriPart = node.getAttributeNS(XML_NAMESPACE, 'base')

    # If the xml:base in scope for the current node is not absolute, we find
    # the element where that xml:base was declared, then Absolutize our
    # relative xml:base against the base URI of the parent of declaring
    # element, recursively.
    if (not Uri.IsAbsolute(baseUriPart) and node.parentNode is not None):
        baseUriPart = Uri.Absolutize(baseUriPart,
                                     BaseUri(node.parentNode))

    # If we still don't have an absolute base URI, resolve against the
    # document's URI.
    if not Uri.IsAbsolute(baseUriPart):
        if hasattr(node, 'createElementNS'):
          baseUriPart = Uri.Absolutize(baseUriPart,
                                       node.documentURI)
        else:
          baseUriPart = Uri.Absolutize(baseUriPart,
                                       node.ownerDocument.documentURI)
    
    # Next, we try resolving against the fallback base URI, if one has been
    # provided.
    if not Uri.IsAbsolute(baseUriPart) and fallback is not None:
        baseUriPart = Uri.Absolutize(baseUriPart, fallback)

    # And if we *still* don't have an absolute base URI, well, there's not
    # much more we can do.  No biscuit.  Do we want to generate one if we
    # get to this case, instead of returning `None`?
    if not Uri.IsAbsolute(baseUriPart):
        return None
    else:
        return baseUriPart
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.