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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/attributevaluereader.py@7109
Revision 7109, 3.5 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 AttributeValue type
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.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    @cvar TYPE: XACML class type that this reader will read values into
23    @type TYPE: abc.ABCMeta
24   
25    @cvar FACTORY: factory function for returning an Attribute value type for a
26    given XACML Attribute value URI
27    @type FACTORY: ndg.xacml.core.attributevalue.AttributeValueClassFactory
28    '''
29    TYPE = AttributeValue
30    FACTORY = AttributeValueClassFactory()
31   
32    def __call__(self, obj):
33        """Parse *AttributeValue type element - override this method instead of
34        _parseExtension since AttributeValue class is virtual.  A sub-type can
35        be instantiated only once the data type attribute is parsed
36       
37        @param obj: input object to parse
38        @type obj: ElementTree Element, or stream object
39        @return: new XACML attribute value instance
40        @rtype: ndg.xacml.core.attributevalue.AttributeValue derived type
41        @raise XMLParseError: error reading element       
42        """
43        elem = super(AttributeValueReader, self)._parse(obj)
44       
45        xacmlType = self.__class__.TYPE
46        localName = QName.getLocalPart(elem.tag)
47        if localName != xacmlType.ELEMENT_LOCAL_NAME:
48            raise XMLParseError("No \"%s\" element found" % 
49                                xacmlType.ELEMENT_LOCAL_NAME)
50           
51        # Unpack *required* attributes from top-level element
52        elemAttributeValues = []
53        for attributeName in (xacmlType.DATA_TYPE_ATTRIB_NAME,):
54            attributeValue = elem.attrib.get(attributeName)
55            if attributeValue is None:
56                raise XMLParseError('No "%s" attribute found in "%s" element' %
57                                    (attributeName, 
58                                     xacmlType.ELEMENT_LOCAL_NAME))
59               
60            elemAttributeValues.append(attributeValue)
61             
62        attributeValueClass = self.__class__.FACTORY(elemAttributeValues[0])
63        attributeValue = attributeValueClass()
64        attributeValue.dataType = elemAttributeValues[0]
65        self._parseExtension(elem, attributeValue)
66       
67        return attributeValue
68   
69    def _parseExtension(self, elem, attributeValue):
70        """Parse XML Attribute value element
71       
72        @param elem: ElementTree XML element
73        @type elem: xml.etree.Element
74       
75        @param attributeValue: attribute selector to be updated with parsed
76        values
77        @type attributeValue: ndg.xacml.core.attributevalue.AttributeValue
78       
79        @raise XMLParseError: error parsing attribute ID XML attribute
80        """
81        if elem.text is None:
82            raise XMLParseError('No attribute value element found parsing %r' % 
83                                AttributeValueReader.TYPE.ELEMENT_LOCAL_NAME) 
84           
85        attributeValue.value = elem.text
86   
Note: See TracBrowser for help on using the repository browser.