source: TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/targetreader.py @ 6752

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

Added parsing for Resource type and also a reader factory to return the reader class for a given XACML type.

Line 
1"""NDG XACML ElementTree based Target Element reader
2
3NERC DataGrid Project
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: $"
12from ndg.xacml.core.target import Target
13from ndg.xacml.core.subject import Subject
14from ndg.xacml.core.resource import Resource
15from ndg.xacml.core.action import Action
16from ndg.xacml.core.environment import Environment
17from ndg.xacml.parsers import XMLParseError
18from ndg.xacml.parsers.etree import QName
19from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
20from ndg.xacml.parsers.etree.factory import ReaderFactory
21
22
23class TargetReader(ETreeAbstractReader):
24    """ElementTree based parser for XACML Target elements
25   
26    @cvar TYPE: XACML type to instantiate from parsed object
27    @type string: type"""
28    TYPE = Target
29   
30    def __call__(self, obj):
31        elem = super(TargetReader, self)._parse(obj)
32       
33        xacmlType = TargetReader.TYPE
34        target = xacmlType()
35       
36        localName = QName.getLocalPart(elem.tag)
37        if localName != xacmlType.ELEMENT_LOCAL_NAME:
38            raise XMLParseError('No "%s" element found' % 
39                                xacmlType.ELEMENT_LOCAL_NAME)
40       
41        # Parse sub-elements
42        for childElem in elem:
43            localName = QName.getLocalPart(childElem.tag)
44           
45            if localName == xacmlType.SUBJECTS_ELEMENT_LOCAL_NAME:
46                for subjElem in childElem:
47                    SubjectReader = ReaderFactory.getReader(Subject)
48                    target.subjects.append(SubjectReader.parse(subjElem))
49                               
50            elif localName == xacmlType.RESOURCES_ELEMENT_LOCAL_NAME:
51                for resourceElem in childElem:
52                    ResourceReader = ReaderFactory.getReader(Resource)
53                    target.resources.append(ResourceReader.parse(resourceElem))
54               
55            elif localName == xacmlType.ACTIONS_ELEMENT_LOCAL_NAME:
56                for targetElem in childElem:
57                    ActionReader = ReaderFactory.getReader(Action)
58                    target.actions.append(ActionReader.parse(targetElem))
59               
60            elif localName == xacmlType.ENVIRONMENTS_ELEMENT_LOCAL_NAME:
61                for environElem in childElem:
62                    EnvironmentReader = ReaderFactory.getReader(Environment)
63                    target.environments.append(EnvironmentReader.parse(
64                                                                environElem))
65            else:
66                raise XMLParseError("XACML Target child element name %r not "
67                                    "recognised" % localName)
68               
69        return target
Note: See TracBrowser for help on using the repository browser.