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

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

Adding parsing for Subject type and SubjectMatch? elements.

Line 
1"""NDG XACML ElementTree based Rule 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 import XMLParseError
16from ndg.xacml.parsers.etree import QName
17from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
18from ndg.xacml.parsers.etree.conditionreader import ConditionReader
19from ndg.xacml.parsers.etree.targetreader import TargetReader
20
21
22class RuleReader(ETreeAbstractReader):
23    '''ElementTree based XACML Rule parser
24    '''
25    def __call__(self, obj):
26        """Parse rule object"""
27        elem = super(RuleReader, self)._parse(obj)
28       
29        rule = Rule()
30        cls = Rule
31       
32        localName = QName.getLocalPart(elem.tag)
33        if localName != cls.ELEMENT_LOCAL_NAME:
34            raise XMLParseError("No \"%s\" element found" % 
35                                cls.ELEMENT_LOCAL_NAME)
36       
37        # Unpack *required* attributes from top-level element
38        attributeValues = []
39        for attributeName in (cls.RULE_ID_ATTRIB_NAME, cls.EFFECT_ATTRIB_NAME):
40            attributeValue = elem.attrib.get(attributeName)
41            if attributeValue is None:
42                raise XMLParseError('No "%s" attribute found in "%s" '
43                                        'element' %
44                                        (attributeName,
45                                         cls.ELEMENT_LOCAL_NAME))
46               
47            attributeValues.append(attributeValue) 
48                   
49        rule.id, rule.effect = attributeValues
50           
51        # Parse sub-elements
52        for childElem in elem:
53            localName = QName.getLocalPart(childElem.tag)
54           
55            if localName == cls.DESCRIPTION_LOCAL_NAME:
56                if elem.text is not None:
57                    rule.description = elem.text.strip()
58                   
59            elif localName == Condition.ELEMENT_LOCAL_NAME:
60                rule.condition = ConditionReader.parse(childElem)
61                                   
62            elif localName == Target.ELEMENT_LOCAL_NAME:
63                rule.target = TargetReader.parse(childElem)
64           
65            else:
66                raise XMLParseError("XACML Rule child element name %r not "
67                                    "recognised" % localName)               
68               
69        return rule
Note: See TracBrowser for help on using the repository browser.