Package ndg :: Package xacml :: Package parsers :: Package etree :: Module conditionreader
[hide private]

Source Code for Module ndg.xacml.parsers.etree.conditionreader

 1  """NDG XACML ElementTree based Target Element reader  
 2   
 3  NERC DataGrid 
 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: conditionreader.py 8028 2012-02-27 14:38:01Z rwilkinson $" 
12  from ndg.xacml.core.condition import Condition 
13  from ndg.xacml.parsers import XMLParseError 
14  from ndg.xacml.parsers.etree import QName, getElementChildren 
15  from ndg.xacml.parsers.etree.reader import ETreeAbstractReader 
16  from ndg.xacml.parsers.etree.applyreader import ApplyReader 
17   
18   
19 -class ConditionReader(ETreeAbstractReader):
20 '''ElementTree based XACML 2.0 Condition parser. Note the difference to 21 XACML 1.0: the Condition element is its own type and not an Apply type. 22 It expects a single Expression derived type child element 23 24 @cvar TYPE: XACML type to instantiate from parsed object 25 @type TYPE: type 26 ''' 27 TYPE = Condition 28
29 - def __call__(self, obj, common):
30 """Parse condition object 31 32 @param obj: input object to parse 33 @type obj: ElementTree Element, or stream object 34 @return: new XACML condition instance 35 @rtype: ndg.xacml.core.condition.Condition 36 @raise XMLParseError: error reading sub-elements 37 """ 38 elem = super(ConditionReader, self)._parse(obj) 39 40 xacmlType = self.__class__.TYPE 41 condition = xacmlType() 42 43 localName = QName.getLocalPart(elem.tag) 44 if localName != xacmlType.ELEMENT_LOCAL_NAME: 45 raise XMLParseError("No \"%s\" element found" % 46 xacmlType.ELEMENT_LOCAL_NAME) 47 48 # Parse sub-elements 49 nSubElem = 0 50 for childElem in getElementChildren(elem): 51 nSubElem += 1 52 subElem = childElem 53 54 # Parse expression sub-element 55 if nSubElem != 1: 56 raise XMLParseError('XACML 2.0 policy schema expects only one ' 57 'expression sub-element in the Condition ' 58 'element; policy file has %d' % nSubElem) 59 60 subElemlocalName = QName.getLocalPart(subElem.tag) 61 if subElemlocalName == xacmlType.APPLY_ELEMENT_LOCAL_NAME: 62 condition.expression = ApplyReader.parse(subElem, common) 63 else: 64 raise XMLParseError('Expecting %r Condition sub-element not ' 65 'recognised' % 66 xacmlType.EXPRESSION_ELEMENT_LOCAL_NAME) 67 68 69 70 return condition
71