예제 #1
0
파일: XMLUtils.py 프로젝트: PaulRudin/xappy
    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)
예제 #2
0
파일: XMLUtils.py 프로젝트: PaulRudin/xappy
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))
예제 #3
0
파일: Errors.py 프로젝트: varunarya10/xappy
 def __init__(self, msg, *args):
     self.msg = msg % tuple(HTMLUtils.encodeText(arg) for arg in args)
예제 #4
0
파일: XMLUtils.py 프로젝트: PaulRudin/xappy
                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