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

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

Parsing functionality near complete for rule1.xml

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