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

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

Parsing functionality near complete for rule1.xml

Line 
1'''
2Created on 15 Mar 2010
3
4@author: pjkersha
5'''
6import logging
7log = logging.getLogger(__name__)
8"""NDG XACML ElementTree reader module containing reader base class
9
10NERC DataGrid Project
11"""
12__author__ = "P J Kershaw"
13__date__ = "16/03/10"
14__copyright__ = "(C) 2010 Science and Technology Facilities Council"
15__contact__ = "Philip.Kershaw@stfc.ac.uk"
16__license__ = "BSD - see LICENSE file in top-level directory"
17__contact__ = "Philip.Kershaw@stfc.ac.uk"
18__revision__ = "$Id: $"
19from xml.etree import ElementTree
20
21from ndg.xacml.core import PolicyComponent
22from ndg.xacml.parsers import AbstractReader
23
24
25class ETreeAbstractReader(AbstractReader):
26    """Base class for ElementTree implementation of XACML reader
27   
28    @cvar TYPE: XACML type to instantiate from parsed object
29    @type string: type"""
30    TYPE = None
31   
32    def __init__(self):
33        if self.__class__.TYPE is None:
34            raise NotImplementedError('No "TYPE" class variable set to specify '
35                                      'the XACML type to instantiate')
36           
37        self.__namespace_map_backup = ElementTree._namespace_map.copy()
38        ElementTree._namespace_map[''] = PolicyComponent.XACML_2_0_XMLNS
39       
40    def __del__(self):
41        """Restore original global namespace map"""
42        ElementTree._namespace_map = self.__namespace_map_backup
43   
44    @staticmethod
45    def _parse(obj):
46        """Parse helper method
47        @param obj: input object to parse
48        @type obj: ElementTree Element, or stream object
49        @return: ElementTree element
50        @rtype: xml.etree.Element
51        """
52        if ElementTree.iselement(obj):
53            elem = obj
54        else:
55            # Treat as stream object
56            elem = ElementTree.parse(obj).getroot()
57           
58        return elem
59         
60# Set up new class as an abstract base itself             
61AbstractReader.register(ETreeAbstractReader)
62
Note: See TracBrowser for help on using the repository browser.