Next / Previous / Contents / Shipman's homepage

6.17. Txny.__openDoc(): Open and validate the XML file

xnomo3.py
# - - -   T x n y . _ _ o p e n D o c

    def __openDoc(self, dataFile):
        '''Open and validate the XML file.

          [ dataFile is a file name, defaulting to DEFAULT_FILE_NAME ->
              if the effective dataFile names a readable XML file that is
              valid against the txny.rnc schema ->
                return an et.ElementTree representing that file
              else -> raise IOError ]
        '''

First we figure out the actual file name we're reading, then we use lxml to parse it into an ElementTree.

xnomo3.py
        #-- 1 --
        # [ if bool(fileName) ->
        #     fileName  :=  dataFile
        #   else ->
        #     fileName  :=  DEFAULT_FILE_NAME ]
        fileName = dataFile or DEFAULT_FILE_NAME

        #-- 2 --
        # [ if fileName is a readable, valid XML file ->
        #     doc  :=  that file as an et.ElementTree
        #   else -> raise IOError ]
        doc = et.parse(fileName)

Next, we'll use the rnc_validate module to validate the document against the RNG schema.

xnomo3.py
        #-- 3 --
        # [ if the RNG schema is readable and valid ->
        #     rv  :=  an rnc_validate.RelaxValidator instance that
        #             validates against that schema
        #   else -> raise IOError ]
        rv = rnc_validate.RelaxValidator("http://www.nmt.edu/~shipman/xnomo3/txny.rng")

        #-- 4 --
        # [ if doc validates against rv ->
        #     I
        #   else -> raise IOError ]
        try:
            rv.validate(doc)
        except rnc_validate.RelaxException, details:
            raise IOError("Input file '%s' is not valid against "
                "its schema: %s" %
                (fileName, str(details)))

There is one more check. In previous schemas, the root taxonomySystem element did not have a version attribute. To be sure that our XML file came from the current version of nomcompile3, we insure that there is such an element, and that its first character is 3.

xnomo3.py
        #-- 5 --
        # [ if doc's root element has a version attribute starting
        #   with 3 ->
        #     I
        #   else -> raise IOError ]
        root = doc.getroot()
        try:
            version = root.attrib[rnc.VERSION_A]
        except KeyError:
            raise IOError("Root element has no version attribute.")
        if version[0] != '3':
            raise IOError("XML file not version 3.")

        #-- 6 --
        return doc