Optimize the rule processing function to parse only once, then manipulate the parsed...
[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     final_processor = '__sfatables_wrap_up__.xsl'
11
12     arguments = {'match':None,'target':None}
13     processors = {'match':None,'target':None}
14     context = {'match':None,'target':None}
15
16     def apply_processor(self, type, doc, output_xpath_filter=None):
17         processor = self.processors[type]
18
19         # XXX TO CLEAN UP
20         filepath = os.path.join(sfatables_config, 'processors', processor)
21         # XXX
22
23         styledoc = libxml2.parseFile(filepath)
24         style = libxslt.parseStylesheetDoc(styledoc)
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
33             p.xpathFreeContext()
34         else:
35             stylesheet_result = result #style.saveResultToString(result)
36
37         style.freeStylesheet()
38         #doc.freeDoc()
39         #result.freeDoc()
40
41         return stylesheet_result
42
43     def wrap_up(self, doc):
44         filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
45
46         if not os.path.exists(filepath):
47             # TODO: final_processor is not there yet
48             return doc#rspec
49
50         styledoc = libxml2.parseFile(filepath)
51         style = libxslt.parseStylesheetDoc(styledoc)
52         #doc = libxml2.parseDoc(rspec)
53         result = style.applyStylesheet(doc, None)
54         stylesheet_result = result #style.saveResultToString(result)
55         style.freeStylesheet()
56         #doc.freeDoc()
57         #result.freeDoc()
58
59         return stylesheet_result
60
61     def match(self, rspec):
62         match_result = self.apply_processor('match',rspec,"//result/@verdict") 
63         return (match_result[0].content=='True')
64
65     def target(self, rspec):
66         target_result = self.apply_processor('target',rspec,None)
67         return target_result
68
69     def apply_interpreted(self, rspec):
70         # Interpreted
71         #
72         # output =
73         #    if (match(match_args, rspec)
74         #       then target(target_args, rspec)
75         #       else rspec
76
77         if (self.match(rspec)):
78             return self.target(rspec)
79         else:
80             return rspec
81
82
83     def apply_compiled(rspec):
84         # Not supported yet
85         return None
86
87     def load_xml_extension (self, type, chain, rule_number):
88         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
89
90         self.xmldoc = libxml2.parseFile(filename)
91         p = self.xmldoc.xpathNewContext()
92
93         context = p.xpathEval('//context/@select')
94         self.context[type] = context[0].content
95
96         processor = p.xpathEval('//processor/@filename')
97
98         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
99         if (context != []):
100             self.terminal = 1
101         
102         self.processors[type] = processor[0].content
103         self.arguments[type] = p.xpathEval('//rule')
104
105         p.xpathFreeContext()
106
107
108     def wrap_rspec (self, type, rspec):
109         argument = self.arguments[type]
110         p = rspec.xmldoc.xpathNewContext()
111         root_node = p.xpathEval('/RSpec')
112         if (not root_node or not root_node):
113             raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
114
115         root_node.addChild(arguments[type])
116         return rspec
117
118     def __init__(self, chain=None, rule_number=None):
119         if (chain and rule_number):
120             self.load_xml_extension('match', chain, rule_number)
121             self.load_xml_extension('target',chain, rule_number)
122             self.rule_number = rule_number
123             self.chain = chain
124         return
125         
126     def free(self):
127         self.xmldoc.freeDoc()