You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
2.3 KiB

  1. """Translate strings to and from SOAP 1.2 XML name encoding
  2. Implements rules for mapping application defined name to XML names
  3. specified by the w3 SOAP working group for SOAP version 1.2 in
  4. Appendix A of "SOAP Version 1.2 Part 2: Adjuncts", W3C Working Draft
  5. 17, December 2001, <http://www.w3.org/TR/soap12-part2/#namemap>
  6. Also see <http://www.w3.org/2000/xp/Group/xmlp-issues>.
  7. Author: Gregory R. Warnes <gregory_r_warnes@groton.pfizer.com>
  8. Date:: 2002-04-25
  9. Version 0.9.0
  10. """
  11. from re import *
  12. def _NCNameChar(x):
  13. return x.isalpha() or x.isdigit() or x=="." or x=='-' or x=="_"
  14. def _NCNameStartChar(x):
  15. return x.isalpha() or x=="_"
  16. def _toUnicodeHex(x):
  17. hexval = hex(ord(x[0]))[2:]
  18. hexlen = len(hexval)
  19. # Make hexval have either 4 or 8 digits by prepending 0's
  20. if (hexlen==1): hexval = "000" + hexval
  21. elif (hexlen==2): hexval = "00" + hexval
  22. elif (hexlen==3): hexval = "0" + hexval
  23. elif (hexlen==4): hexval = "" + hexval
  24. elif (hexlen==5): hexval = "000" + hexval
  25. elif (hexlen==6): hexval = "00" + hexval
  26. elif (hexlen==7): hexval = "0" + hexval
  27. elif (hexlen==8): hexval = "" + hexval
  28. else: raise Exception, "Illegal Value returned from hex(ord(x))"
  29. return "_x"+ hexval + "_"
  30. def _fromUnicodeHex(x):
  31. return eval( r'u"\u'+x[2:-1]+'"' )
  32. def toXMLname(string):
  33. """Convert string to a XML name."""
  34. if string.find(':') != -1 :
  35. (prefix, localname) = string.split(':',1)
  36. else:
  37. prefix = None
  38. localname = string
  39. T = unicode(localname)
  40. N = len(localname)
  41. X = [];
  42. for i in range(N) :
  43. if i< N-1 and T[i]==u'_' and T[i+1]==u'x':
  44. X.append(u'_x005F_')
  45. elif i==0 and N >= 3 and \
  46. ( T[0]==u'x' or T[0]==u'X' ) and \
  47. ( T[1]==u'm' or T[1]==u'M' ) and \
  48. ( T[2]==u'l' or T[2]==u'L' ):
  49. X.append(u'_xFFFF_' + T[0])
  50. elif (not _NCNameChar(T[i])) or (i==0 and not _NCNameStartChar(T[i])):
  51. X.append(_toUnicodeHex(T[i]))
  52. else:
  53. X.append(T[i])
  54. return u''.join(X)
  55. def fromXMLname(string):
  56. """Convert XML name to unicode string."""
  57. retval = sub(r'_xFFFF_','', string )
  58. def fun( matchobj ):
  59. return _fromUnicodeHex( matchobj.group(0) )
  60. retval = sub(r'_x[0-9A-Za-z]+_', fun, retval )
  61. return retval