home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_931 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.0 KB  |  84 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. class ElementWriter(object):
  6.     
  7.     def __init__(self, e, header = False, sourceEncoding = 'ascii', spaceBeforeClose = True, outputEncodingName = 'UTF-16'):
  8.         self.header = header
  9.         self.e = e
  10.         self.sourceEncoding = sourceEncoding
  11.         self.spaceBeforeClose = spaceBeforeClose
  12.         self.outputEncodingName = outputEncodingName
  13.  
  14.     
  15.     def _encodeCdata(self, rawText):
  16.         if type(rawText) is str:
  17.             rawText = rawText.decode(self.sourceEncoding)
  18.         
  19.         text = rawText.replace('&', '&')
  20.         text = text.replace('<', '<')
  21.         text = text.replace('>', '>')
  22.         return text
  23.  
  24.     
  25.     def _writeAttribute(self, f, name, value):
  26.         f.write(u' %s="' % unicode(name))
  27.         if not isinstance(value, basestring):
  28.             value = unicode(value)
  29.         
  30.         value = self._encodeCdata(value)
  31.         value = value.replace('"', '"')
  32.         f.write(value)
  33.         f.write(u'"')
  34.  
  35.     
  36.     def _writeText(self, f, rawText):
  37.         text = self._encodeCdata(rawText)
  38.         f.write(text)
  39.  
  40.     
  41.     def _write(self, f, e):
  42.         f.write(u'<' + unicode(e.tag))
  43.         attributes = e.items()
  44.         attributes.sort()
  45.         for name, value in attributes:
  46.             self._writeAttribute(f, name, value)
  47.         
  48.         if e.text is not None or len(e) > 0:
  49.             f.write(u'>')
  50.             if e.text:
  51.                 self._writeText(f, e.text)
  52.             
  53.             for e2 in e:
  54.                 self._write(f, e2)
  55.             
  56.             f.write(u'</%s>' % e.tag)
  57.         elif self.spaceBeforeClose:
  58.             f.write(' ')
  59.         
  60.         f.write(u'/>')
  61.         if e.tail is not None:
  62.             self._writeText(f, e.tail)
  63.         
  64.  
  65.     
  66.     def toString(self):
  67.         
  68.         class x:
  69.             pass
  70.  
  71.         buffer = []
  72.         x.write = buffer.append
  73.         self.write(x)
  74.         return u''.join(buffer)
  75.  
  76.     
  77.     def write(self, f):
  78.         if self.header:
  79.             f.write(u'<?xml version="1.0" encoding="%s"?>\n' % self.outputEncodingName)
  80.         
  81.         self._write(f, self.e)
  82.  
  83.  
  84.