<s> """<STR_LIT>""" <EOL> ident = "<STR_LIT>" <EOL> from re import sub <EOL> def _NCNameChar ( x ) : <EOL> return x . isalpha ( ) or x . isdigit ( ) or x == "<STR_LIT:.>" or x == '<STR_LIT:->' or x == "<STR_LIT:_>" <EOL> def _NCNameStartChar ( x ) : <EOL> return x . isalpha ( ) or x == "<STR_LIT:_>" <EOL> def _toUnicodeHex ( x ) : <EOL> hexval = hex ( ord ( x [ <NUM_LIT:0> ] ) ) [ <NUM_LIT:2> : ] <EOL> hexlen = len ( hexval ) <EOL> if ( hexlen == <NUM_LIT:1> ) : hexval = "<STR_LIT>" + hexval <EOL> elif ( hexlen == <NUM_LIT:2> ) : hexval = "<STR_LIT>" + hexval <EOL> elif ( hexlen == <NUM_LIT:3> ) : hexval = "<STR_LIT:0>" + hexval <EOL> elif ( hexlen == <NUM_LIT:4> ) : hexval = "<STR_LIT>" + hexval <EOL> elif ( hexlen == <NUM_LIT:5> ) : hexval = "<STR_LIT>" + hexval <EOL> elif ( hexlen == <NUM_LIT:6> ) : hexval = "<STR_LIT>" + hexval <EOL> elif ( hexlen == <NUM_LIT:7> ) : hexval = "<STR_LIT:0>" + hexval <EOL> elif ( hexlen == <NUM_LIT:8> ) : hexval = "<STR_LIT>" + hexval <EOL> else : raise Exception ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" + hexval + "<STR_LIT:_>" <EOL> def _fromUnicodeHex ( x ) : <EOL> return eval ( r'<STR_LIT>' + x [ <NUM_LIT:2> : - <NUM_LIT:1> ] + '<STR_LIT:">' ) <EOL> def toXMLname ( string ) : <EOL> """<STR_LIT>""" <EOL> if string . find ( '<STR_LIT::>' ) != - <NUM_LIT:1> : <EOL> ( prefix , localname ) = string . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> else : <EOL> prefix = None <EOL> localname = string <EOL> T = unicode ( localname ) <EOL> N = len ( localname ) <EOL> X = [ ] ; <EOL> for i in range ( N ) : <EOL> if i < N - <NUM_LIT:1> and T [ i ] == u'<STR_LIT:_>' and T [ i + <NUM_LIT:1> ] == u'<STR_LIT:x>' : <EOL> X . append ( u'<STR_LIT>' ) <EOL> elif i == <NUM_LIT:0> and N >= <NUM_LIT:3> and ( T [ <NUM_LIT:0> ] == u'<STR_LIT:x>' or T [ <NUM_LIT:0> ] == u'<STR_LIT:X>' ) and ( T [ <NUM_LIT:1> ] == u'<STR_LIT:m>' or T [ <NUM_LIT:1> ] == u'<STR_LIT:M>' ) and ( T [ <NUM_LIT:2> ] == u'<STR_LIT:l>' or T [ <NUM_LIT:2> ] == u'<STR_LIT:L>' ) : <EOL> X . append ( u'<STR_LIT>' + T [ <NUM_LIT:0> ] ) <EOL> elif ( not _NCNameChar ( T [ i ] ) ) or ( i == <NUM_LIT:0> and not _NCNameStartChar ( T [ i ] ) ) : <EOL> X . append ( _toUnicodeHex ( T [ i ] ) ) <EOL> else : <EOL> X . append ( T [ i ] ) <EOL> if prefix : <EOL> return "<STR_LIT>" % ( prefix , u'<STR_LIT>' . join ( X ) ) <EOL> return u'<STR_LIT>' . join ( X ) <EOL> def fromXMLname ( string ) : <EOL> """<STR_LIT>""" <EOL> retval = sub ( r'<STR_LIT>' , '<STR_LIT>' , string ) <EOL> def fun ( matchobj ) : <EOL> return _fromUnicodeHex ( matchobj . group ( <NUM_LIT:0> ) ) <EOL> retval = sub ( r'<STR_LIT>' , fun , retval ) <EOL> return retval </s>
