Next / Previous / Contents / TCC Help System / NM Tech homepage

4.7. class ArticleHandler: The customized content handler

This class represents our content handler. It inherits from the SAX ContentHandler class; all we have to do is define three handlers, with given names.

These state items in the instance manage the process of extracting code fragments:

.outFileName

Initially set to None, whenever we are inside a code fragment, this attribute holds the name of the output file.

.outFile

When we are inside a code fragment, this attribute holds a writeable file handle that writes to self.outFileName.

.fileMap

Because we want each output file to be the concatenation of all the code fragments assigned to that file, we want to open each output file only once. Hence, the .fileMap attribute holds a dictionary whose keys are the names of output files we have seen so far, and each corresponding value is a writeable file handle for that file.

litsource
# - - - - -   c l a s s   A r t i c l e H a n d l e r   - - - - -

class ArticleHandler(ContentHandler):
    """Content handler object.

      Exports:
        ArticleHandler():  [ return a new ArticleHandler ]

      State/Invariants:
        .fileMap:
          [ a dictionary whose keys are the names of files in
            fragments seen so far; each value is a writeable
            file handle for that file ]
        .outFileName:
          [ if currently within a fragment ->
              the output file name for that fragment
            else -> None ]
        .outFile:
          [ if currently within a fragment ->
              the output file handle for that fragment
            else -> None ]
    """