Monday, 13 December 2010

So, this is how I did the Choice reader in the end. A DOM approach would have been much easier ... but 20/20 hindsight is a wonderful thing.

That said, if I get many more cross-platform speedbumps, I'm still not ruling out junking .NET for lua or python.

Anyway...

        def load(r as XmlReader):

#
#               we need to know if this has any descendants
#               there is no easy way to do this
#               I am SO wishing I'd gone for the DOM approach
#
                unless r.ReadToDescendant("option"):
                        return

                first = true
                while true:
                        if first:
                                first = false
                        else:
                                r.Read()

                        if r.EOF:
                                raise("unexpected end of XML definition")

                        if r.NodeType == XmlNodeType.Element:
                                if r.Name != "option":
                                        raise "choice.load: not an option: '${r.Name}'"
                                o = Option()
                                o.load(r)
                                _options.Add(o)
                                continue

                        if r.NodeType == XmlNodeType.Whitespace:
                                continue

                        if r.NodeType == XmlNodeType.EndElement:
                                if r.Name == "choice":
                                        return
                                raise("choice.load: EndElement for '${r.Name}'")
                        raise("unexpected NodeType: ${r.NodeType}")


Interestingly, that fails with the unexpected EOF error. Which confirms what I'd half suspected: the problem is with Option.load() overeating...

No comments: