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