using namespace std; /** Attribute of an element */ class xattribute { public: text name; text value; xattribute(const text &, const text &); void set(const text &, const text &); }; /** Xml element */ class xelement { public: text name; // tag name vector attributes; text data; xelement() { } xelement(xelement *); xelement(const char *); void addAttribute(xattribute *); void addAttribute(const text &, const text &); }; /** Embedded script */ class xscript { vector lines; vector tabs; int index; int tab; public: xscript(); xscript(xscript *); inline int size() { return (int) lines.size(); } void push(text *); void push(const string &); void reset(); const char *next(); int getTab(); bool again(); void load(const char *); }; /** Comment line */ class xcomment { public: text data; xcomment() {}; xcomment(xcomment *); void operator=(const char *s) { data = text(s); } void operator=(const string &s) { data = text(s.c_str()); } }; /** Node of the dom tree */ class dom; class xnode { public: int type; int next; int child; int parent; int lastChild; dom *owner; enum { ELEMENT, SCRIPT, COMMENT }; union { xelement *element; xscript *script; xcomment *comment; }; /** Constructor Arguments: optionally another xnode and its owner */ xnode(); xnode(xnode *); xnode(dom *, xnode *); void set(xelement *); void set(xscript *); void set(xcomment *); /** Set Copies the type and the content from a xnode into this one other fields are unchanged or empty (usually follows a call to the constructor) */ void set(xnode *); // copy content and type }; /** The xml tree builder, reader and modifier */ class dom { private: vector header; vector nodes; FILE *domfile; bool fileFlag; bool htmlFlag; bool isFound; text name; int mode; // display or save mode enum { ELEMENT, SCRIPT, COMMENT }; xnode *node; union { xelement *element; xscript *script; xcomment *comment; }; int index; // current node number int top; int tab; public: static const int XML; static const int XSL; static const int SVG; static const int XUL; static const int LIGHT; // displays light xml static const int TREE; // displays as a tree dom(); dom(const char *); ~dom(); inline void setHtml() { htmlFlag = true; } inline int size() { return (int) nodes.size(); } inline bool empty() { return(nodes.size() < 2); } inline void setName(const char *s) { name = s; } inline text getName() { return name; } inline bool found() { return isFound; } int addNext(xnode *, int); int addChild(xnode *, int); int addNext(xelement *); int addNext(int, xelement *); int addChild(xelement *); int addChild(int, xelement *); int addNext(xscript *); int addChild(xscript *); int addNext(xcomment *); int addChild(xcomment *); int addAttribute(xattribute *); int addAttribute(int, xattribute *); int addAttribute(const text &, const text &); static const char *tabtxt; void indent(); // display the starting of a node void openTag(xelement *, int); // display the ending of a node void closeTag(xelement *, int); // display the content of a node void outData(const char *, int); void dump(); void subout(int, int); void display(int = XML); /** Save the xml source into a real xml file */ void save(const char *, int = XML); /** Append the xml source to a xml file */ void append(const char *, int = XML); /** Sends a line to screen or file */ void send(const char *); void sendText(const char *); /** Adds a line to the document */ void addComment(const char *); /** Adds a line to the header */ void addHeader(const char *); /** Adds a script file to the document */ void addScript(const char *); /** Adds a script to the document */ void addScript(xscript *); /** Adds a line to the current script */ void addLine(const string &); /** Sends header to output */ void outHeader(); /** Set 'index' to the root Returns a pointer of the instance of dom, for chaining */ dom *start(); /** Points out the node in the path. Nothing else. */ inline dom *at() { return this; } /** Returns a pointer on the instance of this class for chaining The attribute 'index' must point out a node or the root 'at' returns the first child node the element of which has the name in argument. */ dom *at(const char *); /** Selects an element according to these arguments: - the attribute, - the value of the attribute Returns the instance. Sets up the index to the right node, and error to false; if not found error is true; */ dom *at(const char *, const char *); /** Compare the attribute and the value in parameters for the currently indexed node. Return true or false. */ bool compare(const char *, const char *); /** Get the value of an attribute. The attribute 'element' must point out the element of the node to read Returns: - the content if the parameter is "data" or - the value of the attribute that has the name in parameter or - an empty text if name not found. */ text getValue(const text &); /** Get the data Input: index points out the node for the element. */ text getData(); /** Set a value Input: name of the attribute, value */ void setValue(const char *, const char *); /** Set the data Input: the text to assign as content of an element */ void setData(const char *); //========================= ITERATOR /** Moving the the first ‚l‚ment in the tree */ dom *reset(); /** Moving to the first ‚l‚ment at level */ dom *begin(); /** Points out the last node at the level. Set the error flag if no element preceeds at the level. */ dom *end(); /** Skip current node, points out the next one at the level. Set the error flag if no element follow at the level. */ inline dom *next() { return inc(); } dom *inc(); /** Points out the previous node at the level. Set the error flag if no element preceeds at the level. */ dom *dec(); /** Points out the first child of the current element */ dom *down(); /** Internal method */ /** SetParent arguments: none When a tag is closed or at user call, gets the successor of the parent */ dom *up(); //========================= STRUCTURE CHANGE /** Get Node argument: none Returns a pointer on the node pointed out inside a tree. */ xnode *getNode(); /** Get Element argument: none returns a pointer on the element pointed out inside the tree. If the node is not an element, return an empty element. */ xelement *getElement(); /** SetChild argument: a node or element (inside a xml document) Sets the node the child of the pointed out node, and links the current child as next node to the new node. */ void setChild(xnode *); void setChild(xelement *); /** SetNext argument: a node or element (inside a xml document) Sets the node the successor of the pointed out node, and links the current successor as next node to the new node. */ void setNext(xnode *); void setNext(xelement *); /** Remove Suppress the pointed out node. Childs are removed also. If it has a successor, it is linked to the previous node, but it there was no previous node, it becomes child to the parent. */ void remove(); void copyNext(xnode *); void copyChild(xnode *); void addTree(xnode *); void push(xnode *); void pushNext(xnode *); void pushChild(xnode *); void unknown(const char *); void noElement(const char *); };