source: TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/rulereader.py @ 6746

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/rulereader.py@6746
Revision 6746, 2.4 KB checked in by pjkersha, 10 years ago (diff)

Added more !ETree readers for the different types.

Line 
1"""NDG XACML ElementTree based Target Element reader
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "16/03/10"
7__copyright__ = "(C) 2010 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id: $"
12from ndg.xacml.core.rule import Rule
13from ndg.xacml.core.condition import Condition
14from ndg.xacml.core.target import Target
15from ndg.xacml.parsers.etree import QName
16from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
17from ndg.xacml.parsers.etree.conditionreader import ConditionReader
18from ndg.xacml.parsers.etree.targetreader import TargetReader
19
20
21class RuleReader(ETreeAbstractReader):
22    '''ElementTree based XACML Rule parser
23    '''
24    def __call__(self, obj):
25        """Parse policy object"""
26        elem = super(RuleReader, self)._parse(obj)
27       
28        rule = Rule()
29        cls = Rule
30       
31        localName = QName.getLocalPart(elem.tag)
32        if localName != cls.ELEMENT_LOCAL_NAME:
33            raise XMLParseError("No \"%s\" element found" % 
34                                cls.ELEMENT_LOCAL_NAME)
35       
36        # Unpack *required* attributes from top-level element
37        attributeValues = []
38        for attributeName in (cls.RULE_ID_ATTRIB_NAME, cls.EFFECT_ATTRIB_NAME):
39            attributeValue = elem.attrib.get(attributeName)
40            if attributeValue is None:
41                raise XMLParseError('No "%s" attribute found in "%s" '
42                                        'element' %
43                                        (attributeName,
44                                         cls.ELEMENT_LOCAL_NAME))
45               
46            attributeValues.append(attributeValue) 
47                   
48        rule.id, rule.effect = attributeValues
49           
50        # Parse sub-elements
51        for childElem in elem:
52            localName = QName.getLocalPart(childElem.tag)
53           
54            if localName == cls.DESCRIPTION_LOCAL_NAME:
55                if elem.text is not None:
56                    rule.description = elem.text.strip()
57                   
58            elif localName == Condition.ELEMENT_LOCAL_NAME:
59                rule.condition = ConditionReader.parse(childElem)
60                                   
61            elif localName == Target.ELEMENT_LOCAL_NAME:
62                rule.target = TargetReader.parse(childElem)
63               
64        return rule
Note: See TracBrowser for help on using the repository browser.