4 # allow to run sfa2wsdl if this is missing (for mac)
6 except: print >>sys.stderr, "WARNING, could not import libxslt"
8 from sfatables.globals import sfatables_config
11 def apply_processor(self, type, doc, output_xpath_filter=None):
12 processor = self.processors[type]
15 filepath = os.path.join(sfatables_config, 'processors', processor)
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)
27 stylesheet_result = xpath_result
30 stylesheet_result = result #style.saveResultToString(result)
32 style.freeStylesheet()
36 return stylesheet_result
38 def wrap_up(self, doc):
39 filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
41 if not os.path.exists(filepath):
42 raise Exception('Could not find final rule filter')
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()
52 return stylesheet_result
54 def match(self, rspec):
55 match_result = self.apply_processor('match',rspec,"//result/@verdict")
56 return (match_result[0].content=='True')
58 def target(self, rspec):
59 target_result = self.apply_processor('target',rspec,None)
62 def add_rule_context_to_rspec(self, doc):
63 p = doc.xpathNewContext()
64 context = p.xpathEval("//RSpec")
66 raise Exception('Request is not an rspec')
68 # Add the request context
69 matchNode = libxml2.newNode('match-context')
70 for match_argument in self.arguments['match']:
71 matchNode.addChild(match_argument)
73 targetNode = libxml2.newNode('target-context')
74 for target_argument in self.arguments['target']:
75 targetNode.addChild(target_argument)
77 context[0].addChild(matchNode)
78 context[0].addChild(targetNode)
83 def apply_interpreted(self, rspec):
84 rspec = self.add_rule_context_to_rspec(rspec)
88 # if (match(match_args, rspec)
89 # 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(self, 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()