xml_matching.py
Go to the documentation of this file.
00001 import xml.dom
00002 import re
00003 import sys
00004 
00005 # regex to match whitespace
00006 whitespace = re.compile(r'\s+')
00007 
00008 
00009 def all_attributes_match(a, b):
00010     if len(a.attributes) != len(b.attributes):
00011         print("Different number of attributes")
00012         return False
00013     a_atts = [(a.attributes.item(i).name, a.attributes.item(i).value) for i in range(len(a.attributes))]  # noqa
00014     b_atts = [(b.attributes.item(i).name, b.attributes.item(i).value) for i in range(len(b.attributes))]  # noqa
00015     a_atts.sort()
00016     b_atts.sort()
00017 
00018     for i in range(len(a_atts)):
00019         if a_atts[i][0] != b_atts[i][0]:
00020             print("Different attribute names: %s and %s" % (a_atts[i][0], b_atts[i][0]))  # noqa
00021             return False
00022         try:
00023             if abs(float(a_atts[i][1]) - float(b_atts[i][1])) > 1.0e-9:
00024                 print("Different attribute values: %s and %s" % (a_atts[i][1], b_atts[i][1]))  # noqa
00025                 return False
00026         except ValueError:  # Attribute values aren't numeric
00027             if a_atts[i][1] != b_atts[i][1]:
00028                 print("Different attribute values: %s and %s" % (a_atts[i][1], b_atts[i][1]))  # noqa
00029                 return False
00030 
00031     return True
00032 
00033 
00034 def text_matches(a, b):
00035     a_norm = whitespace.sub(' ', a)
00036     b_norm = whitespace.sub(' ', b)
00037     if a_norm.strip() == b_norm.strip():
00038         return True
00039     print("Different text values: '%s' and '%s'" % (a, b))
00040     return False
00041 
00042 
00043 def nodes_match(a, b, ignore_nodes):
00044     if not a and not b:
00045         return True
00046     if not a or not b:
00047         return False
00048 
00049     if a.nodeType != b.nodeType:
00050         print("Different node types: %s and %s" % (a, b))
00051         return False
00052 
00053     # compare text-valued nodes
00054     if a.nodeType in [xml.dom.Node.TEXT_NODE,
00055                       xml.dom.Node.CDATA_SECTION_NODE,
00056                       xml.dom.Node.COMMENT_NODE]:
00057         return text_matches(a.data, b.data)
00058 
00059     # ignore all other nodes except ELEMENTs
00060     if a.nodeType != xml.dom.Node.ELEMENT_NODE:
00061         return True
00062 
00063     # compare ELEMENT nodes
00064     if a.nodeName != b.nodeName:
00065         print("Different element names: %s and %s" % (a.nodeName, b.nodeName))
00066         return False
00067 
00068     if not all_attributes_match(a, b):
00069         return False
00070 
00071     a = a.firstChild
00072     b = b.firstChild
00073     while a or b:
00074         # ignore whitespace-only text nodes
00075         # we could have several text nodes in a row, due to replacements
00076         while (a and
00077                ((a.nodeType in ignore_nodes) or
00078                 (a.nodeType == xml.dom.Node.TEXT_NODE and whitespace.sub('', a.data) == ""))):  # noqa
00079             a = a.nextSibling
00080         while (b and
00081                ((b.nodeType in ignore_nodes) or
00082                 (b.nodeType == xml.dom.Node.TEXT_NODE and whitespace.sub('', b.data) == ""))):  # noqa
00083             b = b.nextSibling
00084 
00085         if not nodes_match(a, b, ignore_nodes):
00086             return False
00087 
00088         if a:
00089             a = a.nextSibling
00090         if b:
00091             b = b.nextSibling
00092 
00093     return True
00094 
00095 
00096 def xml_matches(a, b, ignore_nodes=[]):
00097     if isinstance(a, str):
00098         return xml_matches(xml.dom.minidom.parseString(a).documentElement, b,
00099                            ignore_nodes)
00100     if isinstance(b, str):
00101         return xml_matches(a, xml.dom.minidom.parseString(b).documentElement,
00102                            ignore_nodes)
00103     if a.nodeType == xml.dom.Node.DOCUMENT_NODE:
00104         return xml_matches(a.documentElement, b, ignore_nodes)
00105     if b.nodeType == xml.dom.Node.DOCUMENT_NODE:
00106         return xml_matches(a, b.documentElement, ignore_nodes)
00107 
00108     if not nodes_match(a, b, ignore_nodes):
00109         print("Match failed:")
00110         a.writexml(sys.stdout)
00111         print()
00112         print('=' * 78)
00113         b.writexml(sys.stdout)
00114         print()
00115         return False
00116     return True


urdfdom_py
Author(s): Thomas Moulard, David Lu, Kelsey Hawkins, Antonio El Khoury, Eric Cousineau, Ioan Sucan , Jackie Kay
autogenerated on Wed Aug 2 2017 02:23:35