The processFile() function handles
all the processing for one DocBook source file.
# - - - p r o c e s s F i l e - - -
def processFile ( inFileName ):
"""Process one input file.
[ inFileName is a string ->
if inFileName names a readable, valid DocBook XML file ->
output files named in that file := code fragments
designated for those files
sys.stderr +:= error messages from processing that file,
if any
else ->
sys.stderr +:= error message ]
"""
The first step is to open the input file, and report errors if that fails.
#-- 1 --
# [ if inFileName names a readable file ->
# inFile := that file opened for reading
# else ->
# sys.stderr +:= error message
# return ]
try:
inFile = open ( inFileName )
except IOError, detail:
sys.stderr.write ( "*** Can't open file '%s' for reading: %s\n" %
(inFileName, detail) )
return
The next step is to create a SAX parser. We first create
a content handler object, an ArticleHandler object. This object
contains the three handlers that observe start tags, text
content, and end tags. See Section 4.7, “class ArticleHandler: The
customized content handler”.
#-- 2 --
# [ ch := an ArticleHandler instance ]
ch = ArticleHandler()
The remaining steps obey the usual SAX protocol. We
create a new SAX parser with the make_parser() function, associate our
content handler with it using its .setContentHandler() method, and then use
its .parse() method to read
inFile.
This process is fairly well-described on page 53 of the
O'Reilly Python
and XML book. (However, the example
on this page does not run unless you first import the
ContentHandler class.)
#-- 3 --
# [ ch is an ArticleHandler object ->
# if inFile contains a readable, well-formed XML file ->
# output files named in inFile := code fragments
# designated for those files
# sys.stderr +:= error message(s), if any ]
saxparser = make_parser()
saxparser.setContentHandler ( ch )
saxparser.parse ( inFile )