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.
 
 
 

89 lines
2.4 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. ident = "$Id$"
  12. from re import *
  13. def _NCNameChar(x):
  14. return x.isalpha() or x.isdigit() or x=="." or x=='-' or x=="_"
  15. def _NCNameStartChar(x):
  16. return x.isalpha() or x=="_"
  17. def _toUnicodeHex(x):
  18. hexval = hex(ord(x[0]))[2:]
  19. hexlen = len(hexval)
  20. # Make hexval have either 4 or 8 digits by prepending 0's
  21. if (hexlen==1): hexval = "000" + hexval
  22. elif (hexlen==2): hexval = "00" + hexval
  23. elif (hexlen==3): hexval = "0" + hexval
  24. elif (hexlen==4): hexval = "" + hexval
  25. elif (hexlen==5): hexval = "000" + hexval
  26. elif (hexlen==6): hexval = "00" + hexval
  27. elif (hexlen==7): hexval = "0" + hexval
  28. elif (hexlen==8): hexval = "" + hexval
  29. else: raise Exception, "Illegal Value returned from hex(ord(x))"
  30. return "_x"+ hexval + "_"
  31. def _fromUnicodeHex(x):
  32. return eval( r'u"\u'+x[2:-1]+'"' )
  33. def toXMLname(string):
  34. """Convert string to a XML name."""
  35. if string.find(':') != -1 :
  36. (prefix, localname) = string.split(':',1)
  37. else:
  38. prefix = None
  39. localname = string
  40. T = unicode(localname)
  41. N = len(localname)
  42. X = [];
  43. for i in range(N) :
  44. if i< N-1 and T[i]==u'_' and T[i+1]==u'x':
  45. X.append(u'_x005F_')
  46. elif i==0 and N >= 3 and \
  47. ( T[0]==u'x' or T[0]==u'X' ) and \
  48. ( T[1]==u'm' or T[1]==u'M' ) and \
  49. ( T[2]==u'l' or T[2]==u'L' ):
  50. X.append(u'_xFFFF_' + T[0])
  51. elif (not _NCNameChar(T[i])) or (i==0 and not _NCNameStartChar(T[i])):
  52. X.append(_toUnicodeHex(T[i]))
  53. else:
  54. X.append(T[i])
  55. return u''.join(X)
  56. def fromXMLname(string):
  57. """Convert XML name to unicode string."""
  58. retval = sub(r'_xFFFF_','', string )
  59. def fun( matchobj ):
  60. return _fromUnicodeHex( matchobj.group(0) )
  61. retval = sub(r'_x[0-9A-Za-z]+_', fun, retval )
  62. return retval