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