00001 // $Header: /bes/bes/BossCvs/Calibration/xmlBase/src/docMan/DocMan.cxx,v 1.1.1.1 2005/10/17 06:10:27 maqm Exp $ 00002 00003 #include <xercesc/dom/DOMElement.hpp> 00004 #include <xercesc/dom/DOMDocument.hpp> 00005 00006 #include "xmlBase/XmlParser.h" 00007 #include "xmlBase/Dom.h" 00008 #include "xmlBase/docMan/DocMan.h" 00009 #include "xmlBase/docMan/DocClient.h" 00010 00011 namespace xmlBase { 00012 XERCES_CPP_NAMESPACE_USE 00013 00014 DocMan* DocMan::m_self = 0; 00015 00016 DocMan::DocMan() { 00017 m_self = this; 00018 m_meFirst = 0; 00019 m_parser = new xmlBase::XmlParser(); 00020 } 00021 00022 DocMan* DocMan::getPointer() { 00023 if (m_self == 0) { 00024 m_self = new DocMan(); 00025 } 00026 return m_self; 00027 } 00028 00029 bool DocMan::parse(const std::string& filename, const std::string& docType) { 00030 // using xercesc::DOMDocument; 00031 // using xercesc::DOMElement; 00032 00033 DOMDocument* doc = m_parser->parse(filename.c_str(), docType); 00034 if (doc == 0) return false; 00035 00036 if (m_meFirst != 0) m_meFirst->handleChild(doc); 00037 00038 DOMElement* root = doc->getDocumentElement(); 00039 00040 DOMElement* child = xmlBase::Dom::getFirstChildElement(root); 00041 00042 while (child != 0) { 00043 std::string eltName = Dom::getTagName(child); 00044 if (m_meFirst != 0) m_meFirst->handleChild(child); 00045 00046 ClientList* list = findList(eltName); 00047 if (list) list->invoke(child); 00048 00049 child = xmlBase::Dom::getSiblingElement(child); 00050 } 00051 00052 // Clean up call for privileged client 00053 if (m_meFirst != 0) m_meFirst->handleChild(0); 00054 00055 m_parser->reset(); 00056 return true; 00057 } 00058 00059 bool DocMan::regClient(const std::string& eltName, DocClient* client) { 00060 ClientList* curList = findList(eltName); 00061 if (curList == 0) { 00062 curList = new ClientList(eltName); 00063 m_lists.push_back(curList); 00064 } 00065 return curList->add(client); 00066 } 00067 00068 bool DocMan::regMeFirst(DocClient* client) { 00069 if (m_meFirst != 0) return false; 00070 00071 m_meFirst = client; 00072 return true; 00073 } 00074 00075 DocMan::ClientList* DocMan::findList(const std::string& eltName) { 00076 unsigned int ix; 00077 00078 for (ix = 0; ix < m_lists.size(); ix++) { 00079 ClientList* cur = m_lists[ix]; 00080 if (eltName.compare(cur->getName()) == 0) return cur; 00081 } 00082 return 0; 00083 } 00084 00085 bool DocMan::ClientList::add(DocClient* client, bool front) { 00086 ClientsIt it = m_clients.begin(); 00087 const std::string& clientName = client->getName(); 00088 while (it != m_clients.end() ) { 00089 // can't register for the same element type more than once 00090 if (clientName.compare((*it)->getName()) == 0 ) return 0; 00091 ++it; 00092 } 00093 if (front) m_clients.insert(m_clients.begin(), client); 00094 else m_clients.push_back(client); 00095 00096 return true; 00097 } 00098 00099 // This is just here for completeness. Don't need a real 00100 // implementation yet because no one calls it 00101 bool DocMan::ClientList::remove(DocClient* client) { 00102 client->getName(); // just to avoid compile-time warning 00103 return true; 00104 } 00105 00106 void DocMan::ClientList::invoke(DOMElement* elt) { 00107 ClientsIt it = m_clients.begin(); 00108 while (it != m_clients.end() ) { 00109 (*it)->handleChild(elt); 00110 ++it; 00111 } 00112 } 00113 00114 // Still need destructors 00115 DocMan::ClientList::~ClientList() { 00116 m_clients.clear(); 00117 } 00118 00119 DocMan::~DocMan() { 00120 ListsIt it = m_lists.begin(); 00121 while (it != m_lists.end()) { 00122 delete (*it); 00123 ++it; 00124 } 00125 delete m_parser; 00126 } 00127 }