home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / syntax / xml_names.py < prev   
Encoding:
Python Source  |  2007-04-04  |  2.8 KB  |  86 lines

  1. # From: http://www.w3.org/TR/REC-xml#NT-CombiningChar
  2. #
  3. # * Name start characters must have one of the categories Ll, Lu, Lo,
  4. #   Lt, Nl.
  5. #
  6. # * Name characters other than Name-start characters must have one of
  7. #   the categories Mc, Me, Mn, Lm, or Nd.
  8. #
  9. # * Characters in the compatibility area (i.e. with character code
  10. #   greater than #xF900 and less than #xFFFE) are not allowed in XML
  11. #   names.
  12. #
  13. # * Characters which have a font or compatibility decomposition
  14. #   (i.e. those with a "compatibility formatting tag" in field 5 of the
  15. #   database -- marked by field 5 beginning with a "<") are not allowed.
  16. #
  17. # * The following characters are treated as name-start characters rather
  18. #   than name characters, because the property file classifies them as
  19. #   Alphabetic: [#x02BB-#x02C1], #x0559, #x06E5, #x06E6.
  20. #
  21. # * Characters #x20DD-#x20E0 are excluded (in accordance with Unicode
  22. #   2.0, section 5.14).
  23. #
  24. # * Character #x00B7 is classified as an extender, because the property
  25. #   list so identifies it.
  26. #
  27. # * Character #x0387 is added as a name character, because #x00B7 is its
  28. #   canonical equivalent.
  29. #
  30. # * Characters ':' and '_' are allowed as name-start characters.
  31. #
  32. # * Characters '-' and '.' are allowed as name characters.
  33.  
  34. from unicodedata import category, decomposition
  35.  
  36. NAME_START_CATEGORIES = ["Ll", "Lu", "Lo", "Lt", "Nl"]
  37. NAME_CATEGORIES = NAME_START_CATEGORIES + ["Mc", "Me", "Mn", "Lm", "Nd"]
  38. ALLOWED_NAME_CHARS = [u"\u00B7", u"\u0387", u"-", u".", u"_"]
  39.  
  40. # http://www.w3.org/TR/REC-xml-names/#NT-NCName
  41. #  [4] NCName ::= (Letter | '_') (NCNameChar)* /* An XML Name, minus
  42. #      the ":" */
  43. #  [5] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar
  44. #      | Extender
  45.  
  46. def is_ncname(name):
  47.     first = name[0]
  48.     if first=="_" or category(first) in NAME_START_CATEGORIES:
  49.         for i in xrange(1, len(name)):
  50.             c = name[i]
  51.             if not category(c) in NAME_CATEGORIES:
  52.                 if c in ALLOWED_NAME_CHARS:
  53.                     continue
  54.                 return 0
  55.             #if in compatibility area
  56.             #if decomposition(c)!='':
  57.             #    return 0
  58.  
  59.         return 1
  60.     else:
  61.         return 0
  62.  
  63. XMLNS = "http://www.w3.org/XML/1998/namespace"
  64.  
  65. def split_uri(uri):
  66.     if uri.startswith(XMLNS):
  67.         return (XMLNS, uri.split(XMLNS)[1])
  68.     length = len(uri)
  69.     for i in xrange(0, length):
  70.         c = uri[-i-1]
  71.         if not category(c) in NAME_CATEGORIES:
  72.             if c in ALLOWED_NAME_CHARS:
  73.                 continue
  74.             for j in xrange(-1-i, length):
  75.                 if category(uri[j]) in NAME_START_CATEGORIES or uri[j]=="_":
  76.                     ns = uri[:j]
  77.                     if not ns:
  78.                         break
  79.                     ln = uri[j:]
  80.                     return (ns, ln)
  81.             break
  82.     raise Exception("Can't split '%s'" % uri)
  83.  
  84.  
  85.  
  86.