1 | """NDG XACML ElementTree based Target Element reader |
---|
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.condition import Condition |
---|
13 | from ndg.xacml.parsers import XMLParseError |
---|
14 | from ndg.xacml.parsers.etree import QName |
---|
15 | from ndg.xacml.parsers.etree.reader import ETreeAbstractReader |
---|
16 | from ndg.xacml.parsers.etree.applyreader import ApplyReader |
---|
17 | |
---|
18 | |
---|
19 | class ConditionReader(ETreeAbstractReader): |
---|
20 | '''ElementTree based XACML 2.0 Condition parser. Note the difference to |
---|
21 | XACML 1.0: the Condition element is its own type and not an Apply type. |
---|
22 | It expects a single Expression derived type child element |
---|
23 | |
---|
24 | @cvar TYPE: XACML type to instantiate from parsed object |
---|
25 | @type TYPE: type |
---|
26 | ''' |
---|
27 | TYPE = Condition |
---|
28 | |
---|
29 | def __call__(self, obj): |
---|
30 | """Parse condition object |
---|
31 | |
---|
32 | @param obj: input object to parse |
---|
33 | @type obj: ElementTree Element, or stream object |
---|
34 | @return: new XACML condition instance |
---|
35 | @rtype: ndg.xacml.core.condition.Condition |
---|
36 | @raise XMLParseError: error reading sub-elements |
---|
37 | """ |
---|
38 | elem = super(ConditionReader, self)._parse(obj) |
---|
39 | |
---|
40 | xacmlType = self.__class__.TYPE |
---|
41 | condition = xacmlType() |
---|
42 | |
---|
43 | localName = QName.getLocalPart(elem.tag) |
---|
44 | if localName != xacmlType.ELEMENT_LOCAL_NAME: |
---|
45 | raise XMLParseError("No \"%s\" element found" % |
---|
46 | xacmlType.ELEMENT_LOCAL_NAME) |
---|
47 | |
---|
48 | # Parse expression sub-element |
---|
49 | nSubElem = len(elem) |
---|
50 | if nSubElem != 1: |
---|
51 | raise XMLParseError('XACML 2.0 policy schema expects only one ' |
---|
52 | 'expression sub-element in the Condition ' |
---|
53 | 'element; policy file has %d' % nSubElem) |
---|
54 | |
---|
55 | subElemlocalName = QName.getLocalPart(elem[0].tag) |
---|
56 | if subElemlocalName == xacmlType.APPLY_ELEMENT_LOCAL_NAME: |
---|
57 | condition.expression = ApplyReader.parse(elem[0]) |
---|
58 | else: |
---|
59 | raise XMLParseError('Expecting %r Condition sub-element not ' |
---|
60 | 'recognised' % |
---|
61 | xacmlType.EXPRESSION_ELEMENT_LOCAL_NAME) |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | return condition |
---|