2 # allow to run sfa2wsdl if this is missing (for mac)
5 except: print >>sys.stderr, "WARNING, could not import libxslt"
7 from sfatables.globals import *
10 def apply_processor(self, type, doc, output_xpath_filter=None):
11 processor = self.processors[type]
14 filepath = os.path.join(sfatables_config, 'processors', processor)
17 styledoc = libxml2.parseFile(filepath)
18 style = libxslt.parseStylesheetDoc(styledoc)
19 result = style.applyStylesheet(doc, None)
20 if (output_xpath_filter):
21 p = result.xpathNewContext()
22 xpath_result = p.xpathEval(output_xpath_filter)
23 if (xpath_result == []):
24 raise Exception("Could not apply processor %s."%processor)
26 stylesheet_result = xpath_result
29 stylesheet_result = result #style.saveResultToString(result)
31 style.freeStylesheet()
35 return stylesheet_result
37 def wrap_up(self, doc):
38 filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
40 if not os.path.exists(filepath):
41 raise Exception('Could not find final rule filter')
43 styledoc = libxml2.parseFile(filepath)
44 style = libxslt.parseStylesheetDoc(styledoc)
45 result = style.applyStylesheet(doc, None)
46 stylesheet_result = result#style.saveResultToString(result)
47 style.freeStylesheet()
51 return stylesheet_result
53 def match(self, rspec):
54 match_result = self.apply_processor('match',rspec,"//result/@verdict")
55 return (match_result[0].content=='True')
57 def target(self, rspec):
58 target_result = self.apply_processor('target',rspec,None)
61 def add_rule_context_to_rspec(self, doc):
62 p = doc.xpathNewContext()
63 context = p.xpathEval("//RSpec")
65 raise Exception('Request is not an rspec')
67 # Add the request context
68 matchNode = libxml2.newNode('match-context')
69 for match_argument in self.arguments['match']:
70 matchNode.addChild(match_argument)
72 targetNode = libxml2.newNode('target-context')
73 for target_argument in self.arguments['target']:
74 targetNode.addChild(target_argument)
76 context[0].addChild(matchNode)
77 context[0].addChild(targetNode)
82 def apply_interpreted(self, rspec):
83 rspec = self.add_rule_context_to_rspec(rspec)
87 # if (match(match_args, rspec)
88 # then target(target_args, rspec)
92 if (self.match(rspec)):
93 return (True,self.wrap_up(self.target(rspec)))
95 return (False,self.wrap_up(rspec))
98 def apply_compiled(rspec):
102 def load_xml_extension (self, type, chain, rule_number):
103 filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
105 self.xmldoc = libxml2.parseFile(filename)
106 p = self.xmldoc.xpathNewContext()
108 context = p.xpathEval('//context/@select')
109 self.context[type] = context[0].content
111 processor = p.xpathEval('//processor/@filename')
113 context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
117 self.processors[type] = processor[0].content
118 self.arguments[type] = p.xpathEval('//rule//argument[value!=""]')
123 def __init__(self, chain=None, rule_number=None):
124 self.rule_number = None
128 self.final_processor = '__sfatables_rule_wrap_up__.xsl'
130 self.arguments = {'match':None,'target':None}
131 self.processors = {'match':None,'target':None}
132 self.context = {'match':None,'target':None}
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
142 self.xmldoc.freeDoc()