1 | """NDG XACML ElementTree based reader for Expression type |
---|
2 | |
---|
3 | NERC DataGrid |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "18/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.expression import Expression |
---|
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 | @cvar TYPE: XACML type to instantiate from parsed object |
---|
24 | @type TYPE: abc.ABCMeta |
---|
25 | ''' |
---|
26 | TYPE = Expression |
---|
27 | |
---|
28 | def __call__(self, obj): |
---|
29 | """Parse Expression object |
---|
30 | |
---|
31 | @param obj: input object to parse |
---|
32 | @type obj: ElementTree Element, or stream object |
---|
33 | @return: new XACML expression instance |
---|
34 | @rtype: ndg.xacml.core.expression.Expression derived type |
---|
35 | @raise XMLParseError: error reading element |
---|
36 | """ |
---|
37 | elem = super(ExpressionReader, self)._parse(obj) |
---|
38 | |
---|
39 | xacmlType = self.__class__.TYPE |
---|
40 | expression = xacmlType() |
---|
41 | |
---|
42 | localName = QName.getLocalPart(elem.tag) |
---|
43 | if localName != xacmlType.ELEMENT_LOCAL_NAME: |
---|
44 | raise XMLParseError("No \"%s\" element found" % |
---|
45 | xacmlType.ELEMENT_LOCAL_NAME) |
---|
46 | |
---|
47 | # Unpack *required* attributes from top-level element |
---|
48 | attributeValues = [] |
---|
49 | for attributeName in (xacmlType.DATA_TYPE_ATTRIB_NAME,): |
---|
50 | attributeValue = elem.attrib.get(attributeName) |
---|
51 | if attributeValue is None: |
---|
52 | raise XMLParseError('No "%s" attribute found in "%s" element' % |
---|
53 | (attributeName, |
---|
54 | xacmlType.ELEMENT_LOCAL_NAME)) |
---|
55 | |
---|
56 | attributeValues.append(attributeValue) |
---|
57 | |
---|
58 | expression.dataType, = attributeValues |
---|
59 | |
---|
60 | self._parseExtension(elem, expression) |
---|
61 | |
---|
62 | return expression |
---|
63 | |
---|
64 | @abstractmethod |
---|
65 | def _parseExtension(self, elem, expression): |
---|
66 | """Derived classes should implement this method to read any remaining |
---|
67 | attributes and elements specific to their type |
---|
68 | |
---|
69 | @param elem: ElementTree XML element |
---|
70 | @type elem: xml.etree.Element |
---|
71 | |
---|
72 | @param expression: attribute selector to be updated with parsed |
---|
73 | values |
---|
74 | @type expression: ndg.xacml.core.attributevalue.AttributeValue |
---|
75 | |
---|
76 | |
---|
77 | @raise NotImplementedError: Derived classes should implement |
---|
78 | """ |
---|
79 | raise NotImplementedError() |
---|
80 | |
---|
81 | # Set up new class as an abstract base itself |
---|
82 | ETreeAbstractReader.register(ExpressionReader) |
---|