1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
########################################################################
#
# File Name: __init__.py
#
#
"""
WWW: http://4suite.org/4XPath e-mail: support@4suite.org
Copyright (c) 2000-2001 Fourthought Inc, USA. All Rights Reserved.
See http://4suite.org/COPYRIGHT for license and copyright information
"""
NAMESPACE_NODE = 10000
FT_OLD_EXT_NAMESPACE = 'http://xmlns.4suite.org/xpath/extensions'
FT_EXT_NAMESPACE = 'http://xmlns.4suite.org/ext'
# Simple trick (thanks Tim Peters) to enable crippled IEEE 754 support
# until ANSI C (or Python) sorts it all out...
Inf = Inf = 1e300 * 1e300
NaN = Inf - Inf
from xml.dom import Node
from xml.FtCore import FtException
g_xpathRecognizedNodes = [
Node.ELEMENT_NODE,
Node.ATTRIBUTE_NODE,
Node.TEXT_NODE,
Node.CDATA_SECTION_NODE,
Node.DOCUMENT_NODE,
Node.PROCESSING_INSTRUCTION_NODE,
Node.COMMENT_NODE
]
g_extFunctions = {}
class CompiletimeException(FtException):
INTERNAL = 1
SYNTAX = 2
PROCESSING = 3
def __init__(self, errorCode, *args):
FtException.__init__(self, errorCode, MessageSource.COMPILETIME, args)
class RuntimeException(FtException):
INTERNAL = 1
NO_CONTEXT = 10
UNDEFINED_VARIABLE = 100
UNDEFINED_PREFIX = 101
WRONG_ARGUMENTS = 200
def __init__(self, errorCode, *args):
FtException.__init__(self, errorCode, MessageSource.RUNTIME, args)
from XPathParserBase import SyntaxException
import MessageSource
def Evaluate(expr, contextNode=None, context=None):
import os
if os.environ.has_key('EXTMODULES'):
RegisterExtensionModules(os.environ["EXTMODULES"].split(':'))
if context:
con = context
elif contextNode:
con = Context.Context(contextNode, 0, 0)
else:
raise RuntimeException(RuntimeException.NO_CONTEXT_ERROR)
retval = parser.new().parse(expr).evaluate(con)
return retval
def Compile(expr):
try:
return parser.new().parse(expr)
except SyntaxError, error:
raise CompiletimeException(CompiletimeException.SYNTAX, str(error))
except:
import traceback, cStringIO
stream = cStringIO.StringIO()
traceback.print_exc(None, stream)
raise RuntimeException(RuntimeException.INTERNAL, stream.getvalue())
def CreateContext(contextNode):
return Context.Context(contextNode, 0, 0)
def RegisterExtensionModules(moduleNames):
mod_names = moduleNames[:]
mods = []
for mod_name in mod_names:
if mod_name:
mod = __import__(mod_name,{},{},['ExtFunctions'])
if hasattr(mod,'ExtFunctions'):
g_extFunctions.update(mod.ExtFunctions)
mods.append(mod)
return mods
#Allow access to the NormalizeNode function
from Util import NormalizeNode
import Context
try:
import XPathParserc
except ImportError:
#import XPathParser
#parser = XPathParser
from pyxpath import ExprParserFactory
parser = ExprParserFactory
else:
parser = XPathParserc
def Init():
from xml.xpath import BuiltInExtFunctions
g_extFunctions.update(BuiltInExtFunctions.ExtFunctions)
Init()
|