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