00001
00002
00003 class Comment:
00004 def __init__(self,comment=""):
00005 self.comment = comment
00006
00007 def xml(self,fo,pre):
00008 lines = self.comment.split('\n')
00009 if 1 == len(lines) and len(lines[0]) <= 70:
00010 fo.write("%s<!-- %s -->\n"%(pre,lines[0]))
00011 return
00012 fo.write("%s<!--\n"%pre)
00013 for line in self.comment.split('\n'):
00014 fo.write(pre+' '+line+'\n')
00015 fo.write("%s-->\n"%pre)
00016 return
00017 class ExternalEntity:
00018 def __init__(self,entity=""):
00019 self.entity = entity
00020
00021 def set_path(self,base):
00022 pass
00023 def xml(self,fo,pre):
00024 fo.write("%s&%s;\n"%(pre,self.entity))
00025 return
00026
00027
00028 class XmlFile:
00029 def __init__(self,dtd=None,objects=[],external_entities=[]):
00030 "XmlFile. external entities are (name,file) tuples"
00031 self.dtd = dtd
00032 self.objects = objects
00033 self.external_entities = external_entities
00034
00035 return
00036
00037 def xml(self,fo,pre=''):
00038 fo.write('<?xml version="1.0" encoding="UTF-8"?>\n')
00039 Comment("Warning: this is a generated file. Any modifications may be lost.").xml(fo,pre)
00040 if self.dtd:
00041 fo.write('<!DOCTYPE DDDB SYSTEM "%s"'%self.dtd)
00042 if self.external_entities:
00043 fo.write(' [\n')
00044 for ee in self.external_entities:
00045 fo.write(' <!ENTITY %s SYSTEM "%s">\n'%(ee[0],ee[1]))
00046 fo.write(' ]>\n')
00047 else:
00048 fo.write(' >\n')
00049 fo.write('<DDDB>\n')
00050 for obj in self.objects:
00051 obj.xml(fo,pre)
00052 if self.dtd:
00053 fo.write('</DDDB>\n')
00054 return
00055
00056 def write(self,filename):
00057 import os
00058 try:
00059 os.remove(filename)
00060 except OSError:
00061 pass
00062 file = open(filename,"w")
00063 self.xml(file,'')
00064 file.close()
00065 os.chmod(filename,0444)
00066 print 'Wrote "%s"'%filename
00067 return
00068
00069
00070