[6747] | 1 | """NDG XACML ElementTree based reader for AttributeValue type |
---|
| 2 | |
---|
[7087] | 3 | NERC DataGrid |
---|
[6747] | 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" |
---|
[7064] | 11 | __revision__ = "$Id$" |
---|
[6802] | 12 | from ndg.xacml.core.attributevalue import (AttributeValue, |
---|
| 13 | AttributeValueClassFactory) |
---|
[6747] | 14 | from ndg.xacml.parsers import XMLParseError |
---|
[6802] | 15 | from ndg.xacml.parsers.etree import QName |
---|
[6750] | 16 | from ndg.xacml.parsers.etree.expressionreader import ExpressionReader |
---|
[6747] | 17 | |
---|
| 18 | |
---|
[6750] | 19 | class AttributeValueReader(ExpressionReader): |
---|
[6747] | 20 | '''ElementTree based XACML Expression type parser |
---|
| 21 | ''' |
---|
| 22 | TYPE = AttributeValue |
---|
[6802] | 23 | FACTORY = AttributeValueClassFactory() |
---|
[6747] | 24 | |
---|
[6802] | 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 | |
---|
[6747] | 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) |
---|
[6802] | 60 | |
---|
| 61 | attributeValue.value = elem.text |
---|
[6747] | 62 | |
---|