1 """Translate strings to and from SOAP 1.2 XML name encoding
3 Implements rules for mapping application defined name to XML names
4 specified by the w3 SOAP working group for SOAP version 1.2 in
5 Appendix A of "SOAP Version 1.2 Part 2: Adjuncts", W3C Working Draft
6 17, December 2001, <http://www.w3.org/TR/soap12-part2/#namemap>
8 Also see <http://www.w3.org/2000/xp/Group/xmlp-issues>.
10 Author: Gregory R. Warnes <Gregory.R.Warnes@Pfizer.com>
22 return x.isalpha() or x.isdigit() or x=="." or x=='-' or x=="_"
25 def _NCNameStartChar(x):
26 return x.isalpha() or x=="_"
30 hexval = hex(ord(x[0]))[2:]
32 # Make hexval have either 4 or 8 digits by prepending 0's
33 if (hexlen==1): hexval = "000" + hexval
34 elif (hexlen==2): hexval = "00" + hexval
35 elif (hexlen==3): hexval = "0" + hexval
36 elif (hexlen==4): hexval = "" + hexval
37 elif (hexlen==5): hexval = "000" + hexval
38 elif (hexlen==6): hexval = "00" + hexval
39 elif (hexlen==7): hexval = "0" + hexval
40 elif (hexlen==8): hexval = "" + hexval
41 else: raise Exception, "Illegal Value returned from hex(ord(x))"
43 return "_x"+ hexval + "_"
46 def _fromUnicodeHex(x):
47 return eval( r'u"\u'+x[2:-1]+'"' )
50 def toXMLname(string):
51 """Convert string to a XML name."""
52 if string.find(':') != -1 :
53 (prefix, localname) = string.split(':',1)
58 T = unicode(localname)
63 if i< N-1 and T[i]==u'_' and T[i+1]==u'x':
65 elif i==0 and N >= 3 and \
66 ( T[0]==u'x' or T[0]==u'X' ) and \
67 ( T[1]==u'm' or T[1]==u'M' ) and \
68 ( T[2]==u'l' or T[2]==u'L' ):
69 X.append(u'_xFFFF_' + T[0])
70 elif (not _NCNameChar(T[i])) or (i==0 and not _NCNameStartChar(T[i])):
71 X.append(_toUnicodeHex(T[i]))
76 return "%s:%s" % (prefix, u''.join(X))
80 def fromXMLname(string):
81 """Convert XML name to unicode string."""
83 retval = sub(r'_xFFFF_','', string )
86 return _fromUnicodeHex( matchobj.group(0) )
88 retval = sub(r'_x[0-9A-Za-z]+_', fun, retval )