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

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

Parsing functionality near complete for rule1.xml

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