Changeset 740

Show
Ignore:
Timestamp:
02/06/04 04:05:14 (4 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBFoundation/RBFoundation/XMLNode.py

    r706 r740  
    6161 
    6262class xmlnodebase(object):  
    63     pass 
     63    def __str__(self): 
     64        """Returns xmlnode as a string in XML form.""" 
     65        return str(self.toxml()) 
     66 
     67    def toxml(self, pretty=False, level=0, indent='    ', newline='\n'): 
     68        raise NotImplementedError 
     69 
     70#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     71 
     72class xmlcomment(xmlnodebase): 
     73    def __init__(self, comment): 
     74        self.comment = comment 
     75 
     76    def toxml(self, pretty=False, level=0, indent='    ', newline='\n'): 
     77        comment = self.comment.replace('<!--', '').replace('-->', '') 
     78        if not pretty: 
     79            return '<!-- ' + comment + ' -->' 
     80        else: 
     81            result = ['<!--'] + list(comment.split(newline)) + ['-->'] 
     82            if len(result) > 3:  
     83                joinstr = newline+indent*level 
     84            else: joinstr = ' ' 
     85            return joinstr.join(result) 
     86 
     87#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    6488 
    6589class xmlnode(xmlnodebase): 
     
    175199        return '''<%s (%r, %r, %r)>''' % (self.__class__.__name__, self.node, self.namespace, self.prefix) 
    176200 
    177     def __str__(self): 
    178         """Returns xmlnode as a string in XML form.""" 
    179         return str(self.toxml()) 
    180  
    181201    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    182202    #~ Public Methods  
     
    187207        if args: self.attrs.update(dict(args)) 
    188208        if kw: self.attrs.update(kw) 
     209        return self 
    189210 
    190211    def adddata(self, cdata): 
    191212        """Adds a child cdata to xmlnode.""" 
    192213        self.elems.append(cdata) 
     214        return self 
     215 
     216    def addcomment(self, comment, *args, **kw): 
     217        self.elems.append(xmlcomment(comment, *args, **kw)) 
    193218        return self 
    194219 
     
    260285    def enumdata(self, match=None, idxonly=True): 
    261286        """Returns a generator to iterate through the matching data indices in xmlnode""" 
    262         if not match: match = _any 
     287        if match is None: match = _any 
    263288        elif callable(match): match = matchobj(match) 
    264289        idx = 0 
    265290        for each in self.elems: 
    266291            if isinstance(each, basestring): 
    267                 if each == match: 
     292                if match == each: 
    268293                    if idxonly: yield idx 
    269294                    else: yield idx, each 
     
    272297    def iterdata(self, match=None): 
    273298        """Returns a generator to iterate through the matching data in xmlnode""" 
    274         if not match: match = _any 
     299        if match is None: match = _any 
    275300        elif callable(match): match = matchobj(match) 
    276301        for each in self.elems: 
    277302            if isinstance(each, basestring): 
    278                 if each == match: 
     303                if match == each: 
    279304                    yield each 
    280305 
     
    312337                return lambda each: node==each.node and namespace==each.namespace and prefix==each.prefix 
    313338            match = makematch(*args, **kw) 
    314         else: match = lambda: True 
     339        else: match = lambda each: True 
    315340        idx = 0 
    316341        for each in self.elems: 
     
    328353                return lambda each: node==each.node and namespace==each.namespace and prefix==each.prefix 
    329354            match = makematch(*args, **kw) 
    330         else: match = lambda: True 
     355        else: match = lambda each: True 
    331356        for each in self.elems: 
    332357            if not isinstance(each, basestring): 
     
    467492        if pretty: prettyindent = newline + indent * level 
    468493        result = [] 
     494        wastext = False 
    469495        for elem in self.elems: 
    470             if pretty: result.append(prettyindent) 
    471496            if isinstance(elem, basestring): 
    472                 result.append(xmlescape(elem)) 
    473             else: result.append(elem.toxml(pretty, level, indent, newline)) 
     497                if pretty and not wastext: result.append(prettyindent) 
     498                elem = xmlescape(elem) 
     499                if pretty:  
     500                    elem = elem.replace(newline, prettyindent) 
     501                result.append(elem) 
     502                wastext = True 
     503            else:  
     504                if pretty: result.append(prettyindent) 
     505                result.append(elem.toxml(pretty, level, indent, newline)) 
     506                wastext = False 
    474507        return result 
    475508