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