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