1 | """NDG XACML ElementTree based reader for AttributeValue type |
---|
2 | |
---|
3 | NERC 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: $" |
---|
12 | from abc import abstractmethod |
---|
13 | |
---|
14 | from ndg.xacml.core.attribute import Expression, AttributeValue |
---|
15 | from ndg.xacml.parsers import XMLParseError |
---|
16 | from ndg.xacml.parsers.etree import QName |
---|
17 | from ndg.xacml.parsers.etree.reader import ETreeAbstractReader |
---|
18 | |
---|
19 | |
---|
20 | class 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 |
---|
56 | ETreeAbstractReader.register(ExpressionReader) |
---|
57 | |
---|
58 | |
---|
59 | class 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 | |
---|