Module EDUtilsXML
[hide private]
[frames] | no frames]

Source Code for Module EDUtilsXML

  1  # 
  2  #    Project: The EDNA Kernel 
  3  #             http://www.edna-site.org 
  4  # 
  5  #    File: "$Id$" 
  6  # 
  7  #    Copyright (C) 2008-2009 Diamond Light Source 
  8  #                            Chilton, Didcot, UK 
  9  # 
 10  #    Principal author:       Karl Levik (karl.levik@diamond.ac.uk) 
 11  # 
 12  #    Contributing authors:   Marie-Francoise Incardona (incardon@esrf.fr) 
 13  #                            Olof Svensson (svensson@esrf.fr)  
 14  # 
 15  #    This program is free software: you can redistribute it and/or modify 
 16  #    it under the terms of the GNU Lesser General Public License as published 
 17  #    by the Free Software Foundation, either version 3 of the License, or 
 18  #    (at your option) any later version. 
 19  # 
 20  #    This program is distributed in the hope that it will be useful, 
 21  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 22  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 23  #    GNU Lesser General Public License for more details. 
 24  # 
 25  #    You should have received a copy of the GNU General Public License 
 26  #    and the GNU Lesser General Public License  along with this program.   
 27  #    If not, see <http://www.gnu.org/licenses/>. 
 28  # 
 29   
 30  __authors__ = [ "Karl Levik", "Marie-Francoise Incardona", "Olof Svensson" ] 
 31  __contact__ = "karl.levik@diamond.ac.uk" 
 32  __license__ = "LGPLv3+" 
 33  __copyright__ = "Diamond Light Source, Chilton, Didcot, UK" 
 34   
 35  import StringIO, re, xml.dom.minidom 
36 37 38 -class EDUtilsXML:
39 40 @staticmethod
41 - def dnaMarshal(_xsDataObject=None):
42 """ 43 Returns a DNA compatible XML representation of an object derived from XSData. 44 DNA compatible XML has no whitespace or newlines etc. between elements and single-quotes must be escaped. 45 In addition, booleans must be either "True" or "False". Anything but "True" is interpreted as "False". 46 """ 47 stringIO = StringIO.StringIO() 48 stringIO.write('<?xml version="1.0" encoding="ISO-8859-1"?>') 49 50 regexp = re.compile('XSDataISPyB(.*)') 51 matchObject = regexp.match(_xsDataObject.__class__.__name__) 52 strDNAClassName = matchObject.group(1) 53 54 _xsDataObject.export(stringIO, 0, name_=strDNAClassName) 55 strXML = stringIO.getvalue() 56 stringIO.close() 57 58 # Remove whitespace and <value> & </value> taqs, escape single-quotes etc 59 strParsed = xml.dom.minidom.parseString(strXML) 60 regexpXMLHeader = re.compile('\<\?xml version="1.0" \?\>(.*)') 61 62 regexpValueTags = re.compile('\<value\>(.*)\</value\>') 63 regexpOtherTag = re.compile('\<(.*)\>') 64 strXML = '' 65 bIsBoolean = False 66 67 for strLine in strParsed.toxml().splitlines(): 68 if strLine.startswith('<?xml version="1.0" ?>'): 69 strXML += '<?xml version="1.0" ?>' 70 strLine = strLine[22:].strip() 71 if len(strLine) == 0: 72 continue 73 if strLine.startswith('</%s>' % strDNAClassName): 74 strXML += '</%s>' % strDNAClassName 75 break 76 if strLine.startswith('<%s>' % strDNAClassName): 77 strXML += '<%s>' % strDNAClassName 78 strLine = strLine[len(strDNAClassName) + 2:] 79 if len(strLine) == 0: 80 continue 81 82 m = regexpValueTags.match(strLine.strip()) 83 if m != None: 84 strValue = m.group(1) 85 strValue = strValue.replace('\'', '\\\'') 86 if bIsBoolean: 87 strValue = strValue.replace('1', 'True') 88 strXML += strValue 89 else: 90 strXML += strLine.strip() 91 m2 = regexpOtherTag.match(strLine.strip()) 92 if m2 != None: 93 strValue = m2.group(1) 94 if not strValue.startswith("/"): 95 method = getattr(_xsDataObject, 'get%s' % EDUtilsXML.capitalizeFirstLetter(strValue)) 96 o = method() 97 if o.__class__.__name__ == 'XSDataBoolean': 98 bIsBoolean = True 99 else: 100 bIsBoolean = False 101 return strXML
102 103 104 @staticmethod
105 - def capitalizeFirstLetter(_str):
106 """ 107 Capitalizes the first letter of a string, leaving the rest untouched 108 """ 109 return (_str[0].capitalize() + _str[1:])
110