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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/rulereader.py@7109
Revision 7109, 3.2 KB checked in by pjkersha, 10 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 TYPE: type
26    '''
27    TYPE = Rule
28   
29    def __call__(self, obj):
30        """Parse rule object
31       
32        @param obj: input object to parse
33        @type obj: ElementTree Element, or stream object
34        @return: new XACML expression instance
35        @rtype: ndg.xacml.core.rule.Rule derived type
36        @raise XMLParseError: error reading element               
37        """
38        elem = super(RuleReader, self)._parse(obj)
39       
40        xacmlType = RuleReader.TYPE
41        rule = xacmlType()
42       
43        localName = QName.getLocalPart(elem.tag)
44        if localName != xacmlType.ELEMENT_LOCAL_NAME:
45            raise XMLParseError("No \"%s\" element found" % 
46                                xacmlType.ELEMENT_LOCAL_NAME)
47       
48        # Unpack *required* attributes from top-level element
49        attributeValues = []
50        for attributeName in (xacmlType.RULE_ID_ATTRIB_NAME, 
51                              xacmlType.EFFECT_ATTRIB_NAME):
52            attributeValue = elem.attrib.get(attributeName)
53            if attributeValue is None:
54                raise XMLParseError('No "%s" attribute found in "%s" '
55                                        'element' %
56                                        (attributeName,
57                                         xacmlType.ELEMENT_LOCAL_NAME))
58               
59            attributeValues.append(attributeValue) 
60       
61        rule.effect = Effect()       
62        rule.id, rule.effect.value = attributeValues
63           
64        # Parse sub-elements
65        for childElem in elem:
66            localName = QName.getLocalPart(childElem.tag)
67           
68            if localName == xacmlType.DESCRIPTION_LOCAL_NAME:
69                if childElem.text is not None:
70                    rule.description = childElem.text.strip()
71                   
72            elif localName == Condition.ELEMENT_LOCAL_NAME:
73                ConditionReader = ReaderFactory.getReader(Condition)
74                rule.condition = ConditionReader.parse(childElem)
75                                   
76            elif localName == Target.ELEMENT_LOCAL_NAME:
77                TargetReader = ReaderFactory.getReader(Target)
78                rule.target = TargetReader.parse(childElem)
79           
80            else:
81                raise XMLParseError("XACML Rule child element name %r not "
82                                    "recognised" % localName)               
83               
84        return rule
Note: See TracBrowser for help on using the repository browser.