Ignore:
Timestamp:
Aug 8, 2008, 9:28:34 AM (16 years ago)
Author:
ole
Message:

Added error check in xml_tools to catch text nodes followed by non-text nodes.
This was in response to an error in one of the license files (not yet fixed).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/xml_tools.py

    r5162 r5628  
    239239        fid = xml
    240240
    241     dom = parse(fid)
    242 
    243 ##     try:
    244 ##         dom = parse(fid)
    245 ##     except Exception, e:
    246 ##         # Throw filename into dom exception
    247 ##         msg = 'XML file "%s" could not be parsed: ' %fid.name
    248 ##         msg += str(e)
    249 ##         raise Exception, msg
    250 
    251     return dom2object(dom)
     241    try:
     242        dom = parse(fid)
     243    except Exception, e:
     244        # Throw filename into dom exception
     245        msg = 'XML file "%s" could not be parsed.\n' %fid.name
     246        msg += 'Error message from parser: "%s"' %str(e)
     247        raise Exception, msg
     248
     249    try:
     250        xml_object = dom2object(dom)
     251    except Exception, e:
     252        msg = 'Could not convert %s into XML object.\n' %fid.name
     253        msg += str(e)
     254        raise Exception, msg
     255   
     256    return xml_object
    252257
    253258
     
    258263
    259264    value = []
     265    textnode_encountered = None
    260266    for n in node.childNodes:
    261 
     267   
    262268        if n.nodeType == 3:
    263269            # Child is a text element - omit the dom tag #text and
    264270            # go straight to the text value.
    265 
     271           
     272            # Note - only the last text value will be recorded
     273           
    266274            msg = 'Text element has child nodes - this shouldn\'t happen'
    267275            assert len(n.childNodes) == 0, msg
     
    273281                continue
    274282           
    275             value = x
     283            textnode_encountered = value = x
    276284        else:
    277285            # XML element
     286           
     287           
     288            if textnode_encountered is not None:
     289                msg = 'A text node was followed by a non-text tag. This is not allowed.\n'
     290                msg += 'Offending text node: "%s" ' %str(textnode_encountered)           
     291                msg += 'was followed by node named: "<%s>"' %str(n.nodeName)
     292                raise Exception, msg
     293           
    278294
    279295            value.append(dom2object(n))
Note: See TracChangeset for help on using the changeset viewer.