TOC PREV NEXT INDEX


Example 1: A Formatted Print Handler



The ASN1C evaluation and distribution kits include a sample program for doing a formatted print of parsed data. This code can be found in the cpp/sample_per/eventHandler directory. Parts of the code will be reproduced here for reference, but refer to this directory to see the full implementation.

The format for the printout will be simple. Each element name will be printed followed by an equal sign (=) and an open brace ({) and newline. The value will then be printed followed by another newline. Finally, a closing brace (}) followed by another newline will terminate the printing of the element. An indentation count will be maintained to allow for a properly indented printout.

A header file must first be created to hold our print handler class definition (or the definition could be added to an existing header file). This file will contain a class derived from the Asn1NamedEventHandler base class as follows:

class PrintHandler : public Asn1NamedEventHandler {
 
 protected:
 
   const char* mVarName;
 
   int mIndentSpaces;
 
 public:
 
   PrintHandler (const char* varName);
 
   ~PrintHandler ();
 
   void indent ();
 
   virtual void startElement (const char* name, int index = -1);
 
   virtual void endElement (const char* name, int index = -1);
 
   virtual void boolValue (OSBOOL value);
 

 
   ... other virtual contents method declarations
 

 
}
 

In this definition, we chose to add the mVarName and mIndentSpaces member variables to keep track of these items. The user is free to add any type of member variables he or she wants. The only firm requirement in defining this derived class is the implementation of the virtual methods.

We implement these virtual methods as follows:

In startElement, we print the name, equal sign, and opening brace:

void PrintHandler::startElement (const char* name, int index)
 
{
 
   indent();
 
   printf ("%s = {\n", name);
 
   mIndentLevel++;
 
}
 

In this simplified implementation, we simply indent (this is another private method within the class) and print out the name, equal sign, and opening brace. We then increment the indent level. Note that this is a highly simplified form. We don't even bother to check if the index argument is greater than or equal to zero. This would determine if a `[x]' should be appended to the element name. In the sample program that is included with the compiler distribution, the implementation is complete.

In endElement, we simply terminate our brace block as follows:

void PrintHandler::endElement (const char* name, int index)
 
{
 
   mIndentLevel--;
 
   indent();
 
   printf ("}\n");
 
}
 

All that each of the various value methods have to do is print a stringified representation of the value out to stdout. For example, the intValue callback would just print an integer value:

void PrintHandler::intValue (int value)
 
{
 
   indent();
 
   printf ("%d\n", value);
 
}
 

Next, we need to create an object of our derived class and register it prior to invoking the decode method. In the reader.cpp program, the following lines do this:

   // Create and register an event handler object
 

 
   PrintHandler* pHandler = new PrintHandler ("employee");
 
   decodeBuffer.addEventHandler (pHandler);
 

The addEventHandler method defined in the Asn1MessageBuffer base class is the mechanism used to do this. Note that event handler objects can be stacked. Several can be registered before invoking the decode function. When this is done, the entire list of event handler objects is iterated through and the appropriate event handling callback function invoked whenever a defined event is encountered.

The implementation is now complete. The program can now be compiled and run. When this is done, the resulting output is as follows:

employee = {
 
   name = {
 
      givenName = {
 
         "John"
 
      }
 
      initial = {
 
         "P"
 
      }
 
      familyName = {
 
         "Smith"
 
      }
 
   }
 
   ...
 

This can certainly be improved. For one thing it can be changed to print primitive values out in a "name = value" format (i.e., without the braces). But this should provide the general idea of how it is done.


Objective Systems, Inc.

55 Dowlin Forge Road
Exton, Pennsylvania 19341
http://www.obj-sys.com
Phone: (484) 875-9841
Toll-free: (877) 307-6855 (US only)
Fax: (484) 875-9830
info@obj-sys.com
TOC PREV NEXT INDEX