source: TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/applyreader.py @ 6753

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

Started adding parsing for Condition and Apply types.

Line 
1"""NDG XACML ElementTree based Apply type reader
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "19/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.apply import Apply
13from ndg.xacml.core.attributedesignator import (SubjectAttributeDesignator,
14                                                ResourceAttributeDesignator,
15                                                ActionAttributeDesignator,
16                                                EnvironmentAttributeDesignator)
17from ndg.xacml.parsers import XMLParseError
18from ndg.xacml.parsers.etree import QName
19from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
20from ndg.xacml.parsers.etree.factory import ReaderFactory
21
22
23class ApplyReader(ETreeAbstractReader):
24    '''ElementTree based XACML Apply type parser
25   
26    @cvar TYPE: XACML type to instantiate from parsed object
27    @type string: type
28    '''
29    TYPE = Apply
30   
31    def __call__(self, obj):
32        """Parse Apply type object"""
33        elem = super(ApplyReader, self)._parse(obj)
34       
35        xacmlType = self.__class__.TYPE
36        applyObj = xacmlType()
37       
38        if QName.getLocalPart(elem.tag) != xacmlType.ELEMENT_LOCAL_NAME:
39            raise XMLParseError("No \"%s\" element found" % 
40                                xacmlType.ELEMENT_LOCAL_NAME)
41           
42        # Unpack *required* attributes from top-level element
43        attributeValues = []
44        for attributeName in (xacmlType.FUNCTION_ID_ATTRIB_NAME, ):
45            attributeValue = elem.attrib.get(attributeName)
46            if attributeValue is None:
47                raise XMLParseError('No "%s" attribute found in "%s" '
48                                    'element' % (attributeName,
49                                                 xacmlType.ELEMENT_LOCAL_NAME))
50               
51            attributeValues.append(attributeValue) 
52                   
53        applyObj.functionId, = attributeValues
54       
55        # Allow for any of the defined Expression sub-types in the child
56        # elements
57        for subElem in elem:
58            localName = QName.getLocalPart(elem[0].tag)
59            if localName == xacmlType.ELEMENT_LOCAL_NAME:
60                applyObj.expressions.append(ApplyReader.parse(subElem))
61               
62            elif localName == SubjectAttributeDesignator.ELEMENT_LOCAL_NAME:
63                SubjectAttributeDesignatorReader = ReaderFactory.getReader(
64                                                    SubjectAttributeDesignator)
65                applyObj.expressions.append(
66                                SubjectAttributeDesignatorReader.parse(subElem))
67            else:
68                raise XMLParseError('%r Apply sub-element not recognised' % 
69                                    localName)
70   
71        return applyObj
Note: See TracBrowser for help on using the repository browser.