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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/expressionreader.py@7109
Revision 7109, 2.9 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 reader for Expression type
2
3NERC DataGrid
4"""
5__author__ = "P J Kershaw"
6__date__ = "18/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 abc import abstractmethod
13
14from ndg.xacml.core.expression import Expression
15from ndg.xacml.parsers import XMLParseError
16from ndg.xacml.parsers.etree import QName
17from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
18
19
20class ExpressionReader(ETreeAbstractReader):
21    '''ElementTree based XACML Expression type parser
22   
23    @cvar TYPE: XACML type to instantiate from parsed object
24    @type TYPE: abc.ABCMeta
25    '''
26    TYPE = Expression
27   
28    def __call__(self, obj):
29        """Parse Expression object
30       
31        @param obj: input object to parse
32        @type obj: ElementTree Element, or stream object
33        @return: new XACML expression instance
34        @rtype: ndg.xacml.core.expression.Expression derived type
35        @raise XMLParseError: error reading element               
36        """
37        elem = super(ExpressionReader, self)._parse(obj)
38       
39        xacmlType = self.__class__.TYPE
40        expression = xacmlType()
41       
42        localName = QName.getLocalPart(elem.tag)
43        if localName != xacmlType.ELEMENT_LOCAL_NAME:
44            raise XMLParseError("No \"%s\" element found" % 
45                                xacmlType.ELEMENT_LOCAL_NAME)
46           
47        # Unpack *required* attributes from top-level element
48        attributeValues = []
49        for attributeName in (xacmlType.DATA_TYPE_ATTRIB_NAME,):
50            attributeValue = elem.attrib.get(attributeName)
51            if attributeValue is None:
52                raise XMLParseError('No "%s" attribute found in "%s" element' %
53                                    (attributeName, 
54                                     xacmlType.ELEMENT_LOCAL_NAME))
55               
56            attributeValues.append(attributeValue)
57             
58        expression.dataType, = attributeValues
59                   
60        self._parseExtension(elem, expression)
61       
62        return expression
63       
64    @abstractmethod
65    def _parseExtension(self, elem, expression):
66        """Derived classes should implement this method to read any remaining
67        attributes and elements specific to their type
68       
69        @param elem: ElementTree XML element
70        @type elem: xml.etree.Element
71       
72        @param expression: attribute selector to be updated with parsed
73        values
74        @type expression: ndg.xacml.core.attributevalue.AttributeValue
75       
76       
77        @raise NotImplementedError: Derived classes should implement
78        """
79        raise NotImplementedError()
80         
81# Set up new class as an abstract base itself             
82ETreeAbstractReader.register(ExpressionReader)
Note: See TracBrowser for help on using the repository browser.