ParsedNodeTest.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Xml » XPath » 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 » XPath » ParsedNodeTest.py
########################################################################
# $Header: /var/local/cvsroot/4Suite/Ft/Xml/XPath/ParsedNodeTest.py,v 1.11 2005/08/02 22:43:00 mbrown Exp $
"""
A parsed token that represents a node test.

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

from xml.dom import Node

from Ft.Xml.XPath import RuntimeException
from Ft.Xml.XPath.XPathTypes import g_xpathRecognizedNodes


def ParsedNameTest(name):
    if name == '*':
        return PrincipalTypeTest()
    index = name.find(':')
    if index == -1:
        return LocalNameTest(name)
    elif name[index:] == ':*':
        return NamespaceTest(name[:index])
    return QualifiedNameTest(name[:index], name[index+1:])


def ParsedNodeTest(test, literal=None):
    if literal:
        if test != 'processing-instruction':
            raise SyntaxError('Literal only allowed in processing-instruction')
        return ProcessingInstructionNodeTest(literal)
    return g_classMap[test]()


class NodeTestBase:

    priority = -0.5
    nodeType = None

    def getQuickKey(self, namespaces):
        """
        Returns a tuple that indicates the expected node type and, if
        applicable, the expected name.
        """
        return (self.nodeType, None)

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        """
        The principalType is discussed in section [2.3 Node Tests]
        of the XPath 1.0 spec.  Only attribute and namespace axes
        differ from the default of elements.
        """
        return node.nodeType == self.nodeType

    def pprint(self, indent):
        print indent + str(self)

    def __str__(self):
        return '<%s at %x: %s>' % (
            self.__class__.__name__,
            id(self),
            repr(self),
            )


class NodeTest(NodeTestBase):

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        return node.nodeType in g_xpathRecognizedNodes

    def __repr__(self):
        return 'node()'


class CommentNodeTest(NodeTestBase):

    nodeType = Node.COMMENT_NODE

    def __repr__(self):
        return 'comment()'


class TextNodeTest(NodeTestBase):

    nodeType = Node.TEXT_NODE

    def __repr__(self):
        return 'text()'


class ProcessingInstructionNodeTest(NodeTestBase):

    nodeType = Node.PROCESSING_INSTRUCTION_NODE

    def __init__(self, target=None):
        if target:
            self.priority = 0
            if target[0] not in ['"', "'"]:
                raise SyntaxError("Invalid literal: %s" % target)
            self.target = target[1:-1]
        else:
            self.priority = -0.5
            self.target = ''

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        if node.nodeType != self.nodeType:
            return 0
        if self.target:
            return node.target == self.target
        return 1

    def __repr__(self):
        if self.target:
            target = repr(self.target)
        else:
            target = ''
        return 'processing-instruction(%s)' % target

# Name tests

class PrincipalTypeTest(NodeTestBase):

    nodeType = Node.ELEMENT_NODE

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        return node.nodeType == principalType

    def __repr__(self):
        return '*'

class LocalNameTest(NodeTestBase):

    nodeType = Node.ELEMENT_NODE

    def __init__(self, name):
        self.priority = 0
        self._name = name

    def getQuickKey(self, namespaces):
        return (self.nodeType, (None, self._name))

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        # NameTests do not use the default namespace, just as attributes
        if node.nodeType == principalType and not node.namespaceURI:
            return node.localName == self._name
        return 0

    def __repr__(self):
        return self._name

class NamespaceTest(NodeTestBase):

    nodeType = Node.ELEMENT_NODE

    def __init__(self, prefix):
        self.priority = -0.25
        self._prefix = prefix

    def getQuickKey(self, namespaces):
        # By specifing a name of None, this test will fall into the 'general'
        # category for the principal type
        return (self.nodeType, None)

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        if node.nodeType != principalType:
            return 0
        try:
            return node.namespaceURI == context.processorNss[self._prefix]
        except KeyError:
            raise RuntimeException(RuntimeException.UNDEFINED_PREFIX,
                                   self._prefix)

    def __repr__(self):
        return self._prefix + ':*'


class QualifiedNameTest(NodeTestBase):

    nodeType = Node.ELEMENT_NODE

    def __init__(self, prefix, localName):
        self.priority = 0
        self._prefix = prefix
        self._localName = localName

    def getQuickKey(self, namespaces):
        try:
            namespace = namespaces[self._prefix]
        except KeyError:
            raise RuntimeException(RuntimeException.UNDEFINED_PREFIX,
                                   self._prefix)
        return (self.nodeType, (namespace, self._localName))

    def match(self, context, node, principalType=Node.ELEMENT_NODE):
        if node.nodeType == principalType:
            if node.localName == self._localName:
                try:
                    return node.namespaceURI == context.processorNss[self._prefix]
                except KeyError:
                    raise RuntimeException(RuntimeException.UNDEFINED_PREFIX,
                                           self._prefix)
        return 0

    def __repr__(self):
        return self._prefix + ':' + self._localName

g_classMap = {
    'node' : NodeTest,
    'comment' : CommentNodeTest,
    'text' : TextNodeTest,
    'processing-instruction' : ProcessingInstructionNodeTest,
    }

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.