Beispiel #1
0
 def startElement(self, tag, attrs={}):
     if self.__pending_doctype:
         self.handle_doctype(tag)
     self._check_pending_content()
     self.__pushtag(tag)
     self.__check_flowing(tag, attrs)
     if attrs.has_key("xml:lang"):
         self.__lang = attrs["xml:lang"]
         del attrs["xml:lang"]
     if self._packing:
         prefix = ""
     elif self._flowing:
         prefix = self._prefix[:-self.indentation]
     else:
         prefix = ""
     stag = "%s%s%s" % (prefix, self.__syntax.stago, tag)
     prefix = "%s %s" % (prefix, (len(tag) * " "))
     lit = self.__syntax.lit
     lita = self.__syntax.lita
     vi = self.__syntax.vi
     a = ''
     if self._flowing != self.__stack[-1][0]:
         if self._dtdflowing is not None \
            and self._flowing == self._dtdflowing:
             pass
         else:
             a = ' xml:space%s%s%s%s' \
                 % (vi, lit, ["default", "preserve"][self._flowing], lit)
     if self.__lang != self.__stack[-1][1]:
         a = '%s xml:lang%s%s%s%s' % (a, vi, lit, self.lang, lit)
     line = stag + a
     self._offset = self._offset + len(line)
     a = ''
     for k, v in attrs.items():
         if v is None:
             continue
         v = str(v)
         if string.find(v, lit) == -1:
             a = ' %s%s%s%s%s' % (k, vi, lit, escape(str(v)), lit)
         elif string.find(v, lita) == -1:
             a = ' %s%s%s%s%s' % (k, vi, lita, escape(str(v)), lita)
         else:
             a = ' %s%s%s%s%s' % (k, vi, lit,
                                  escape(str(v), {lit:self.__syntax.lit_quoted}),
                                  lita)
         if (self._offset + len(a)) > self.lineLength:
             self._write(line + "\n")
             line = prefix + a
             self._offset = len(line)
         else:
             line = line + a
             self._offset = self._offset + len(a)
     self._write(line)
     self.__pending_content = 1
     if (  self.__dtdinfo and not
           (self.__dtdinfo.has_element_content(tag)
            or self.__dtdinfo.is_empty(tag))):
         self._packing = 1
Beispiel #2
0
 def startElement(self, tag, attrs={}):
     if self.__pending_doctype:
         self.handle_doctype(tag)
     self._check_pending_content()
     self.__pushtag(tag)
     self.__check_flowing(tag, attrs)
     if attrs.has_key("xml:lang"):
         self.__lang = attrs["xml:lang"]
         del attrs["xml:lang"]
     if self._packing:
         prefix = ""
     elif self._flowing:
         prefix = self._prefix[:-self.indentation]
     else:
         prefix = ""
     stag = "%s%s%s" % (prefix, self.__syntax.stago, tag)
     prefix = "%s %s" % (prefix, (len(tag) * " "))
     lit = self.__syntax.lit
     lita = self.__syntax.lita
     vi = self.__syntax.vi
     a = ''
     if self._flowing != self.__stack[-1][0]:
         if self._dtdflowing is not None \
            and self._flowing == self._dtdflowing:
             pass
         else:
             a = ' xml:space%s%s%s%s' \
                 % (vi, lit, ["default", "preserve"][self._flowing], lit)
     if self.__lang != self.__stack[-1][1]:
         a = '%s xml:lang%s%s%s%s' % (a, vi, lit, self.lang, lit)
     line = stag + a
     self._offset = self._offset + len(line)
     a = ''
     for k, v in attrs.items():
         if v is None:
             continue
         v = str(v)
         if string.find(v, lit) == -1:
             a = ' %s%s%s%s%s' % (k, vi, lit, escape(str(v)), lit)
         elif string.find(v, lita) == -1:
             a = ' %s%s%s%s%s' % (k, vi, lita, escape(str(v)), lita)
         else:
             a = ' %s%s%s%s%s' % (
                 k, vi, lit, escape(str(v),
                                    {lit: self.__syntax.lit_quoted}), lita)
         if (self._offset + len(a)) > self.lineLength:
             self._write(line + "\n")
             line = prefix + a
             self._offset = len(line)
         else:
             line = line + a
             self._offset = self._offset + len(a)
     self._write(line)
     self.__pending_content = 1
     if (self.__dtdinfo and not (self.__dtdinfo.has_element_content(tag)
                                 or self.__dtdinfo.is_empty(tag))):
         self._packing = 1
Beispiel #3
0
 def characters(self, data, start, length):
     data = data[start:start + length]
     if data:
         self._check_pending_content()
         data = escape(data)
         if "\n" in data:
             p = string.find(data, "\n")
             self._offset = len(data) - (p + 1)
         else:
             self._offset = self._offset + len(data)
         self._check_pending_content()
         self._write(data)
Beispiel #4
0
 def characters(self, data, start, length):
     data = data[start: start+length]
     if data:
         self._check_pending_content()
         data = escape(data)
         if "\n" in data:
             p = string.find(data, "\n")
             self._offset = len(data) - (p + 1)
         else:
             self._offset = self._offset + len(data)
         self._check_pending_content()
         self._write(data)
Beispiel #5
0
 def handle_cdata(self, data):
     self._check_pending_content()
     # There should be a better way to generate '[CDATA['
     start = self.__syntax.mdo + "[CDATA["
     end = self.__syntax.msc + self.__syntax.mdc
     s = "%s%s%s" % (start, escape(data), end)
     if self._packing:
         if "\n" in s:
             rpos = string.rfind(s, "\n")
             self._offset = len(s) - (rpos + 1) + len(end)
         else:
             self._offset = self._offset + len(s) + len(start + end)
         self._write(s)
     else:
         self._offset = 0
         self._write(s + "\n")
Beispiel #6
0
 def handle_cdata(self, data):
     self._check_pending_content()
     # There should be a better way to generate '[CDATA['
     start = self.__syntax.mdo + "[CDATA["
     end = self.__syntax.msc + self.__syntax.mdc
     s = "%s%s%s" % (start, escape(data), end)
     if self._packing:
         if "\n" in s:
             rpos = string.rfind(s, "\n")
             self._offset = len(s) - (rpos + 1) + len(end)
         else:
             self._offset = self._offset + len(s) + len(start + end)
         self._write(s)
     else:
         self._offset = 0
         self._write(s + "\n")
Beispiel #7
0
 def characters(self, data, start, length):
     data = data[start:start + length]
     if not data:
         return
     self._check_pending_content()
     data = escape(data)
     if not self._flowing:
         self._write(data)
         return
     words = string.split(data)
     begspace = data[0] in string.whitespace
     endspace = words and (data[-1] in string.whitespace)
     prefix = self._prefix
     if len(prefix) > 40:
         prefix = "    "
     offset = self._offset
     L = []
     append = L.append
     if begspace:
         append(" ")
         offset = offset + 1
     ws = ""
     ws_len = 0
     while words:
         w = words[0]
         del words[0]
         if (offset + ws_len + len(w)) > self.lineLength:
             append("\n")
             append(prefix)
             append(w)
             offset = len(prefix) + len(w)
         else:
             append(ws)
             ws, ws_len = " ", 1
             append(w)
             offset = offset + 1 + len(w)
     if endspace:
         append(" ")
         offset = offset + 1
     self._offset = offset
     self._write(string.join(L, ""))
Beispiel #8
0
 def characters(self, data, start, length):
     data = data[start: start + length]
     if not data:
         return
     self._check_pending_content()
     data = escape(data)
     if not self._flowing:
         self._write(data)
         return
     words = string.split(data)
     begspace = data[0] in string.whitespace
     endspace = words and (data[-1] in string.whitespace)
     prefix = self._prefix
     if len(prefix) > 40:
         prefix = "    "
     offset = self._offset
     L = []
     append = L.append
     if begspace:
         append(" ")
         offset = offset + 1
     ws = ""
     ws_len = 0
     while words:
         w = words[0]
         del words[0]
         if (offset + ws_len + len(w)) > self.lineLength:
             append("\n")
             append(prefix)
             append(w)
             offset = len(prefix) + len(w)
         else:
             append(ws)
             ws, ws_len = " ", 1
             append(w)
             offset = offset + 1 + len(w)
     if endspace:
         append(" ")
         offset = offset + 1
     self._offset = offset
     self._write(string.join(L, ""))