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