1 | """NDG XACML ElementTree based Generic Target Child Element reader - for |
---|
2 | Reosurce, Subject, Action and Environment |
---|
3 | |
---|
4 | NERC DataGrid |
---|
5 | """ |
---|
6 | __author__ = "P J Kershaw" |
---|
7 | __date__ = "18/03/10" |
---|
8 | __copyright__ = "(C) 2010 Science and Technology Facilities Council" |
---|
9 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
10 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
11 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
12 | __revision__ = "$Id$" |
---|
13 | from ndg.xacml.core.subject import Subject |
---|
14 | from ndg.xacml.parsers import XMLParseError |
---|
15 | from ndg.xacml.parsers.etree import QName |
---|
16 | from ndg.xacml.parsers.etree.reader import ETreeAbstractReader |
---|
17 | from ndg.xacml.parsers.etree.factory import ReaderFactory |
---|
18 | |
---|
19 | |
---|
20 | class TargetChildReader(ETreeAbstractReader): |
---|
21 | '''ElementTree based XACML generic target child element parser |
---|
22 | @cvar TYPE: XACML type to instantiate from parsed object |
---|
23 | @type string: type |
---|
24 | ''' |
---|
25 | |
---|
26 | def __call__(self, obj): |
---|
27 | """Parse subject object""" |
---|
28 | elem = super(TargetChildReader, self)._parse(obj) |
---|
29 | |
---|
30 | xacmlType = self.__class__.TYPE |
---|
31 | targetChild = xacmlType() |
---|
32 | |
---|
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 | # Parse match elements |
---|
39 | for childElem in elem: |
---|
40 | localName = QName.getLocalPart(childElem.tag) |
---|
41 | |
---|
42 | if localName == xacmlType.MATCH_TYPE.ELEMENT_LOCAL_NAME: |
---|
43 | # Get reader for the match type |
---|
44 | matchReader = ReaderFactory.getReader(xacmlType.MATCH_TYPE) |
---|
45 | targetChild.matches.append(matchReader.parse(childElem)) |
---|
46 | |
---|
47 | else: |
---|
48 | raise XMLParseError("XACML %r child element name %r not " |
---|
49 | "recognised" % (xacmlType.ELEMENT_LOCAL_NAME, |
---|
50 | localName)) |
---|
51 | |
---|
52 | return targetChild |
---|