1 | """NDG XACML ElementTree based Rule 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.rule import Rule, Effect |
---|
13 | from ndg.xacml.core.condition import Condition |
---|
14 | from ndg.xacml.core.target import Target |
---|
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 | from ndg.xacml.parsers.etree.factory import ReaderFactory |
---|
19 | |
---|
20 | |
---|
21 | class RuleReader(ETreeAbstractReader): |
---|
22 | '''ElementTree based XACML Rule parser |
---|
23 | |
---|
24 | @cvar TYPE: XACML type to instantiate from parsed object |
---|
25 | @type TYPE: type |
---|
26 | ''' |
---|
27 | TYPE = Rule |
---|
28 | |
---|
29 | def __call__(self, obj): |
---|
30 | """Parse rule object |
---|
31 | |
---|
32 | @param obj: input object to parse |
---|
33 | @type obj: ElementTree Element, or stream object |
---|
34 | @return: new XACML expression instance |
---|
35 | @rtype: ndg.xacml.core.rule.Rule derived type |
---|
36 | @raise XMLParseError: error reading element |
---|
37 | """ |
---|
38 | elem = super(RuleReader, self)._parse(obj) |
---|
39 | |
---|
40 | xacmlType = RuleReader.TYPE |
---|
41 | rule = 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 | # Unpack *required* attributes from top-level element |
---|
49 | attributeValues = [] |
---|
50 | for attributeName in (xacmlType.RULE_ID_ATTRIB_NAME, |
---|
51 | xacmlType.EFFECT_ATTRIB_NAME): |
---|
52 | attributeValue = elem.attrib.get(attributeName) |
---|
53 | if attributeValue is None: |
---|
54 | raise XMLParseError('No "%s" attribute found in "%s" ' |
---|
55 | 'element' % |
---|
56 | (attributeName, |
---|
57 | xacmlType.ELEMENT_LOCAL_NAME)) |
---|
58 | |
---|
59 | attributeValues.append(attributeValue) |
---|
60 | |
---|
61 | rule.effect = Effect() |
---|
62 | rule.id, rule.effect.value = attributeValues |
---|
63 | |
---|
64 | # Parse sub-elements |
---|
65 | for childElem in elem: |
---|
66 | localName = QName.getLocalPart(childElem.tag) |
---|
67 | |
---|
68 | if localName == xacmlType.DESCRIPTION_LOCAL_NAME: |
---|
69 | if childElem.text is not None: |
---|
70 | rule.description = childElem.text.strip() |
---|
71 | |
---|
72 | elif localName == Condition.ELEMENT_LOCAL_NAME: |
---|
73 | ConditionReader = ReaderFactory.getReader(Condition) |
---|
74 | rule.condition = ConditionReader.parse(childElem) |
---|
75 | |
---|
76 | elif localName == Target.ELEMENT_LOCAL_NAME: |
---|
77 | TargetReader = ReaderFactory.getReader(Target) |
---|
78 | rule.target = TargetReader.parse(childElem) |
---|
79 | |
---|
80 | else: |
---|
81 | raise XMLParseError("XACML Rule child element name %r not " |
---|
82 | "recognised" % localName) |
---|
83 | |
---|
84 | return rule |
---|