#ifndef XML_H
#define XML_H
///////////////////////////////////////////////////////////////
// xml.h - build, format, and parse xml objects //
// ver 1.0 //
// //
// Language: Visual C++, ver 6.0 //
// Platform: Del XPS1000, Windows 2000 Professional //
// Application: CSE691, Pr1 demo, Fall 2000 //
// Author: Jim Fawcett, CST 2-187, Syracuse Univ. //
// (315) 443-3948, jfawcett@twcny.rr.com //
///////////////////////////////////////////////////////////////
/*
Module Operations:
==================
This module contains classes used to build, format, and
parse xml representation strings:
xmlRep - base representation class
xmlElement - class representing xml tagged elements
xmlDocument - class representing xml tagged documents
It also defines a class to format xml strings:
xmlFormat - makes xml string readable for humans
And defines a class to parse xml strings:
xmlParse - extracts elements, their tags, attributes,
and payloads from xml documents
The parser's action is complicated by the fact that an xml
element's payload may be one or more xml elements.
Public Interfaces:
==================
xmlRep base class
-----------------
xmlRep rep1; // creates an empty rep object
rep1.tag("myTag"); // called first, inserts
rep1.attr("name1","value1");
//
rep1.data("a string");
// a string
xmlRep rep2;
rep2.closedTag("yourTag");
//
rep2.attr("name2","value2");
//
rep1.tagStr(); // returns myTag
rep1.attrStr(); // returns name1="value1"
rep1.dataStr(); // returns a string
*/
//
/*
xmlElement class
----------------
xmlElement has the same interface as xmlRep for structuring
xml objects.
xmlElement el1, el2, el3; // creates empty xml elements
// instructions to structure elements el1 and el2 here
el3.tag("el3"); // endows el3 with a tag;
el3.data(el1).data(el2); // makes el1 and el2 data elements of el3
xmlDocument class
-----------------
The xmlDocument class has exactly the same interface as xmlElement.
It simply adds the xml declaration: to its
element hierarchy.
xmlFormat class
---------------
xmlFormat view; // create a formatter object
view.format(xmlStr); // format an xml string representation
xmlParse class
--------------
xmlParse parse(xmlStr); // creates a parser for xmlStr string;
parse.tagStr(); // returns tag string
parse.attrStr(); // returns attribute string
parse.dataStr(); // returns data string
parse.str(); // returns the current state of xmlStr;
parse.accept(xmlStr); // restarts with a new xml string
parse.accept(parse.dataStr());
// pushes down to a lower level
parse.next(); // selects the next sibling element
*/
///////////////////////////////////////////////////////////////
// Build Process
///////////////////////////////////////////////////////////////
// Required Files:
// xml.h, xml.cpp
//
// compiler command:
// cl /GX /DTEST_XML xml.cpp
///////////////////////////////////////////////////////////////
/*
Maintenance History:
====================
ver 1.0 : 6 Sep 00
- first release
*/
//
#pragma warning(disable : 4786)
#include
#include
///////////////////////////////////////////////////////////////
// class xmlFormat - makes representation human readable
class xmlFormat {
public:
xmlFormat();
std::string format(const std::string &rep);
void reset();
private:
void nextTag();
bool isBegTag();
bool isEndTag();
bool isClosedTag();
bool isPayload();
void eatWhiteSpace();
void eatTag();
void eatElement();
void insertBreak();
int pos;
int indent;
std::string xmlStr;
};
///////////////////////////////////////////////////////////////
// class xmlRep - base class for building xml representations
class xmlRep {
public:
virtual ~xmlRep() { }
virtual xmlRep& tag(const std::string &tag);
xmlRep& closedTag(const std::string &tag);
virtual xmlRep& data(const std::string &d);
virtual xmlRep& attr(const std::string &name, const std::string &value);
std::string str() const;
virtual std::string format();
virtual std::string tagStr() const;
virtual std::string attrStr() const;
virtual std::string dataStr() const;
protected:
std::string xmlStr;
std::string xmlTag;
xmlFormat view;
};
inline std::string xmlRep::str() const { return xmlStr; }
inline std::string xmlRep::format() { return view.format(xmlStr); }
//
///////////////////////////////////////////////////////////////
// class xmlElement - builds representation of xml element
class xmlElement : public xmlRep {
public:
xmlRep& data(const std::string &d);
xmlElement& data(const xmlElement &element);
};
///////////////////////////////////////////////////////////////
// class xmlDocument - builds representation of xml document
class xmlDocument : public xmlRep {
public:
virtual xmlRep& tag(const std::string &tag);
virtual xmlRep& data(const std::string &d);
xmlDocument& data(const xmlElement &element);
};
///////////////////////////////////////////////////////////////
// class xmlParse - extracts tags, attributes, and payloads
class xmlParse : private xmlRep {
public:
xmlParse(const std::string &repStr);
xmlParse(const xmlRep &rep);
void accept(const std::string &repStr);
void accept(const xmlRep &rep);
bool next();
std::string tagStr() const;
std::string attrStr() const;
std::string dataStr() const;
std::string str() const;
private:
int eatTag() const;
int endTag(const std::string &tag) const;
void stripDecl();
};
inline std::string xmlParse::str() const { return xmlStr; }
#endif