1 | """NDG XACML ElementTree based Apply type reader |
---|
2 | |
---|
3 | NERC DataGrid |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "19/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 | import logging |
---|
13 | log = logging.getLogger(__name__) |
---|
14 | |
---|
15 | from ndg.xacml.core.apply import Apply |
---|
16 | from ndg.xacml.core.attributevalue import AttributeValue |
---|
17 | from ndg.xacml.core.variablereference import VariableReference |
---|
18 | from ndg.xacml.core.attributeselector import AttributeSelector |
---|
19 | from ndg.xacml.core.attributedesignator import (SubjectAttributeDesignator, |
---|
20 | EnvironmentAttributeDesignator, |
---|
21 | ActionAttributeDesignator, |
---|
22 | EnvironmentAttributeDesignator) |
---|
23 | from ndg.xacml.parsers import XMLParseError |
---|
24 | from ndg.xacml.parsers.etree import QName |
---|
25 | from ndg.xacml.parsers.etree.reader import ETreeAbstractReader |
---|
26 | from ndg.xacml.parsers.etree.expressionreader import ExpressionReader |
---|
27 | from ndg.xacml.parsers.etree.factory import ReaderFactory |
---|
28 | |
---|
29 | |
---|
30 | class ApplyReader(ETreeAbstractReader): |
---|
31 | '''ElementTree based XACML Apply type parser |
---|
32 | |
---|
33 | @cvar TYPE: XACML type to instantiate from parsed object |
---|
34 | @type string: type |
---|
35 | ''' |
---|
36 | TYPE = Apply |
---|
37 | |
---|
38 | # These two are not currently implemented. When an implementation is made |
---|
39 | # the ELEMENT_LOCAL_NAME may be referenced from the native class rather than |
---|
40 | # a class variable here |
---|
41 | FUNCTION_ELEMENT_LOCAL_NAME = 'Function' |
---|
42 | VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME = 'VariableReference' |
---|
43 | |
---|
44 | def __call__(self, obj): |
---|
45 | """Parse Apply type object""" |
---|
46 | elem = super(ApplyReader, self)._parse(obj) |
---|
47 | |
---|
48 | xacmlType = self.__class__.TYPE |
---|
49 | applyObj = xacmlType() |
---|
50 | |
---|
51 | if QName.getLocalPart(elem.tag) != xacmlType.ELEMENT_LOCAL_NAME: |
---|
52 | raise XMLParseError("No \"%s\" element found" % |
---|
53 | xacmlType.ELEMENT_LOCAL_NAME) |
---|
54 | |
---|
55 | # Unpack *required* attributes from top-level element |
---|
56 | attributeValues = [] |
---|
57 | for attributeName in (xacmlType.FUNCTION_ID_ATTRIB_NAME, ): |
---|
58 | attributeValue = elem.attrib.get(attributeName) |
---|
59 | if attributeValue is None: |
---|
60 | raise XMLParseError('No "%s" attribute found in "%s" ' |
---|
61 | 'element' % (attributeName, |
---|
62 | xacmlType.ELEMENT_LOCAL_NAME)) |
---|
63 | |
---|
64 | attributeValues.append(attributeValue) |
---|
65 | |
---|
66 | applyObj.functionId, = attributeValues |
---|
67 | |
---|
68 | # Allow for any of the defined Expression sub-types in the child |
---|
69 | # elements |
---|
70 | for subElem in elem: |
---|
71 | localName = QName.getLocalPart(subElem.tag) |
---|
72 | if localName == xacmlType.ELEMENT_LOCAL_NAME: |
---|
73 | applyObj.expressions.append(ApplyReader.parse(subElem)) |
---|
74 | |
---|
75 | elif localName == AttributeValue.ELEMENT_LOCAL_NAME: |
---|
76 | AttributeValueReader = ReaderFactory.getReader(AttributeValue) |
---|
77 | applyObj.expressions.append(AttributeValueReader.parse(subElem)) |
---|
78 | |
---|
79 | elif localName == SubjectAttributeDesignator.ELEMENT_LOCAL_NAME: |
---|
80 | SubjectAttributeDesignatorReader = ReaderFactory.getReader( |
---|
81 | SubjectAttributeDesignator) |
---|
82 | applyObj.expressions.append( |
---|
83 | SubjectAttributeDesignatorReader.parse(subElem)) |
---|
84 | |
---|
85 | elif localName == EnvironmentAttributeDesignator.ELEMENT_LOCAL_NAME: |
---|
86 | EnvironmentAttributeDesignatorReader = ReaderFactory.getReader( |
---|
87 | EnvironmentAttributeDesignator) |
---|
88 | applyObj.expressions.append( |
---|
89 | EnvironmentAttributeDesignatorReader.parse(subElem)) |
---|
90 | |
---|
91 | elif localName == ActionAttributeDesignator.ELEMENT_LOCAL_NAME: |
---|
92 | ActionAttributeDesignatorReader = ReaderFactory.getReader( |
---|
93 | ActionAttributeDesignator) |
---|
94 | applyObj.expressions.append( |
---|
95 | ActionAttributeDesignatorReader.parse(subElem)) |
---|
96 | |
---|
97 | elif localName == EnvironmentAttributeDesignator.ELEMENT_LOCAL_NAME: |
---|
98 | EnvironmentAttributeDesignatorReader = ReaderFactory.getReader( |
---|
99 | EnvironmentAttributeDesignator) |
---|
100 | applyObj.expressions.append( |
---|
101 | EnvironmentAttributeDesignatorReader.parse(subElem)) |
---|
102 | |
---|
103 | elif localName == AttributeSelector.ELEMENT_LOCAL_NAME: |
---|
104 | AttributeSelectorReader = ReaderFactory.getReader( |
---|
105 | AttributeSelector) |
---|
106 | applyObj.expressions.append( |
---|
107 | AttributeSelectorReader.parse(subElem)) |
---|
108 | |
---|
109 | elif localName == Condition.ELEMENT_LOCAL_NAME: |
---|
110 | ConditionReader = ReaderFactory.getReader(Condition) |
---|
111 | applyObj.expressions.append(ConditionReader.parse(subElem)) |
---|
112 | |
---|
113 | elif localName == self.__class__.FUNCTION_ELEMENT_LOCAL_NAME: |
---|
114 | raise NotImplementedError('%r Apply sub-element not ' |
---|
115 | 'implemented', localName) |
---|
116 | |
---|
117 | elif (localName == VariableReference.ELEMENT_LOCAL_NAME): |
---|
118 | raise NotImplementedError('%r Apply sub-element not ' |
---|
119 | 'implemented', localName) |
---|
120 | else: |
---|
121 | raise XMLParseError('%r Apply sub-element not recognised', |
---|
122 | localName) |
---|
123 | |
---|
124 | return applyObj |
---|