source: TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/attributevaluereader.py @ 6802

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

Fixed AttributeValueReader? to handler attribute value derived types correctly.

Line 
1"""NDG XACML ElementTree based reader for AttributeValue type
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.attributevalue import (AttributeValue, 
13                                           AttributeValueClassFactory)
14from ndg.xacml.parsers import XMLParseError
15from ndg.xacml.parsers.etree import QName
16from ndg.xacml.parsers.etree.expressionreader import ExpressionReader
17
18
19class AttributeValueReader(ExpressionReader):
20    '''ElementTree based XACML Expression type parser
21    '''
22    TYPE = AttributeValue
23    FACTORY = AttributeValueClassFactory()
24   
25    def __call__(self, obj):
26        """Parse *AttributeValue type element - override this method instead of
27        _parseExtension since AttributeValue class is virtual.  A sub-type can
28        be instantiated only once the data type attribute is parsed
29        """
30        elem = super(AttributeValueReader, self)._parse(obj)
31       
32        xacmlType = self.__class__.TYPE
33        localName = QName.getLocalPart(elem.tag)
34        if localName != xacmlType.ELEMENT_LOCAL_NAME:
35            raise XMLParseError("No \"%s\" element found" % 
36                                xacmlType.ELEMENT_LOCAL_NAME)
37           
38        # Unpack *required* attributes from top-level element
39        elemAttributeValues = []
40        for attributeName in (xacmlType.DATA_TYPE_ATTRIB_NAME,):
41            attributeValue = elem.attrib.get(attributeName)
42            if attributeValue is None:
43                raise XMLParseError('No "%s" attribute found in "%s" element' %
44                                    (attributeName, 
45                                     xacmlType.ELEMENT_LOCAL_NAME))
46               
47            elemAttributeValues.append(attributeValue)
48             
49        attributeValueClass = self.__class__.FACTORY(elemAttributeValues[0])
50        attributeValue = attributeValueClass()
51        attributeValue.dataType = elemAttributeValues[0]
52        self._parseExtension(elem, attributeValue)
53       
54        return attributeValue
55   
56    def _parseExtension(self, elem, attributeValue):
57        if elem.text is None:
58            raise XMLParseError('No attribute value element found parsing %r' % 
59                                AttributeValueReader.TYPE.ELEMENT_LOCAL_NAME) 
60           
61        attributeValue.value = elem.text
62   
Note: See TracBrowser for help on using the repository browser.