1############################################################################## 2## Name: parse_dtd.py 3## Purpose: parses dtd file -> makes maps to get dtd names from idl class 4## names or the other way round 5## Author: Alex Thuering 6## Created: 2005/01/19 7## RCS-ID: $Id: parse_dtd.py,v 1.4 2014/03/21 21:15:35 ntalex Exp $ 8## Copyright: (c) 2005 Alex Thuering 9## Notes: It must be rewritten, because can't parse dtd file from svg 1.1 10## Now it use dtd file from svg 1.0 11## -> xmlto library can be used to parse dtd 12############################################################################## 13 14import re, string 15import parse_attr 16import os.path 17import conf 18 19pathtosvg=os.path.expanduser(conf.share_dir + "/svg.dtd") 20 21f=open(pathtosvg,'r') 22content=f.read() 23 24# mmm... they should not be different, especially with a so thin 25# difference: there is an \s before the > in the second case... 26 27entity_type_decl_re = re.compile('<!ENTITY\s+%\s+([\w\-_]+)\s"([^"]+)"\s*>') 28entity_common_attr_re = re.compile('<!ENTITY\s+%\s+([\w\-_]+)\s+"([^"]+)"\s>', re.MULTILINE) 29 30element_re = re.compile('<!ELEMENT\s+([\w\-_]+)\s+([^>]+)>', re.MULTILINE) 31attlist_re = re.compile('<!ATTLIST\s+([\w\-_]+)\s+([^>]+)>', re.MULTILINE) 32 33entity_type_decls={} 34entity_common_attrs={} 35elements={} 36attlists={} 37attlists["tbreak"] = "" 38 39beg=0 40while 1: 41 m = entity_type_decl_re.search(content, beg) 42 if m==None: 43 break 44 beg = m.end() 45 name = m.group(1) 46 defi = m.group(2) 47 48 if defi[0]=='(': 49 enums, pos = parse_attr.get_enums(defi,1) 50 theattr = parse_attr.attr_named_enum(name, enums) 51 else: 52 theattr = parse_attr.attr_named_simple_type(name,defi) 53 54 entity_type_decls[name]=theattr 55 56 57beg=0 58while 1: 59 m = entity_common_attr_re.search(content, beg) 60 if m==None: 61 break 62 beg = m.end() 63 name = m.group(1).strip() 64 defi = m.group(2) 65 attributes=parse_attr.parse_attr(defi) 66 entity_common_attrs[name]=attributes 67 68 69beg=0 70while 1: 71 m = element_re.search(content, beg) 72 if m==None: 73 break 74 beg = m.end() 75 76 name = m.group(1).strip() 77 elements[name]=m.group(2) 78 79beg=0 80while 1: 81 m = attlist_re.search(content, beg) 82 if m==None: 83 break 84 beg = m.end() 85 86 name = m.group(1).strip() 87 defi = m.group(2) 88 89 attributes=parse_attr.parse_attr(defi) 90 attlists[name] = attributes 91 92 continue 93 print('---') 94 print(name) 95 for i in attributes: 96 l = i.expand(entity_type_decls, entity_common_attrs) 97 for j in l: 98 print(j) 99 100