(no commit message)
[sfa.git] / sfatables / xmlextension.py
1 # Matches and targets are specified using XML files.
2 # They provide the following information:
3 #   - The context required by the match
4 #   - The processor that actually implements the match or target
5 #   - The parameters that the processor needs to evaluate the context
6
7 import libxml2
8
9 match_dir = 'matches'
10
11 class Xmlextension:
12     context = ""
13     processor = ""
14     operand = "VALUE"
15     arguments = []
16
17     def __init__(self, component_name):
18         filename = match_dir+"/"+component_name+".xml"
19         self.xmldoc = libxml2.parseFile(filename)
20
21         # TODO: Check xmldoc against a schema
22         p = self.xmldoc.xpathNewContext()
23
24         # <context select="..."/>
25         # <rule><argument param="..."/></rule>
26         # <processor name="..."/>
27
28         context = p.xpathEval('//context/@select')
29         self.context = context[0].content
30
31         processor = p.xpathEval('//processor/@filename')
32         self.context = processor[0].content
33
34         name = p.xpathEval('//rule/argument/name')
35         help = p.xpathEval('//rule/argument/help')
36         target = p.xpathEval('//rule/argument/operand')
37
38         self.arguments = map(lambda (name,help,target):{'name':name.content,'help':help.content,'target':target.content}, zip(name,help,target))
39         
40         p.xpathFreeContext()
41         self.xmldoc.freeDoc()
42
43         return
44