source: TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/rulereader.py @ 7087

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

Incomplete - task 2: XACML-Security Integration

  • updating epydoc ready for release.
  • Property svn:keywords set to Id
Line 
1"""NDG XACML ElementTree based Rule Element reader
2
3NERC DataGrid
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, Effect
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, 
44                              xacmlType.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                                         xacmlType.ELEMENT_LOCAL_NAME))
51               
52            attributeValues.append(attributeValue) 
53       
54        rule.effect = Effect()       
55        rule.id, rule.effect.value = attributeValues
56           
57        # Parse sub-elements
58        for childElem in elem:
59            localName = QName.getLocalPart(childElem.tag)
60           
61            if localName == xacmlType.DESCRIPTION_LOCAL_NAME:
62                if childElem.text is not None:
63                    rule.description = childElem.text.strip()
64                   
65            elif localName == Condition.ELEMENT_LOCAL_NAME:
66                ConditionReader = ReaderFactory.getReader(Condition)
67                rule.condition = ConditionReader.parse(childElem)
68                                   
69            elif localName == Target.ELEMENT_LOCAL_NAME:
70                TargetReader = ReaderFactory.getReader(Target)
71                rule.target = TargetReader.parse(childElem)
72           
73            else:
74                raise XMLParseError("XACML Rule child element name %r not "
75                                    "recognised" % localName)               
76               
77        return rule
Note: See TracBrowser for help on using the repository browser.