Set up different namespaces for match and target arguments.
[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 add_rule_context_to_rspec(self, doc):
59         p = doc.xpathNewContext()
60         context = p.xpathEval("//rspec")
61         if (not context):
62             raise Exception('Request is not an rspec')
63         else:
64             # Add the request context
65             matchNode = libxml2.newNode('match-context')
66             matchNode.addChild(self.arguments['match'])
67             targetNode = libxml2.newNode('target-context')
68             targetNode.addChild(self.arguments['target'])
69             context[0].addChild(matchNode)
70             context[0].addChild(targetNode)
71         p.xpathFreeContext()
72
73         return doc
74
75     def apply_interpreted(self, rspec):
76         self.add_rule_context_to_rspec(rspec)
77         # Interpreted
78         #
79         # output =
80         #    if (match(match_args, rspec)
81         #       then target(target_args, rspec)
82         #       else rspec
83         
84         if (self.match(rspec)):
85             return self.wrap_up(self.target(rspec))
86         else:
87             return self.wrap_up(rspec)
88
89
90     def apply_compiled(rspec):
91         # Not supported yet
92         return None
93
94     def load_xml_extension (self, type, chain, rule_number):
95         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
96
97         self.xmldoc = libxml2.parseFile(filename)
98         p = self.xmldoc.xpathNewContext()
99
100         context = p.xpathEval('//context/@select')
101         self.context[type] = context[0].content
102
103         processor = p.xpathEval('//processor/@filename')
104
105         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
106         if (context != []):
107             self.terminal = 1
108         
109         self.processors[type] = processor[0].content
110         self.arguments[type] = p.xpathEval('//rule')
111
112         p.xpathFreeContext()
113
114
115     def wrap_rspec (self, type, rspec):
116         argument = self.arguments[type]
117         p = rspec.xmldoc.xpathNewContext()
118         root_node = p.xpathEval('/RSpec')
119         if (not root_node or not root_node):
120             raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
121
122         root_node.addChild(arguments[type])
123         return rspec
124
125     def __init__(self, chain=None, rule_number=None):
126         self.rule_number = None
127         self.chain = None
128         self.xmldoc = None
129         self.terminal = 0
130         self.final_processor = '__sfatables_rule_wrap_up__.xsl'
131
132         self.arguments = {'match':None,'target':None}
133         self.processors = {'match':None,'target':None}
134         self.context = {'match':None,'target':None}
135
136         if (chain and rule_number):
137             self.load_xml_extension('match', chain, rule_number)
138             self.load_xml_extension('target',chain, rule_number)
139             self.rule_number = rule_number
140             self.chain = chain
141         return
142         
143     def free(self):
144         self.xmldoc.freeDoc()