Removed some debuggin print statements.
[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         styledoc = libxml2.parseFile(filepath)
14         style = libxslt.parseStylesheetDoc(styledoc)
15         result = style.applyStylesheet(doc, None)
16         if (output_xpath_filter):
17             p = result.xpathNewContext()
18             xpath_result = p.xpathEval(output_xpath_filter)
19             if (xpath_result == []):
20                 raise Exception("Could not apply processor %s."%processor)
21
22             stylesheet_result = xpath_result
23             p.xpathFreeContext()
24         else:
25             stylesheet_result = result #style.saveResultToString(result)
26
27         style.freeStylesheet()
28         #doc.freeDoc()
29         #result.freeDoc()
30
31         return stylesheet_result
32
33     def wrap_up(self, doc):
34         filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
35
36         if not os.path.exists(filepath):
37             raise Exception('Could not find final rule filter')
38
39         styledoc = libxml2.parseFile(filepath)
40         style = libxslt.parseStylesheetDoc(styledoc)
41         result = style.applyStylesheet(doc, None)
42         stylesheet_result = result#style.saveResultToString(result)
43         style.freeStylesheet()
44         #doc.freeDoc()
45         #result.freeDoc()
46
47         return stylesheet_result
48
49     def match(self, rspec):
50         match_result = self.apply_processor('match',rspec,"//result/@verdict") 
51         return (match_result[0].content=='True')
52
53     def target(self, rspec):
54         target_result = self.apply_processor('target',rspec,None)
55         return target_result
56
57     def add_rule_context_to_rspec(self, doc):
58         p = doc.xpathNewContext()
59         context = p.xpathEval("//RSpec")
60         if (not context):
61             raise Exception('Request is not an rspec')
62         else:
63             # Add the request context
64             matchNode = libxml2.newNode('match-context')
65             for match_argument in self.arguments['match']:
66                 matchNode.addChild(match_argument)
67
68             targetNode = libxml2.newNode('target-context')
69             for target_argument in self.arguments['target']:
70                 targetNode.addChild(target_argument)
71
72             context[0].addChild(matchNode)
73             context[0].addChild(targetNode)
74         p.xpathFreeContext()
75
76         return doc
77
78     def apply_interpreted(self, rspec):
79         rspec = self.add_rule_context_to_rspec(rspec)
80         # Interpreted
81         #
82         # output =
83         #    if (match(match_args, rspec)
84         #       then target(target_args, rspec)
85         #       else rspec
86         
87         if (self.match(rspec)):
88             return self.wrap_up(self.target(rspec))
89         else:
90             return self.wrap_up(rspec)
91
92
93     def apply_compiled(rspec):
94         # Not supported yet
95         return None
96
97     def load_xml_extension (self, type, chain, rule_number):
98         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
99
100         self.xmldoc = libxml2.parseFile(filename)
101         p = self.xmldoc.xpathNewContext()
102
103         context = p.xpathEval('//context/@select')
104         self.context[type] = context[0].content
105
106         processor = p.xpathEval('//processor/@filename')
107
108         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
109         if (context != []):
110             self.terminal = 1
111         
112         self.processors[type] = processor[0].content
113         self.arguments[type] = p.xpathEval('//rule//argument[value!=""]')
114
115         p.xpathFreeContext()
116
117
118     def __init__(self, chain=None, rule_number=None):
119         self.rule_number = None
120         self.chain = None
121         self.xmldoc = None
122         self.terminal = 0
123         self.final_processor = '__sfatables_rule_wrap_up__.xsl'
124
125         self.arguments = {'match':None,'target':None}
126         self.processors = {'match':None,'target':None}
127         self.context = {'match':None,'target':None}
128
129         if (chain and rule_number):
130             self.load_xml_extension('match', chain, rule_number)
131             self.load_xml_extension('target',chain, rule_number)
132             self.rule_number = rule_number
133             self.chain = chain
134         return
135         
136     def free(self):
137         self.xmldoc.freeDoc()