• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/ZSI/wstools/XMLname.py

00001 """Translate strings to and from SOAP 1.2 XML name encoding
00002 
00003 Implements rules for mapping application defined name to XML names
00004 specified by the w3 SOAP working group for SOAP version 1.2 in
00005 Appendix A of "SOAP Version 1.2 Part 2: Adjuncts", W3C Working Draft
00006 17, December 2001, <http://www.w3.org/TR/soap12-part2/#namemap>
00007 
00008 Also see <http://www.w3.org/2000/xp/Group/xmlp-issues>.
00009 
00010 Author: Gregory R. Warnes <Gregory.R.Warnes@Pfizer.com>
00011 Date::  2002-04-25
00012 Version 0.9.0
00013 
00014 """
00015 
00016 ident = "$Id: XMLname.py 954 2005-02-16 14:45:37Z warnes $"
00017 
00018 from re import *
00019 
00020 
00021 def _NCNameChar(x):
00022     return x.isalpha() or x.isdigit() or x=="." or x=='-' or x=="_" 
00023 
00024 
00025 def _NCNameStartChar(x):
00026     return x.isalpha() or x=="_" 
00027 
00028 
00029 def _toUnicodeHex(x):
00030     hexval = hex(ord(x[0]))[2:]
00031     hexlen = len(hexval)
00032     # Make hexval have either 4 or 8 digits by prepending 0's
00033     if   (hexlen==1): hexval = "000" + hexval
00034     elif (hexlen==2): hexval = "00"  + hexval
00035     elif (hexlen==3): hexval = "0"   + hexval
00036     elif (hexlen==4): hexval = ""    + hexval
00037     elif (hexlen==5): hexval = "000" + hexval
00038     elif (hexlen==6): hexval = "00"  + hexval
00039     elif (hexlen==7): hexval = "0"   + hexval
00040     elif (hexlen==8): hexval = ""    + hexval    
00041     else: raise Exception, "Illegal Value returned from hex(ord(x))"
00042     
00043     return "_x"+ hexval + "_"
00044 
00045 
00046 def _fromUnicodeHex(x):
00047     return eval( r'u"\u'+x[2:-1]+'"' ) 
00048 
00049 
00050 def toXMLname(string):
00051     """Convert string to a XML name."""
00052     if string.find(':') != -1 :
00053         (prefix, localname) = string.split(':',1)
00054     else:
00055         prefix = None
00056         localname = string
00057     
00058     T = unicode(localname)
00059 
00060     N = len(localname)
00061     X = [];
00062     for i in range(N) :
00063         if i< N-1 and T[i]==u'_' and T[i+1]==u'x':
00064             X.append(u'_x005F_')
00065         elif i==0 and N >= 3 and \
00066                  ( T[0]==u'x' or T[0]==u'X' ) and \
00067                  ( T[1]==u'm' or T[1]==u'M' ) and \
00068                  ( T[2]==u'l' or T[2]==u'L' ):
00069             X.append(u'_xFFFF_' + T[0])
00070         elif (not _NCNameChar(T[i])) or (i==0 and not _NCNameStartChar(T[i])):
00071             X.append(_toUnicodeHex(T[i]))
00072         else:
00073             X.append(T[i])
00074     
00075     if prefix:
00076         return "%s:%s" % (prefix, u''.join(X))
00077     return u''.join(X)
00078 
00079 
00080 def fromXMLname(string):
00081     """Convert XML name to unicode string."""
00082 
00083     retval = sub(r'_xFFFF_','', string )
00084 
00085     def fun( matchobj ):
00086         return _fromUnicodeHex( matchobj.group(0) )
00087 
00088     retval = sub(r'_x[0-9A-Za-z]+_', fun, retval )
00089         
00090     return retval

Generated on Wed Oct 20 2010 11:12:16 for APBS by  doxygen 1.7.2