hpp::util::parser::ObjectFactory Class Reference

Class that catch XML Parser events for a specific tag and build the corresponding Object. More...

#include <hpp/util/parser.hh>

Inheritance diagram for hpp::util::parser::ObjectFactory:
[legend]

List of all members.

Public Types

typedef std::list
< ObjectFactory * > 
ObjectFactoryList

Public Member Functions

 ObjectFactory (ObjectFactory *parent=NULL, const XMLElement *element=NULL)
virtual ~ObjectFactory ()
void name (const std::string &n)
 Set the name.
void name (const char *n)
 See name(const std::string&)
template<typename T >
T * as ()
 Cast this class to any child class.
Events
virtual bool init ()
 Called when the object is created.
void setAttribute (const XMLAttribute *attr)
 Called for each attribute.
virtual void addTextChild (const XMLText *text)
 Add Text child.
virtual bool finishAttributes ()
 Called when all the attributes have been processed.
virtual void finishTags ()
 Called when all the child tags have been processed.
virtual void finishFile ()
 Called when parsing is finished.
Write to file
 ObjectFactory (const std::string &tagName, ObjectFactory *parent=NULL)
 Constructor for writing objects from scratch.
void addAttribute (const std::string &name, const std::string &value)
 Add an attribute.
XMLNodewrite (XMLNode *node) const
 Add this factory as child of the node argument.
Accessors
std::string tagName () const
 Return tag name of the element is any.
std::string name () const
 Return the content of the attribute name, or an empty string.
bool hasAttribute (const std::string &attr) const
 Check if an attribute was set.
std::string getAttribute (const std::string &attr) const
 Return a given attributes.
ObjectFactoryList getChildrenOfType (std::string type)
 Get a list of ObjectFactory whose tag name is type.
bool getChildOfType (std::string type, ObjectFactory *&o)
 Get the ObjectFactory whose tag name is type.

Protected Member Functions

 ObjectFactory (ObjectFactory *root)
ObjectFactoryparent ()
virtual ObjectFactoryroot ()
bool hasParent () const
const XMLElementXMLelement ()
virtual void impl_setAttribute (const XMLAttribute *attr)
virtual void impl_write (XMLElement *element) const
void addChild (ObjectFactory *child)
virtual std::ostream & print (std::ostream &os) const

Friends

std::ostream & operator<< (std::ostream &, const ObjectFactory &)

Detailed Description

Class that catch XML Parser events for a specific tag and build the corresponding Object.

Derive this class if you wish to extend the Parser. The event callbacks are called in the following order:

Note:
The derived class must have the following construtor
 DerivedFactory (ObjectFactory* parent, const XMLElement* element) :
       ObjectFactory (parent, element)
 {
   /*
 Keep in mind that it might be more convenient
 to build objects in an event callback, when some information
 has already been parsed.
    */
 }

Member Typedef Documentation


Constructor & Destructor Documentation

hpp::util::parser::ObjectFactory::ObjectFactory ( ObjectFactory parent = NULL,
const XMLElement element = NULL 
)

References addChild(), and root().

virtual hpp::util::parser::ObjectFactory::~ObjectFactory ( ) [inline, virtual]
hpp::util::parser::ObjectFactory::ObjectFactory ( const std::string &  tagName,
ObjectFactory parent = NULL 
)

Constructor for writing objects from scratch.

References addChild(), and root().

hpp::util::parser::ObjectFactory::ObjectFactory ( ObjectFactory root) [protected]

Member Function Documentation

void hpp::util::parser::ObjectFactory::addAttribute ( const std::string &  name,
const std::string &  value 
)

Add an attribute.

References name().

void hpp::util::parser::ObjectFactory::addChild ( ObjectFactory child) [protected]

References tagName().

Referenced by ObjectFactory().

void hpp::util::parser::ObjectFactory::addTextChild ( const XMLText text) [virtual]

Add Text child.

Reimplemented in hpp::util::parser::SequenceFactory< ValueType >.

template<typename T >
T* hpp::util::parser::ObjectFactory::as ( ) [inline]

Cast this class to any child class.

bool hpp::util::parser::ObjectFactory::finishAttributes ( ) [virtual]

Called when all the attributes have been processed.

Returns:
True to continue parsing this tag, False otherwise.
void hpp::util::parser::ObjectFactory::finishFile ( ) [virtual]

Called when parsing is finished.

void hpp::util::parser::ObjectFactory::finishTags ( ) [virtual]

Called when all the child tags have been processed.

std::string hpp::util::parser::ObjectFactory::getAttribute ( const std::string &  attr) const

Return a given attributes.

References hppDout.

bool hpp::util::parser::ObjectFactory::getChildOfType ( std::string  type,
ObjectFactory *&  o 
)

Get the ObjectFactory whose tag name is type.

Parameters:
[out]oSet to the first element of the requested type.
Returns:
true if there was only element of the requested type. false if there are more than one.
Exceptions:
std::invalid_argumentif no ObjectFactory of the requested type exists.

References getChildrenOfType(), hppDout, and tagName().

std::list< ObjectFactory * > hpp::util::parser::ObjectFactory::getChildrenOfType ( std::string  type)

Get a list of ObjectFactory whose tag name is type.

Referenced by getChildOfType().

bool hpp::util::parser::ObjectFactory::hasAttribute ( const std::string &  attr) const

Check if an attribute was set.

bool hpp::util::parser::ObjectFactory::hasParent ( ) const [protected]
void hpp::util::parser::ObjectFactory::impl_setAttribute ( const XMLAttribute attr) [protected, virtual]

Referenced by setAttribute().

void hpp::util::parser::ObjectFactory::impl_write ( XMLElement element) const [protected, virtual]
bool hpp::util::parser::ObjectFactory::init ( ) [virtual]

Called when the object is created.

Returns:
True to continue parsing this tag, False otherwise.

Reimplemented in hpp::util::parser::IgnoreTagFactory.

std::string hpp::util::parser::ObjectFactory::name ( ) const

Return the content of the attribute name, or an empty string.

Referenced by addAttribute(), name(), print(), and setAttribute().

void hpp::util::parser::ObjectFactory::name ( const std::string &  n)

Set the name.

The default value is the value of the attribute "name" of the XML tag or an empty string if this does not exist.

void hpp::util::parser::ObjectFactory::name ( const char *  n)
ObjectFactory * hpp::util::parser::ObjectFactory::parent ( ) [protected]
std::ostream & hpp::util::parser::ObjectFactory::print ( std::ostream &  os) const [protected, virtual]

References name(), and tagName().

Referenced by hpp::util::parser::operator<<().

ObjectFactory * hpp::util::parser::ObjectFactory::root ( ) [protected, virtual]

Referenced by ObjectFactory().

void hpp::util::parser::ObjectFactory::setAttribute ( const XMLAttribute attr)

Called for each attribute.

A few reserved name are automatocally catched. The reserved names are "name" and "id". "name" expects a string. "id" expects an unsigned integer and can be use to define pointers to elements.

References hppDout, impl_setAttribute(), and name().

std::string hpp::util::parser::ObjectFactory::tagName ( ) const

Return tag name of the element is any.

Returns "No element" otherwise.

Referenced by addChild(), getChildOfType(), print(), and write().

XMLNode * hpp::util::parser::ObjectFactory::write ( XMLNode node) const

Add this factory as child of the node argument.

Get a new XMLElement from the content of this factory.

Tags are handled throught children so you should add children before calling this function. If you factory must write something different from tags (XMLText or XMLComment), reimplement method impl_write.

References impl_write(), and tagName().

const XMLElement * hpp::util::parser::ObjectFactory::XMLelement ( ) [protected]

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const ObjectFactory o 
) [friend]