Added a "negate result" option
[sfa.git] / sfatables / xmlrule.py
1 import libxml2
2 import libxslt
3 from sfatables.globals import *
4
5 class XMLRule:
6     rule_number = None
7     chain = None
8     xmldoc = None
9     terminal = 0
10
11     arguments = {'match':None,'target':None}
12     processors = {'match':None,'target':None}
13     context = {'match':None,'target':None}
14
15     def apply_processor(self, type, rspec, output_xpath_filter=None):
16         processor = self.processors[type]
17
18         # XXX TO CLEAN UP
19         filepath = 'processors/' + processor
20         # XXX
21
22         styledoc = libxml2.parseFile(filepath)
23         style = libxslt.parseStylesheetDoc(styledoc)
24         doc = libxml2.parseDoc(rspec)
25         result = style.applyStylesheet(doc, None)
26         if (output_xpath_filter):
27             p = result.xpathNewContext()
28             xpath_result = p.xpathEval(output_xpath_filter)
29             if (xpath_result == []):
30                 raise Exception("Could not apply processor %s."%processor)
31
32             stylesheet_result = xpath_result[0].content
33             p.xpathFreeContext()
34         else:
35             stylesheet_result = style.saveResultToString(result)
36         style.freeStylesheet()
37         doc.freeDoc()
38         result.freeDoc()
39
40
41         return stylesheet_result
42
43     def match(self, rspec):
44         match_result = self.apply_processor('match',rspec,"//result/@verdict") 
45         return (match_result=='True')
46
47     def target(self, rspec):
48         target_result = self.apply_processor('target',rspec,None)
49         return target_result
50
51     def apply_interpreted(self, rspec):
52         # Interpreted
53         #
54         # output =
55         #    if (match(match_args, rspec)
56         #       then target(target_args, rspec)
57         #       else rspec
58
59         if (self.match(rspec)):
60             return self.target(rspec)
61         else:
62             return rspec
63
64
65     def apply_compiled(rspec):
66         # Not supported yet
67         return None
68
69     def load_xml_extension (self, type, chain, rule_number):
70         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
71
72         self.xmldoc = libxml2.parseFile(filename)
73         p = self.xmldoc.xpathNewContext()
74
75         context = p.xpathEval('//context/@select')
76         self.context[type] = context[0].content
77
78         processor = p.xpathEval('//processor/@filename')
79
80         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
81         if (context != []):
82             self.terminal = 1
83         
84         self.processors[type] = processor[0].content
85         self.arguments[type] = p.xpathEval('//rule')
86
87         p.xpathFreeContext()
88
89
90     def wrap_rspec (self, type, rspec):
91         argument = self.arguments[type]
92         p = rspec.xmldoc.xpathNewContext()
93         root_node = p.xpathEval('/RSpec')
94         if (not root_node or not root_node):
95             raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
96
97         root_node.addChild(arguments[type])
98         return rspec
99
100     def __init__(self, chain, rule_number):
101
102         self.load_xml_extension('match', chain, rule_number)
103         self.load_xml_extension('target',chain, rule_number)
104         self.rule_number = rule_number
105         self.chain = chain
106
107         return
108         
109     def free(self):
110         self.xmldoc.freeDoc()