source: TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/applyreader.py @ 7109

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/applyreader.py@7109
Revision 7109, 6.4 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 2: XACML-Security Integration

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