this is required for the federica driver as well
[sfa.git] / sfatables / xmlrule.py
1 import sys,os
2
3 import libxml2
4 # allow to run sfa2wsdl if this is missing (for mac)
5 try:import libxslt
6 except: print >>sys.stderr, "WARNING, could not import libxslt"
7
8 from sfatables.globals import sfatables_config
9
10 class XMLRule:
11     def apply_processor(self, type, doc, output_xpath_filter=None):
12         processor = self.processors[type]
13
14         # XXX TO CLEAN UP
15         filepath = os.path.join(sfatables_config, 'processors', processor)
16         # XXX
17
18         styledoc = libxml2.parseFile(filepath)
19         style = libxslt.parseStylesheetDoc(styledoc)
20         result = style.applyStylesheet(doc, None)
21         if (output_xpath_filter):
22             p = result.xpathNewContext()
23             xpath_result = p.xpathEval(output_xpath_filter)
24             if (xpath_result == []):
25                 raise Exception("Could not apply processor %s."%processor)
26
27             stylesheet_result = xpath_result
28             p.xpathFreeContext()
29         else:
30             stylesheet_result = result #style.saveResultToString(result)
31
32         style.freeStylesheet()
33         #doc.freeDoc()
34         #result.freeDoc()
35
36         return stylesheet_result
37
38     def wrap_up(self, doc):
39         filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
40
41         if not os.path.exists(filepath):
42             raise Exception('Could not find final rule filter')
43
44         styledoc = libxml2.parseFile(filepath)
45         style = libxslt.parseStylesheetDoc(styledoc)
46         result = style.applyStylesheet(doc, None)
47         stylesheet_result = result#style.saveResultToString(result)
48         style.freeStylesheet()
49         #doc.freeDoc()
50         #result.freeDoc()
51
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 add_rule_context_to_rspec(self, doc):
63         p = doc.xpathNewContext()
64         context = p.xpathEval("//RSpec")
65         if (not context):
66             raise Exception('Request is not an rspec')
67         else:
68             # Add the request context
69             matchNode = libxml2.newNode('match-context')
70             for match_argument in self.arguments['match']:
71                 matchNode.addChild(match_argument)
72
73             targetNode = libxml2.newNode('target-context')
74             for target_argument in self.arguments['target']:
75                 targetNode.addChild(target_argument)
76
77             context[0].addChild(matchNode)
78             context[0].addChild(targetNode)
79         p.xpathFreeContext()
80
81         return doc
82
83     def apply_interpreted(self, rspec):
84         rspec = self.add_rule_context_to_rspec(rspec)
85         # Interpreted
86         #
87         # output =
88         #    if (match(match_args, rspec)
89         #       then target(target_args, rspec)
90         #       else rspec
91         
92         if (self.match(rspec)):
93             return (True,self.wrap_up(self.target(rspec)))
94         else:
95             return (False,self.wrap_up(rspec))
96
97
98     def apply_compiled(self, rspec):
99         # Not supported yet
100         return None
101
102     def load_xml_extension (self, type, chain, rule_number):
103         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
104
105         self.xmldoc = libxml2.parseFile(filename)
106         p = self.xmldoc.xpathNewContext()
107
108         context = p.xpathEval('//context/@select')
109         self.context[type] = context[0].content
110
111         processor = p.xpathEval('//processor/@filename')
112
113         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
114         if (context != []):
115             self.terminal = 1
116         
117         self.processors[type] = processor[0].content
118         self.arguments[type] = p.xpathEval('//rule//argument[value!=""]')
119
120         p.xpathFreeContext()
121
122
123     def __init__(self, chain=None, rule_number=None):
124         self.rule_number = None
125         self.chain = None
126         self.xmldoc = None
127         self.terminal = 0
128         self.final_processor = '__sfatables_rule_wrap_up__.xsl'
129
130         self.arguments = {'match':None,'target':None}
131         self.processors = {'match':None,'target':None}
132         self.context = {'match':None,'target':None}
133
134         if (chain and rule_number):
135             self.load_xml_extension('match', chain, rule_number)
136             self.load_xml_extension('target',chain, rule_number)
137             self.rule_number = rule_number
138             self.chain = chain
139         return
140         
141     def free(self):
142         self.xmldoc.freeDoc()