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.
 
 
 

101 lines
2.5 KiB

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