An Object Oriented Approach
[Previous] [Main] [Next]

 
Introduction
html++ uses an object-oriented design, incorporating functionality common to the entire library in a few base classes:  
  • StringString storage and manipulation
  • CNVListName/value pair list management
  • htmlObjectGeneric HTML element class
  • htmlGroupGeneric class for storing groups of HTML element classes
 
Other classes, incorporating feature-specific member methods for each HTML tag, are derived from the base classes.  
 
Several string storage classes exist for C++, and many application frameworks, including the Microsoft Foundation Classes, provide their own support for strings. Since html++ is designed to be a cross-platform solution, we chose to implement our own String class that works consistently across all systems. If you've used other string classes before, you'll find everything you've been used to, plus a few extras like built-in string parsing.  
 
Attributes and name/value pairs
The HTML specification allows tags to have various options that affect their behavior and appearance. These options appear as space-separated strings of the form "name=value". The htmlObject class defines several member methods for managing name/value pairs for all html++ objects:  
   AttributeExists()  
   GetAttribute()  
   GetAttributeAsInt()  
   SetAttribute()  
   ClearAttribute()  
 
Each htmlObject-derived class provides specific member methods appropriate to the particular HTML tag it encapsulates. You can quickly tell what options are available for any tag simply by examining the list of member methods for it's html++ class. You'll rarely need to use the get/set attribute methods directly, but they're there in case you need to use extended attributes (such as for JavaScript) or new HTML tags. Here's an example of using SetAttribute() to specify a JavaScript handler for the JavaScript "onMouseOver" event with a hyperlink:  
 
   htmlHyperLink hlink( "http://www.cnn.com" ) ;  
   hlink.SetAttribute( "onMouseOver""Mover(0);" ) ;  
 
Adding and inserting objects
Unlike htmlObject-derived classes, any object derived from htmlGroup can contain other html++ objects. An example of such an object is htmlTable. Tables can contain rows, which can in turn contain cells. Likewise, htmlTable accepts multiple htmlTableRow objects, which in turn can accept multiple htmlTableCell objects. Pages are another example. htmlPage can contain multiple htmlObject-derived objects, which is how documents are created.  
 
html++ provides two ways of adding and inserting objects within other objects: the Add() method and the << insertion operator. The Add() method is declared as a virtual function, and accepts a reference to an html++ object (the routines are overloaded to accept both String and htmlObject objects). As the name implies, a copy of the referenced object is added to the parent object.  
 
While the Add() method is useful, the preferred way of adding objects is to use the << insertion operator. The << insertion operator uses the virtual Add() method, so objects that override the Add() method modify the behavior of the << operator at the same time.  
 
Internally, html++ converts String insertions to htmlText objects so that they will display properly on a browser, relieving you of having to insert special codes such as "&gt;" instead of simply ">". If you don't want the strings converted for you, add them as htmlLiteral objects instead:  
   page << htmlLiteral( "This is literal text: < > & '" ) ;  
 
Generally, the htmlGroup base-class implementations for Add() and << are not overridden, meaning that the objects will accept any htmlObject-derived object. However, certain HTML tags restrict the types of tags and attributes that can be used, and in such cases the related html++ classes provide overriden versions of Add() and/or the << operator.  
 
An example of such a class is htmlFrameSet, which only accepts htmlFrameSet and htmlFrame objects. This technique uses C++'s built-in type checking to ensure that developers cannot add objects inconsistent with the HTML specification.  
 
Common methods and operators
All html++ objects implement the following methods and operators:  
Copy constructor      htmlObject( const htmlObject& rhs)  
Assignment operator      htmlObject& operator = (const htmlObject& rhs )  
Streaming output operator   ostream& operator << (ostream& os,  
               const htmlObject& element )  
 
Return values
Wherever possible, modifier methods return a reference to the object being modifed. This allows methods of a class to be combined in a single statement to produce more readable code. For example:  
 
htmlTable   table ;  
table.Align( align_left ).Width( "100%" ).CellSpacing( 5 ) ;  


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.