GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/Util/XmlParser.h
00001 #ifndef __UTIL_XMLPARSER_H
00002 #define __UTIL_XMLPARSER_H
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <string>
00007 #include <list>
00008 #include <map>
00009 
00010 namespace Util
00011 {
00012   struct XmlHead
00013   {
00014     XmlHead() : is_xml(false) { }
00015 
00016     bool is_xml;
00017     std::map<std::string, std::string> atts;
00018   };
00019   
00020   struct XmlBody;
00021   class XmlDoc;
00022 
00023   struct XmlElement
00024   {
00025     XmlElement(XmlDoc* in_doc, XmlElement* parent_elem) : body(0), doc(in_doc), parent(parent_elem) { }
00026     XmlElement(XmlDoc* in_doc) : body(0), doc(in_doc), parent(0) { }
00027     ~XmlElement();
00028 
00029     void process_elements();
00030 
00031     std::string name;
00032     std::map<std::string, std::string> atts;
00033     XmlBody* body;
00034     XmlDoc* doc;
00035     XmlElement* parent;
00036   };
00037 
00038   struct XmlBody
00039   {
00040     XmlBody(XmlDoc* parent) : doc(parent), element(parent) { }
00041 
00042     void process_element();
00043 
00044     std::string text;
00045     XmlElement element;
00046     XmlDoc* doc;
00047   };
00048 
00049   typedef void (*XmlElementHandler)(XmlElement&);
00050 
00051   class XmlDoc
00052   {
00053   public:
00054     XmlDoc(const char* filename);
00055 
00056     bool is_valid() const { return head.is_xml; }
00057     void add_handler(const std::string& element_name, const XmlElementHandler& h) { handlers[element_name] = h; }
00058     void process_elements();
00059     void close() { infile.close(); }
00060 
00061     XmlHead head;
00062 
00063   private:
00064     friend struct XmlElement;
00065     friend struct XmlBody;
00066 
00067     std::ifstream infile;
00068     XmlBody body;
00069     std::map<std::string, XmlElementHandler> handlers;
00070   };
00071 
00072   std::ostream& operator<<(std::ostream& out, const XmlHead& head);
00073 }
00074 
00075 #endif // XMLPARSER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations