def toxml (self): """ Return this tag/data formatted as XML. """ if self.type == self.START: ret = ['<%s' % self.nodeNames[-1]] for i in xrange(len(self.atts)): att = self.atts.item (i) ret.append (u' %s="%s"' % (att.name, HTMLUtils.encodeText (att.nodeValue))) ret.append ('>') return ''.join (ret) elif self.type == self.END: return '</%s>' % self.nodeNames[-1] else: return HTMLUtils.encodeText (self.data)
def _convertParseExceptions(callable, xmlString=None): """ Convert exceptions raised by XML parsers to UserErrors describing the error. """ try: return callable() except xml.parsers.expat.ExpatError, e: context = '' if xmlString: try: lines = xmlString.split('\n') pos = max(e.offset - 10, 0) line = lines[e.lineno - 1] context = " (near <q>%s</q>)" % HTMLUtils.encodeText(line[pos:pos + 20]) context = context.replace('%', '%%') except: pass raise Errors.UserError("at line %%s, column %%s%s: %%s" % context, e.lineno, e.offset, xml.parsers.expat.ErrorString(e.code))
def __init__(self, msg, *args): self.msg = msg % tuple(HTMLUtils.encodeText(arg) for arg in args)
line = lines[e.lineno - 1] context = " (near <q>%s</q>)" % HTMLUtils.encodeText(line[pos:pos + 20]) context = context.replace('%', '%%') except: pass raise Errors.UserError("at line %%s, column %%s%s: %%s" % context, e.lineno, e.offset, xml.parsers.expat.ErrorString(e.code)) except xml.sax.SAXParseException, e: context = '' if xmlString: try: lines = xmlString.split('\n') pos = max(e.getColumnNumber() - 10, 0) line = lines[e.getLineNumber() - 1] context = " (near <q>%s</q>)" % HTMLUtils.encodeText(line[pos:pos + 20]) context = context.replace('%', '%%') except: pass raise Errors.UserError("at line %%s, column %%s%s: %%s" % context, e.getLineNumber(), e.getColumnNumber(), e.getMessage()) except ValueError, e: raise Errors.UserError("Parse error: %s", str(e)) class ParsedXml: """ Class representing a parsed piece of XML, and providing several convenience methods for getting at the parsed XML. The parsed piece of XML is a single well-formed tag (ie, has exactly one