BOSS 7.0.7
BESIII Offline Software System
Loading...
Searching...
No Matches
DocMan.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/xmlBase/src/docMan/DocMan.cxx,v 1.1.1.1 2005/10/17 06:10:27 maqm Exp $
2
3#include <xercesc/dom/DOMElement.hpp>
4#include <xercesc/dom/DOMDocument.hpp>
5
6#include "xmlBase/XmlParser.h"
7#include "xmlBase/Dom.h"
10
11namespace xmlBase {
12XERCES_CPP_NAMESPACE_USE
13
14 DocMan* DocMan::m_self = 0;
15
17 m_self = this;
18 m_meFirst = 0;
19 m_parser = new xmlBase::XmlParser();
20 }
21
23 if (m_self == 0) {
24 m_self = new DocMan();
25 }
26 return m_self;
27 }
28
29 bool DocMan::parse(const std::string& filename, const std::string& docType) {
30 // using xercesc::DOMDocument;
31 // using xercesc::DOMElement;
32
33 DOMDocument* doc = m_parser->parse(filename.c_str(), docType);
34 if (doc == 0) return false;
35
36 if (m_meFirst != 0) m_meFirst->handleChild(doc);
37
38 DOMElement* root = doc->getDocumentElement();
39
40 DOMElement* child = xmlBase::Dom::getFirstChildElement(root);
41
42 while (child != 0) {
43 std::string eltName = Dom::getTagName(child);
44 if (m_meFirst != 0) m_meFirst->handleChild(child);
45
46 ClientList* list = findList(eltName);
47 if (list) list->invoke(child);
48
50 }
51
52 // Clean up call for privileged client
53 if (m_meFirst != 0) m_meFirst->handleChild(0);
54
55 m_parser->reset();
56 return true;
57 }
58
59 bool DocMan::regClient(const std::string& eltName, DocClient* client) {
60 ClientList* curList = findList(eltName);
61 if (curList == 0) {
62 curList = new ClientList(eltName);
63 m_lists.push_back(curList);
64 }
65 return curList->add(client);
66 }
67
69 if (m_meFirst != 0) return false;
70
71 m_meFirst = client;
72 return true;
73 }
74
75 DocMan::ClientList* DocMan::findList(const std::string& eltName) {
76 unsigned int ix;
77
78 for (ix = 0; ix < m_lists.size(); ix++) {
79 ClientList* cur = m_lists[ix];
80 if (eltName.compare(cur->getName()) == 0) return cur;
81 }
82 return 0;
83 }
84
85 bool DocMan::ClientList::add(DocClient* client, bool front) {
86 ClientsIt it = m_clients.begin();
87 const std::string& clientName = client->getName();
88 while (it != m_clients.end() ) {
89 // can't register for the same element type more than once
90 if (clientName.compare((*it)->getName()) == 0 ) return 0;
91 ++it;
92 }
93 if (front) m_clients.insert(m_clients.begin(), client);
94 else m_clients.push_back(client);
95
96 return true;
97 }
98
99 // This is just here for completeness. Don't need a real
100 // implementation yet because no one calls it
102 client->getName(); // just to avoid compile-time warning
103 return true;
104 }
105
106 void DocMan::ClientList::invoke(DOMElement* elt) {
107 ClientsIt it = m_clients.begin();
108 while (it != m_clients.end() ) {
109 (*it)->handleChild(elt);
110 ++it;
111 }
112 }
113
114 // Still need destructors
116 m_clients.clear();
117 }
118
120 ListsIt it = m_lists.begin();
121 while (it != m_lists.end()) {
122 delete (*it);
123 ++it;
124 }
125 delete m_parser;
126 }
127}
std::string root
Definition: CalibModel.cxx:39
virtual void handleChild(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node)=0
Must be overridden by derived classes.
virtual const std::string & getName()=0
Nested class to keep track of clients for one element type.
Definition: DocMan.h:52
void invoke(DomElement *elt)
call back each client in turn
Definition: DocMan.cxx:106
const std::string & getName()
which list are we?
Definition: DocMan.h:68
bool add(DocClient *client, bool front=false)
Add a client to list.
Definition: DocMan.cxx:85
bool remove(DocClient *client)
Definition: DocMan.cxx:101
DocMan allows different clients to share a single xml document.
Definition: DocMan.h:31
virtual bool regClient(const std::string &eltName, DocClient *client)
Definition: DocMan.cxx:59
virtual ~DocMan()
Definition: DocMan.cxx:119
static DocMan * getPointer()
Implements singleton.
Definition: DocMan.cxx:22
bool regMeFirst(DocClient *client)
Register privileged client; only available to derived classes.
Definition: DocMan.cxx:68
virtual bool parse(const std::string &filename, const std::string &docType=std::string(""))
Definition: DocMan.cxx:29
ClientList * findList(const std::string &eltName)
Definition: DocMan.cxx:75
static std::string getTagName(const DOMElement *node)
Definition: Dom.cxx:142
static DOMElement * getSiblingElement(const DOMNode *child)
Return next element sibling, if any.
Definition: Dom.cxx:96
static DOMElement * getFirstChildElement(const DOMNode *parent)
Get first child which is an element node, if any.
Definition: Dom.cxx:115
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
Definition: XmlParser.cxx:108
#define ix(i)
Definition: minoreval.cpp:387