1 | """NDG XACML ElementTree based reader for AttributeValue type |
---|
2 | |
---|
3 | NERC 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$" |
---|
12 | from ndg.xacml.core.attributevalue import (AttributeValue, |
---|
13 | AttributeValueClassFactory) |
---|
14 | from ndg.xacml.parsers import XMLParseError |
---|
15 | from ndg.xacml.parsers.etree import QName |
---|
16 | from ndg.xacml.parsers.etree.expressionreader import ExpressionReader |
---|
17 | |
---|
18 | |
---|
19 | class 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 | |
---|