[issue1343] XMLGenerator: nice elements
panzi added the comment:
patch for xml/sax/saxutils.py
Added file: http://bugs.python.org/file8631/xml.sax.saxutils.patch
__
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1343>
__--- /usr/lib/python2.5/xml/sax/saxutils.py 2007-05-02 18:56:40.0 +0200
+++ xml/sax/saxutils.py 2007-10-22 22:44:33.0 +0200
@@ -4,8 +4,8 @@ convenience of application and driver wr
"""
import os, urlparse, urllib, types
-import handler
-import xmlreader
+from xml.sax import handler
+from xml.sax import xmlreader
try:
_StringTypes = [types.StringType, types.UnicodeType]
@@ -93,6 +93,7 @@ class XMLGenerator(handler.ContentHandle
self._current_context = self._ns_contexts[-1]
self._undeclared_ns_maps = []
self._encoding = encoding
+self._pending_start_element = False
def _write(self, text):
if isinstance(text, str):
@@ -111,6 +112,11 @@ class XMLGenerator(handler.ContentHandle
# Return the unqualified name
return name[1]
+def _finish_pending_start_element(self,endElement=False):
+if self._pending_start_element:
+self._write('>')
+self._pending_start_element = False
+
# ContentHandler methods
def startDocument(self):
@@ -127,15 +133,21 @@ class XMLGenerator(handler.ContentHandle
del self._ns_contexts[-1]
def startElement(self, name, attrs):
+self._finish_pending_start_element()
self._write('<' + name)
for (name, value) in attrs.items():
self._write(' %s=%s' % (name, quoteattr(value)))
-self._write('>')
+self._pending_start_element = True
def endElement(self, name):
-self._write('' % name)
+if self._pending_start_element:
+self._write('/>')
+self._pending_start_element = False
+else:
+self._write('' % name)
def startElementNS(self, name, qname, attrs):
+self._finish_pending_start_element()
self._write('<' + self._qname(name))
for prefix, uri in self._undeclared_ns_maps:
@@ -147,18 +159,27 @@ class XMLGenerator(handler.ContentHandle
for (name, value) in attrs.items():
self._write(' %s=%s' % (self._qname(name), quoteattr(value)))
-self._write('>')
+self._pending_start_element = True
def endElementNS(self, name, qname):
-self._write('' % self._qname(name))
+if self._pending_start_element:
+self._write('/>')
+self._pending_start_element = False
+else:
+self._write('' % self._qname(name))
def characters(self, content):
-self._write(escape(content))
+if content:
+self._finish_pending_start_element()
+self._write(escape(content))
def ignorableWhitespace(self, content):
-self._write(content)
+if content:
+self._finish_pending_start_element()
+self._write(content)
def processingInstruction(self, target, data):
+self._finish_pending_start_element()
self._write('' % (target, data))
___
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1343] XMLGenerator: nice elements
panzi added the comment:
patch for _xmlplus.sax.saxutils.py
Added file: http://bugs.python.org/file8632/_xmlplus.sax.saxutils.patch
__
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1343>
__--- /usr/lib/python2.5/site-packages/_xmlplus/sax/saxutils.py 2004-11-29 13:36:36.0 +0100
+++ site-packages/_xmlplus/sax/saxutils.py 2007-10-22 22:44:54.0 +0200
@@ -6,9 +6,11 @@ $Id: saxutils.py,v 1.35 2004/03/20 07:46
"""
import os, urlparse, urllib2, types
-import handler
-import xmlreader
-import sys, _exceptions, saxlib
+from xml.sax import handler
+from xml.sax import xmlreader
+from xml.sax import _exceptions
+from xml.sax import saxlib
+import sys
try:
_StringTypes = [types.StringType, types.UnicodeType]
@@ -173,7 +175,7 @@ class ErrorRaiser:
raise exception
# --- AttributesImpl now lives in xmlreader
-from xmlreader import AttributesImpl
+from xml.sax.xmlreader import AttributesImpl
# --- XMLGenerator is the SAX2 ContentHandler for writing back XML
import codecs
@@ -231,7 +233,12 @@ class XMLGenerator(handler.ContentHandle
self._undeclared_ns_maps = []
self._encoding = encoding
self._generated_prefix_ctr = 0
-return
+self._pending_start_element = False
+
+def _finish_pending_start_element(self,endElement=False):
+if self._pending_start_element:
+self._out.write('>')
+self._pending_start_element = False
# ContentHandler methods
@@ -249,16 +256,22 @@ class XMLGenerator(handler.ContentHandle
del self._ns_contexts[-1]
def startElement(self, name, attrs):
+self._finish_pending_start_element()
self._out.write('<' + name)
for (name, value) in attrs.items():
self._out.write(' %s=' % name)
writeattr(self._out, value)
-self._out.write('>')
+self._pending_start_element = True
def endElement(self, name):
-self._out.write('' % name)
+if self._pending_start_element:
+self._out.write('/>')
+self._pending_start_element = False
+else:
+self._out.write('' % name)
def startElementNS(self, name, qname, attrs):
+self._finish_pending_start_element()
if name[0] is None:
name = name[1]
elif self._current_context[name[0]] is None:
@@ -291,27 +304,36 @@ class XMLGenerator(handler.ContentHandle
name = self._current_context[name[0]] + ":" + name[1]
self._out.write(' %s=' % name)
writeattr(self._out, value)
-self._out.write('>')
+self._pending_start_element = True
def endElementNS(self, name, qname):
-# XXX: if qname is not None, we better use it.
-# Python 2.0b2 requires us to use the recorded prefix for
-# name[0], though
-if name[0] is None:
-qname = name[1]
-elif self._current_context[name[0]] is None:
-qname = name[1]
+if self._pending_start_element:
+self._out.write('/>')
+self._pending_start_element = False
else:
-qname = self._current_context[name[0]] + ":" + name[1]
-self._out.write('' % qname)
+# XXX: if qname is not None, we better use it.
+# Python 2.0b2 requires us to use the recorded prefix for
+# name[0], though
+if name[0] is None:
+qname = name[1]
+elif self._current_context[name[0]] is None:
+qname = name[1]
+else:
+qname = self._current_context[name[0]] + ":" + name[1]
+self._out.write('' % qname)
def characters(self, content):
-writetext(self._out, content)
+if content:
+self._finish_pending_start_element()
+writetext(self._out, content)
def ignorableWhitespace(self, content):
-self._out.write(content)
+if content:
+self._finish_pending_start_element()
+self._out.write(content)
def processingInstruction(self, target, data):
+self._finish_pending_start_element()
self._out.write('' % (target, data))
@@ -323,10 +345,12 @@ class LexicalXMLGenerator(XMLGenerator,
self._in_cdata = 0
def characters(self, content):
-if self._in_cdata:
-self._out.write(content.replace(']]>', ']]>]]>', ']]>]]>
[issue7948] Complete your registration to Python tracker -- key yAMyUeuHR9LS2Q86aUUs2GO4rujOhgWH
New submission from panzi : On 02/17/2010 02:35 AM, Python tracker wrote: > To complete your registration of the user "panzi" with > Python tracker, please do one of the following: > > - send a reply to [email protected] and maintain the subject line as is > (the > reply's additional "Re:" is ok), > > - or visit the following URL: > > http://bugs.python.org/?...@action=confrego&otk=yAMyUeuHR9LS2Q86aUUs2GO4rujOhgWH > -- messages: 99455 nosy: panzi severity: normal status: open title: Complete your registration to Python tracker -- key yAMyUeuHR9LS2Q86aUUs2GO4rujOhgWH ___ Python tracker <http://bugs.python.org/issue7948> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
