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

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

spearate out Expression and AttributeValue? classes into separate modules.

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 abc import abstractmethod
13
14from ndg.xacml.core.attribute import Expression, AttributeValue
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    TYPE = Expression
24   
25    def __call__(self, obj):
26        """Parse AttributeValue object"""
27        elem = super(ExpressionReader, self)._parse(obj)
28       
29        cls = self.__class__.TYPE
30        expression = cls()
31       
32        localName = QName.getLocalPart(elem.tag)
33        if localName != cls.ELEMENT_LOCAL_NAME:
34            raise XMLParseError("No \"%s\" element found" % 
35                                cls.ELEMENT_LOCAL_NAME)
36       
37        dataType = elem.attrib.get(cls.DATA_TYPE_ATTRIB_NAME)
38        if dataType is None:
39            raise XMLParseError('No "%s" attribute found in "%s" element' %
40                                (cls.DATA_TYPE_ATTRIB_NAME, 
41                                 cls.ELEMENT_LOCAL_NAME))
42               
43        expression.dataType = dataType
44       
45        self._parseExtension(elem, expression)
46       
47        return expression
48       
49    @abstractmethod
50    def _parseExtension(self, elem, expression):
51        """Derived classes should implement this method to read any remaining
52        attributes and elements specific to their type"""
53        raise NotImplementedError()
54         
55# Set up new class as an abstract base itself             
56ETreeAbstractReader.register(ExpressionReader)
57
58
59class AttributeValueReader(ExpressionReader):
60    '''ElementTree based XACML AttributeValue type parser
61    '''
62    TYPE = AttributeValue
63   
64    def _parseExtension(self, elem, attributeValue):
65        """Implement abstract method to complete parsing of attribute value"""
66           
67        if elem.text is None:
68            raise XMLParseError('No attribute value element found parsing %r' % 
69                                AttributeValueReader.TYPE.ELEMENT_LOCAL_NAME) 
70             
71        attributeValue.value = elem.text.strip()
72   
Note: See TracBrowser for help on using the repository browser.