summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/matcher
diff options
context:
space:
mode:
authorNathan Fritz <fritzy@netflint.net>2009-06-03 22:56:51 +0000
committerNathan Fritz <fritzy@netflint.net>2009-06-03 22:56:51 +0000
commit96b103b27599e5af247c1e3b95d62c80c1e32a63 (patch)
tree0527b1607b16adb020759ee9a944e1b22e3e0e6b /sleekxmpp/xmlstream/matcher
downloadslixmpp-96b103b27599e5af247c1e3b95d62c80c1e32a63.tar.gz
slixmpp-96b103b27599e5af247c1e3b95d62c80c1e32a63.tar.bz2
slixmpp-96b103b27599e5af247c1e3b95d62c80c1e32a63.tar.xz
slixmpp-96b103b27599e5af247c1e3b95d62c80c1e32a63.zip
moved seesmic branch to trunk
Diffstat (limited to 'sleekxmpp/xmlstream/matcher')
-rw-r--r--sleekxmpp/xmlstream/matcher/__init__.py0
-rw-r--r--sleekxmpp/xmlstream/matcher/base.py8
-rw-r--r--sleekxmpp/xmlstream/matcher/many.py10
-rw-r--r--sleekxmpp/xmlstream/matcher/xmlmask.py43
-rw-r--r--sleekxmpp/xmlstream/matcher/xpath.py11
5 files changed, 72 insertions, 0 deletions
diff --git a/sleekxmpp/xmlstream/matcher/__init__.py b/sleekxmpp/xmlstream/matcher/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/sleekxmpp/xmlstream/matcher/__init__.py
diff --git a/sleekxmpp/xmlstream/matcher/base.py b/sleekxmpp/xmlstream/matcher/base.py
new file mode 100644
index 00000000..97e4465c
--- /dev/null
+++ b/sleekxmpp/xmlstream/matcher/base.py
@@ -0,0 +1,8 @@
+
+class MatcherBase(object):
+
+ def __init__(self, criteria):
+ self._criteria = criteria
+
+ def match(self, xml):
+ return False
diff --git a/sleekxmpp/xmlstream/matcher/many.py b/sleekxmpp/xmlstream/matcher/many.py
new file mode 100644
index 00000000..42e92b28
--- /dev/null
+++ b/sleekxmpp/xmlstream/matcher/many.py
@@ -0,0 +1,10 @@
+from . import base
+from xml.etree import cElementTree
+
+class MatchMany(base.MatcherBase):
+
+ def match(self, xml):
+ for m in self._criteria:
+ if m.match(xml):
+ return True
+ return False
diff --git a/sleekxmpp/xmlstream/matcher/xmlmask.py b/sleekxmpp/xmlstream/matcher/xmlmask.py
new file mode 100644
index 00000000..02a644cb
--- /dev/null
+++ b/sleekxmpp/xmlstream/matcher/xmlmask.py
@@ -0,0 +1,43 @@
+from . import base
+from xml.etree import cElementTree
+from xml.parsers.expat import ExpatError
+
+class MatchXMLMask(base.MatcherBase):
+
+ def __init__(self, criteria):
+ base.MatcherBase.__init__(self, criteria)
+ if type(criteria) == type(''):
+ self._criteria = cElementTree.fromstring(self._criteria)
+ self.default_ns = 'jabber:client'
+
+ def setDefaultNS(self, ns):
+ self.default_ns = ns
+
+ def match(self, xml):
+ return self.maskcmp(xml, self._criteria, True)
+
+ def maskcmp(self, source, maskobj, use_ns=False, default_ns='__no_ns__'):
+ """maskcmp(xmlobj, maskobj):
+ Compare etree xml object to etree xml object mask"""
+ #TODO require namespaces
+ if source == None: #if element not found (happens during recursive check below)
+ return False
+ if type(maskobj) == type(str()): #if the mask is a string, make it an xml obj
+ try:
+ maskobj = cElementTree.fromstring(maskobj)
+ except ExpatError:
+ logging.log(logging.WARNING, "Expat error: %s\nIn parsing: %s" % ('', maskobj))
+ if not use_ns and source.tag.split('}', 1)[-1] != maskobj.tag.split('}', 1)[-1]: # strip off ns and compare
+ return False
+ if use_ns and (source.tag != maskobj.tag and "{%s}%s" % (self.default_ns, maskobj.tag) != source.tag ):
+ return False
+ if maskobj.text and source.text != maskobj.text:
+ return False
+ for attr_name in maskobj.attrib: #compare attributes
+ if source.attrib.get(attr_name, "__None__") != maskobj.attrib[attr_name]:
+ return False
+ #for subelement in maskobj.getiterator()[1:]: #recursively compare subelements
+ for subelement in maskobj: #recursively compare subelements
+ if not self.maskcmp(source.find(subelement.tag), subelement, use_ns):
+ return False
+ return True
diff --git a/sleekxmpp/xmlstream/matcher/xpath.py b/sleekxmpp/xmlstream/matcher/xpath.py
new file mode 100644
index 00000000..b141dd87
--- /dev/null
+++ b/sleekxmpp/xmlstream/matcher/xpath.py
@@ -0,0 +1,11 @@
+from . import base
+from xml.etree import cElementTree
+
+class MatchXPath(base.MatcherBase):
+
+ def match(self, xml):
+ x = cElementTree.Element('x')
+ x.append(xml)
+ if x.find(self._criteria) is not None:
+ return True
+ return False