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

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

Added parsing for Resource type and also a reader factory to return the reader class for a given XACML type.

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