Chapter 19: Building an XML-Managed Application
Many software applications, from servers to simple client applications, use configuration files to set up their environment, run-time options, look and feel, and other persisted conditions and options. In the past these files have usually been simply a collection of name-value pairs with specified delimiters. It turns out that this format suffers significant limitations. For example, it is a very fragile format, because misplacement of a single character may invalidate every subsequent entry. Also, there is no random access to this type of format, so if you want a single bit of information, it has to be processed linearly until the data appears. Finally, a flat list of pairs may not be sufficient to support the required semantics. For these reasons and more, these flat files are being replaced by XML files, which eliminate or mitigate these issues.In this chapter, you will build a C XML application that is configured by an XML file. The scenario is a simple one that is easily extensible and involves publishing XML content to a variety of devices by using XSLT and the XML configuration file to specify the stylesheets and profiles that control the transformations.
Designing the Framework
For this example, you are going to build a media catalog application, called publishcat, for a fictitious company that brokers the sale of books, CDs, and DVDs. It receives its catalogs in XML and must publish them in a way that optimizes their presentation on a variety of devices, such as PCs, PDAs, and cell phones. You could implement this by using a custom-built stylesheet for each device; however, you can use a single stylesheet and have the device profile passed in as parameters read from an XML configuration file. This is the approach we will examine and implement.To build this application, you will need the XML-formatted catalog entries for books, CDs, and DVDs. Since each of these media types has differing semantics for its description, and thus different DTDs, you will have a different stylesheet for each. The configuration file will also have a schema that contains not only the XSL stylesheets to be applied to each type but also the three profiles and their elements. To optimize the display characteristics, you will need to specify the following parameters when applying the stylesheet:
Character Set The proper character encoding for the device
OutputFileType The markup language to use for the display
LinesPerPage The number of lines to display per screen page
Graphics An option to display graphics
Color Depth The number of colors supported to display the best graphics
Because these parameters are numerous and a mixture of data types, instead of passing each one in on the command line, you will group them as named profiles that the application can read from the configuration file.The publishcat application will be a command-line application that functionally performs the following steps to produce the appropriate output from a set of XML input files:
Parses the command line.
Validates and creates a DOM from the input XML media documents.
Retrieves the schema URL for each DOM.
Parses XSLConfig.xml using SAX.
Retrieves XSLT Stylesheet and profile parameters.
Performs XSLT transformation on the DOMs passing the profile parameters to the stylesheet.
Serializes the result to the specified output file. If no output file is specified, it prints to the screen.
Cleans up and terminates.Each of these steps will be discussed and implemented in the following sections.