BOSS 8.0.0
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
2// 06:10:27 maqm Exp $
3
4#include <xercesc/dom/DOMDocument.hpp>
5#include <xercesc/dom/DOMElement.hpp>
6
7#include "xmlBase/Dom.h"
8#include "xmlBase/XmlParser.h"
9#include "xmlBase/docMan/DocClient.h"
10#include "xmlBase/docMan/DocMan.h"
11
12namespace xmlBase {
13 XERCES_CPP_NAMESPACE_USE
14
15 DocMan* DocMan::m_self = 0;
16
18 m_self = this;
19 m_meFirst = 0;
20 m_parser = new xmlBase::XmlParser();
21 }
22
24 if ( m_self == 0 ) { m_self = new DocMan(); }
25 return m_self;
26 }
27
28 bool DocMan::parse( const std::string& filename, const std::string& docType ) {
29 // using xercesc::DOMDocument;
30 // using xercesc::DOMElement;
31
32 DOMDocument* doc = m_parser->parse( filename.c_str(), docType );
33 if ( doc == 0 ) return false;
34
35 if ( m_meFirst != 0 ) m_meFirst->handleChild( doc );
36
37 DOMElement* root = doc->getDocumentElement();
38
39 DOMElement* child = xmlBase::Dom::getFirstChildElement( root );
40
41 while ( child != 0 )
42 {
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
49 child = xmlBase::Dom::getSiblingElement( child );
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 {
63 curList = new ClientList( eltName );
64 m_lists.push_back( curList );
65 }
66 return curList->add( client );
67 }
68
69 bool DocMan::regMeFirst( DocClient* client ) {
70 if ( m_meFirst != 0 ) return false;
71
72 m_meFirst = client;
73 return true;
74 }
75
76 DocMan::ClientList* DocMan::findList( const std::string& eltName ) {
77 unsigned int ix;
78
79 for ( ix = 0; ix < m_lists.size(); ix++ )
80 {
81 ClientList* cur = m_lists[ix];
82 if ( eltName.compare( cur->getName() ) == 0 ) return cur;
83 }
84 return 0;
85 }
86
87 bool DocMan::ClientList::add( DocClient* client, bool front ) {
88 ClientsIt it = m_clients.begin();
89 const std::string& clientName = client->getName();
90 while ( it != m_clients.end() )
91 {
92 // can't register for the same element type more than once
93 if ( clientName.compare( ( *it )->getName() ) == 0 ) return 0;
94 ++it;
95 }
96 if ( front ) m_clients.insert( m_clients.begin(), client );
97 else m_clients.push_back( client );
98
99 return true;
100 }
101
102 // This is just here for completeness. Don't need a real
103 // implementation yet because no one calls it
105 client->getName(); // just to avoid compile-time warning
106 return true;
107 }
108
109 void DocMan::ClientList::invoke( DOMElement* elt ) {
110 ClientsIt it = m_clients.begin();
111 while ( it != m_clients.end() )
112 {
113 ( *it )->handleChild( elt );
114 ++it;
115 }
116 }
117
118 // Still need destructors
119 DocMan::ClientList::~ClientList() { m_clients.clear(); }
120
122 ListsIt it = m_lists.begin();
123 while ( it != m_lists.end() )
124 {
125 delete ( *it );
126 ++it;
127 }
128 delete m_parser;
129 }
130} // namespace xmlBase
std::string root
virtual const std::string & getName()=0
Nested class to keep track of clients for one element type.
void invoke(DomElement *elt)
call back each client in turn
bool add(DocClient *client, bool front=false)
Add a client to list.
Definition DocMan.cxx:87
bool remove(DocClient *client)
Definition DocMan.cxx:104
DocMan allows different clients to share a single xml document.
virtual bool regClient(const std::string &eltName, DocClient *client)
Definition DocMan.cxx:59
virtual ~DocMan()
Definition DocMan.cxx:121
static DocMan * getPointer()
Implements singleton.
Definition DocMan.cxx:23
bool regMeFirst(DocClient *client)
Register privileged client; only available to derived classes.
Definition DocMan.cxx:69
virtual bool parse(const std::string &filename, const std::string &docType=std::string(""))
Definition DocMan.cxx:28
ClientList * findList(const std::string &eltName)
Definition DocMan.cxx:76
static std::string getTagName(const DOMElement *node)
Definition Dom.cxx:134
static DOMElement * getSiblingElement(const DOMNode *child)
Return next element sibling, if any.
Definition Dom.cxx:90
static DOMElement * getFirstChildElement(const DOMNode *parent)
Get first child which is an element node, if any.
Definition Dom.cxx:109
#define ix(i)