warn user about bitcoin connection slots if p2p connect takes longer than 5 seconds
[p2pool.git] / wstools / XMLname.py
1 """Translate strings to and from SOAP 1.2 XML name encoding
2
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>
7
8 Also see <http://www.w3.org/2000/xp/Group/xmlp-issues>.
9
10 Author: Gregory R. Warnes <Gregory.R.Warnes@Pfizer.com>
11 Date::  2002-04-25
12 Version 0.9.0
13
14 """
15
16 ident = "$Id$"
17
18 from re import *
19
20
21 def _NCNameChar(x):
22     return x.isalpha() or x.isdigit() or x=="." or x=='-' or x=="_" 
23
24
25 def _NCNameStartChar(x):
26     return x.isalpha() or x=="_" 
27
28
29 def _toUnicodeHex(x):
30     hexval = hex(ord(x[0]))[2:]
31     hexlen = len(hexval)
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))"
42     
43     return "_x"+ hexval + "_"
44
45
46 def _fromUnicodeHex(x):
47     return eval( r'u"\u'+x[2:-1]+'"' ) 
48
49
50 def toXMLname(string):
51     """Convert string to a XML name."""
52     if string.find(':') != -1 :
53         (prefix, localname) = string.split(':',1)
54     else:
55         prefix = None
56         localname = string
57     
58     T = unicode(localname)
59
60     N = len(localname)
61     X = [];
62     for i in range(N) :
63         if i< N-1 and T[i]==u'_' and T[i+1]==u'x':
64             X.append(u'_x005F_')
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]))
72         else:
73             X.append(T[i])
74     
75     if prefix:
76         return "%s:%s" % (prefix, u''.join(X))
77     return u''.join(X)
78
79
80 def fromXMLname(string):
81     """Convert XML name to unicode string."""
82
83     retval = sub(r'_xFFFF_','', string )
84
85     def fun( matchobj ):
86         return _fromUnicodeHex( matchobj.group(0) )
87
88     retval = sub(r'_x[0-9A-Za-z]+_', fun, retval )
89         
90     return retval