Inheritance in code: the rich get richer
25 Nov 2015Because the GreekNode class of the greeklang foundational libraries extends the XmlNode class in the groovy XML utilities package, you can now use the toXml method on GreekNodes just as you would on an XmlNode. A neat interaction results from three previously implemented features, two in the parent class and one in the GreekString class that GreekNodes depend on:
XmlNodeguarantees the XML equivalence of the XML String created bytoXmlto its parsed sourceXmlNodeguarantees that the String output ofextractTextis identical for XML equivalent nodesGreekStringguarantees a uniform underlying representation that allows comparison ofGreekStringobjects no matter how they were constructed
Watch what happens when we combine these. First, let’s make a new GreekString derived from the collected text of XML source in ASCII:
String asciiSource = """
<p n="1">ABG</p>
"""
GreekNode asciiNode = new GreekNode(asciiSource)
GreekString gs1 = new GreekString(asciiNode.collectText())
Then, let’s do the same thing with a source in Unicode Greek:
String unicodeSrc = """
<p n="1">ΑΒΓ</p>
"""
GreekNode unicodeNode = new GreekNode(unicodeSrc, true)
GreekString gs2 = new GreekString(unicodeNode.collectText(), true)
Now the punchline: the two GreekStrings evaluate as equivalent.
assert gs1 == gs2
What seems to me just a little bit like magic is not that a handful of lines is enough to compare differently formatted XML with different representations in their text nodes: it’s that we never resorted to munging Strings or transcoding Greek. No regular expressions anywhere! Conceptually, we’re working at a high level: here’s an XML node with text content in Greek; let’s therefore make a Greek String from its contents; and of course we can compare Greek Strings. The final assert gs1 == gs2 feels as satisfying as the QED of a Euclidean proof.