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.
 
 
 

100 lines
2.5 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@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):
  22. hexval = "000" + hexval
  23. elif (hexlen == 2):
  24. hexval = "00" + hexval
  25. elif (hexlen == 3):
  26. hexval = "0" + hexval
  27. elif (hexlen == 4):
  28. hexval = "" + hexval
  29. elif (hexlen == 5):
  30. hexval = "000" + hexval
  31. elif (hexlen == 6):
  32. hexval = "00" + hexval
  33. elif (hexlen == 7):
  34. hexval = "0" + hexval
  35. elif (hexlen == 8):
  36. hexval = "" + hexval
  37. else:
  38. raise Exception("Illegal Value returned from hex(ord(x))")
  39. return "_x" + hexval + "_"
  40. def _fromUnicodeHex(x):
  41. return eval(r'u"\u' + x[2:-1] + '"')
  42. def toXMLname(string):
  43. """Convert string to a XML name."""
  44. if string.find(':') != -1:
  45. (prefix, localname) = string.split(':', 1)
  46. else:
  47. prefix = None
  48. localname = string
  49. T = unicode(localname)
  50. N = len(localname)
  51. X = []
  52. for i in range(N):
  53. if i < N - 1 and T[i] == u'_' and T[i + 1] == u'x':
  54. X.append(u'_x005F_')
  55. elif i == 0 and N >= 3 and \
  56. (T[0] == u'x' or T[0] == u'X') and \
  57. (T[1] == u'm' or T[1] == u'M') and \
  58. (T[2] == u'l' or T[2] == u'L'):
  59. X.append(u'_xFFFF_' + T[0])
  60. elif (not _NCNameChar(T[i])) or (i == 0 and not _NCNameStartChar(T[i])):
  61. X.append(_toUnicodeHex(T[i]))
  62. else:
  63. X.append(T[i])
  64. if prefix:
  65. return "%s:%s" % (prefix, u''.join(X))
  66. return u''.join(X)
  67. def fromXMLname(string):
  68. """Convert XML name to unicode string."""
  69. retval = sub(r'_xFFFF_', '', string)
  70. def fun(matchobj):
  71. return _fromUnicodeHex(matchobj.group(0))
  72. retval = sub(r'_x[0-9A-Fa-f]{4}_', fun, retval)
  73. return retval