1 //////////////////////////////////////////////////////////
2 /*********************************************************
3  *
4  * Triggerconf XML wrapper for the XercesC library
5  *
6  * This version of Triggerconf works with XercesC v3.x.x
7  *
8  *********************************************************/
9 //////////////////////////////////////////////////////////
10 
11 #include "triggerconf.h"
12 
Triggerconf(const string & _filename,bool _autocreateitems,bool _mustexist,bool _savechanges)13 Triggerconf::Triggerconf(const string& _filename, bool _autocreateitems, bool _mustexist, bool _savechanges)
14 	: file( _filename ), autocreate( _autocreateitems ), savechanges( _savechanges ) {
15 
16 	rootnode	= NULL;
17 	goodstate	= true;
18 	lasterror	= "";
19 
20 	try {
21 
22 		XMLPlatformUtils::Initialize();
23 
24 		//
25 		// if the file does not exists we create it with the root node
26 		// but only if the constructor parameter is fine with this
27 		//
28 
29 		FileHandle filehandle = XMLPlatformUtils::openFile (file.c_str ());
30 
31 		if (filehandle == NULL && _mustexist == false) {
32 
33 			static const XMLCh	gLS []	= {chLatin_L, chLatin_S, chNull};
34 			DOMImplementation*	impl	= DOMImplementationRegistry::getDOMImplementation(gLS);
35 
36 			XMLCh*			xroot	= XMLString::transcode( ROOT_NAME );
37 			DOMDocument*		doc	= impl->createDocument( 0, xroot, 0 );
38 			DOMElement*		elemrt	= doc->getDocumentElement();
39 			DOMLSSerializer* 	writer  = ((DOMImplementationLS*)impl)->createLSSerializer();
40 
41 			XMLString::release (&xroot);
42 
43 			if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
44 				writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
45 
46 			if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTBOM, true))
47 				writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTBOM, true);
48 
49 			XMLFormatTarget* target	= new LocalFileFormatTarget (file.c_str ());
50 			target->flush();
51 
52 			DOMLSOutput* output = ((DOMImplementationLS*)impl)->createLSOutput();
53         		output->setByteStream( target );
54             		writer->write( elemrt, output );
55 
56 			writer->release();
57 			doc->release();
58 
59 			delete output;
60 			delete target;
61 
62 		} else if (filehandle == NULL && _mustexist == true) {
63 
64 			setError ("file " + file + " does not exist");
65 			return;
66 
67 		} else {
68 
69 			XMLPlatformUtils::closeFile (filehandle);
70 
71 		}
72 
73 		//
74 		// parse the file
75 		//
76 
77 		parser		= new XercesDOMParser ();
78 		errhandler	= (ErrorHandler*) new HandlerBase ();
79 
80 		parser->setErrorHandler	(errhandler);
81 		parser->parse		(file.c_str ());
82 		rootnode		= getChildOfType (parser->getDocument (), DOMNode::ELEMENT_NODE);
83 
84 		if (rootnode != NULL) {
85 
86 			char* xmlstring = XMLString::transcode (rootnode->getNodeName ());
87 
88 			if (((string) ROOT_NAME).compare (xmlstring) == 0)
89 				resetError();
90 			else
91 				setError("invalid root item in file " + file);
92 
93 			XMLString::release (&xmlstring);
94 
95 		} else
96 			setError("parsing xml file " + file + " failed");
97 
98 	} catch (const XMLException& toCatch) {
99 		char* message = XMLString::transcode (toCatch.getMessage());
100 		setError ("failed parsing file " + file + ": " + message);
101 		XMLString::release(&message);
102 	}
103 	catch (const DOMException& toCatch) {
104 		char* message = XMLString::transcode (toCatch.msg);
105 		setError ("failed parsing file " + file + ": " + message);
106 		XMLString::release(&message);
107 	}
108 	catch (...) {
109 		setError( "failed parsing file " + file );
110 	}
111 
112 }
113 
~Triggerconf()114 Triggerconf::~Triggerconf ()
115 {
116 	if( savechanges ){
117 
118 		//
119 		// save file
120 		//
121 
122 		static const XMLCh	gLS []	= {chLatin_L, chLatin_S, chNull};
123 		DOMImplementation*	impl	= DOMImplementationRegistry::getDOMImplementation(gLS);
124 		DOMLSSerializer*	writer	= ((DOMImplementationLS*)impl)->createLSSerializer();
125 
126 		if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
127 			writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
128 
129 		if (writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTBOM, true))
130 			writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTBOM, true);
131 
132 		XMLFormatTarget* target	= new LocalFileFormatTarget (file.c_str ());
133 		target->flush();
134 
135 		DOMLSOutput* output = ((DOMImplementationLS*)impl)->createLSOutput();
136 		output->setByteStream( target );
137 		writer->write( rootnode , output );
138 
139 		writer->release();
140 		delete target;
141 		delete output;
142 
143 	} // if( savechanges )
144 
145 	//
146 	// delete all stuff here
147 	//
148 
149 	delete parser;
150 	delete errhandler;
151 
152 	//
153 	// now we can safely terminate xerces
154 	//
155 
156 	XMLPlatformUtils::Terminate();
157 }
158 
isGood()159 bool Triggerconf::isGood ()
160 {
161 	return goodstate;
162 }
163 
getError()164 string Triggerconf::getError ()
165 {
166 	return lasterror;
167 }
168 
selectModule(string modulename)169 DOMNode* Triggerconf::selectModule (string modulename)
170 {
171 	if(rootnode == NULL) {
172 		setError ("invalid root node");
173 		return NULL;
174 	}
175 
176 	DOMNodeList* childs = rootnode->getChildNodes ();
177 
178 	if (childs == NULL) {
179 
180 		if (autocreate == false) {
181 			setError ("module " + modulename + " not found");
182 			return NULL;
183 		} else {
184 			resetError ();
185 			createModule (modulename);
186 			return selectModule (modulename);
187 		}
188 
189 	} // if (childs == NULL)
190 
191 	for (unsigned int i=0; i<childs->getLength (); i++) {
192 
193 		DOMNode* node = childs->item (i);
194 		if (node == NULL || node->getNodeType () != DOMNode::ELEMENT_NODE) continue;
195 
196 		char* xmlstring = XMLString::transcode (node->getNodeName ());
197 
198 		if (((string) ELEMENT_MODULE_NAME).compare (xmlstring) != 0) {
199 			XMLString::release (&xmlstring);
200 			continue;
201 		}
202 
203 		XMLString::release (&xmlstring);
204 
205 		if ((getAttributeValue (node, ATTRIBUTE_NAME)).compare (modulename) == 0) {
206 			resetError ();
207 			return node;
208 		}
209 
210 	} // for (unsigned int i=0; i<childs->getLength (); i++)
211 
212 	if (autocreate == false) {
213 		setError ("module " + modulename + " not found");
214 		return NULL;
215 	} else {
216 		resetError ();
217 		createModule (modulename);
218 		return selectModule (modulename);
219 	}
220 }
221 
resetError()222 void Triggerconf::resetError ()
223 {
224 	goodstate = true;
225 	lasterror = "";
226 }
227 
setError(const string & err)228 void Triggerconf::setError (const string& err)
229 {
230 	goodstate = false;
231 	lasterror = err;
232 }
233 
selectSubmodule(string modulename,string submodulename)234 DOMNode* Triggerconf::selectSubmodule (string modulename, string submodulename)
235 {
236 	DOMNode* currentModule = selectModule (modulename);
237 
238 	if (currentModule == NULL) {
239 		setError ("invalid module: " + modulename);
240 		return NULL;
241 	}
242 
243 	DOMNodeList* childs = currentModule->getChildNodes ();
244 
245 	if (childs == NULL) {
246 
247 		if (autocreate == false) {
248 			setError ("submodule " + modulename + " not found");
249 			return NULL;
250 		} else {
251 			resetError ();
252 			createSubmodule (modulename, submodulename);
253 			return selectSubmodule (modulename, submodulename);
254 		}
255 
256 	} // if (childs == NULL)
257 
258 	for (unsigned int i=0; i<childs->getLength (); i++) {
259 
260 		DOMNode* node = childs->item (i);
261 		if (node == NULL || node->getNodeType () != DOMNode::ELEMENT_NODE) continue;
262 
263 		char* xmlstring = XMLString::transcode (node->getNodeName ());
264 
265 		if (((string) ELEMENT_SUBMODULE_NAME).compare (xmlstring) != 0) {
266 			XMLString::release (&xmlstring);
267 			continue;
268 		}
269 
270 		XMLString::release (&xmlstring);
271 
272 		if ((getAttributeValue (node, ATTRIBUTE_NAME)).compare (submodulename) == 0) {
273 			resetError ();
274 			return node;
275 		}
276 
277 	} // for (unsigned int i=0; i<childs->getLength (); i++)
278 
279 	if (autocreate == false) {
280 		setError ("submodule " + submodulename + " not found");
281 		return NULL;
282 	} else {
283 		resetError ();
284 		createSubmodule (modulename, submodulename);
285 		return selectSubmodule (modulename, submodulename);
286 	}
287 }
288 
selectConfigElement(string modulename,string submodulename,string configname)289 DOMNode* Triggerconf::selectConfigElement (string modulename, string submodulename, string configname)
290 {
291 	DOMNode* currentSubmodule = selectSubmodule (modulename, submodulename);
292 	if (currentSubmodule == NULL) {
293 		setError ("invalid submodule " + submodulename + " in module " + modulename);
294 		return NULL;
295 	}
296 
297 	DOMNodeList* childs = currentSubmodule->getChildNodes ();
298 	if (childs == NULL) {
299 		setError ("submodule " + submodulename + " in module " + modulename + " not found");
300 		return NULL;
301 	}
302 
303 	for (unsigned int i=0; i<childs->getLength (); i++) {
304 
305 		DOMNode* child = childs->item (i);
306 		if (child == NULL || child->getNodeType () != DOMNode::ELEMENT_NODE) continue;
307 
308 		char* xmlstring = XMLString::transcode (child->getNodeName ());
309 
310 		if (((string) ELEMENT_CONFIGITEM_NAME).compare (xmlstring) != 0) {
311 			XMLString::release (&xmlstring);
312 			continue;
313 		}
314 
315 		XMLString::release (&xmlstring);
316 
317 		if ((getAttributeValue (child, ATTRIBUTE_NAME)).compare (configname) == 0) {
318 			resetError ();
319 			return child;
320 		}
321 
322 	} // for (unsigned int i=0; i<childs->getLength (); i++)
323 
324 	if (autocreate == false) {
325 		setError ("configelement " + configname + " not found");
326 		return NULL;
327 	} else {
328 		resetError ();
329 		createConfigElement (modulename, submodulename, configname);
330 		return selectConfigElement (modulename, submodulename, configname);
331 	}
332 }
333 
getAttributeValue(DOMNode * element,string attributename)334 string Triggerconf::getAttributeValue (DOMNode* element, string attributename)
335 {
336 	if (element == NULL) return "";
337 	if (element->getNodeType () != DOMNode::ELEMENT_NODE) return "";
338 
339 	DOMElement*  	domelem		= (DOMElement*) element;
340 	XMLCh*		xattrname	= XMLString::transcode	(attributename.c_str ());
341 	const XMLCh*	xattrval	= domelem->getAttribute (xattrname);
342 	char*		charval		= XMLString::transcode	(xattrval);
343 	string		ret		= charval;
344 
345 	XMLString::release	(&xattrname);
346 	//XMLString::release	(&xattrval);
347 	XMLString::release	(&charval);
348 
349 	return ret;
350 }
351 
getConfigValue(string module,string submodule,string configname)352 string Triggerconf::getConfigValue (string module, string submodule, string configname)
353 {
354 	if (existsConfigElement (module, submodule, configname) == false) {
355 		setError ("invalid config element");
356 		return "";
357 	}
358 
359 	DOMNode* node = selectConfigElement (module, submodule, configname);
360 	if (node == NULL) return "";
361 
362 	resetError ();
363 	const XMLCh* xval	= node->getTextContent ();
364 	char*	charstring	= XMLString::transcode (xval);
365 	if(charstring != NULL){
366 		string	ret = charstring;
367 		XMLString::release (&charstring);
368 		return ret;
369 	}else{
370 		return "";
371 	}
372 }
373 
getConfigValue(string path)374 string Triggerconf::getConfigValue (string path)
375 {
376 	if (! checkItemCount (path, 3)) return "";
377 
378 	STRING_LIST items = tokenize (path);
379 	return getConfigValue (items [0], items [1], items [2]);
380 }
381 
getConfigAttributeValue(string module,string submodule,string configname,string attributename)382 string Triggerconf::getConfigAttributeValue(string module, string submodule, string configname, string attributename)
383 {
384 	if (! existsConfigElement (module, submodule, configname)) {
385 		setError ("invalid config element");
386 		return "";
387 	}
388 
389 	DOMNode* node = selectConfigElement (module, submodule, configname);
390 	assert (node->getNodeType () == DOMNode::ELEMENT_NODE);
391 	DOMElement* elem = (DOMElement*) node;
392 
393 	XMLCh*	xattrname	= XMLString::transcode (attributename.c_str ());
394 	const XMLCh* xattrval	= elem->getAttribute (xattrname);
395 	char*	charstring	= XMLString::transcode (xattrval);
396 	string	ret		= charstring;
397 
398 	XMLString::release	(&xattrname);
399 	XMLString::release	(&charstring);
400 
401 	resetError		();
402 
403 	return ret;
404 }
405 
getConfigAttributeValue(string path)406 string Triggerconf::getConfigAttributeValue (string path)
407 {
408 	if (! checkItemCount (path, 4)) return "";
409 
410 	STRING_LIST items = tokenize (path);
411 	return getConfigAttributeValue (items [0], items [1], items [2], items [3]);
412 }
413 
getSubmoduleAttributeValue(string module,string submodule,string attributename)414 string Triggerconf::getSubmoduleAttributeValue (string module, string submodule, string attributename)
415 {
416 	if (! existsSubmodule (module, submodule)) {
417 		setError ("invalid submodule");
418 		return "";
419 	}
420 
421 	DOMNode* node = selectSubmodule (module, submodule);
422 	assert (node->getNodeType () == DOMNode::ELEMENT_NODE);
423 	DOMElement* elem = (DOMElement*) node;
424 
425 	XMLCh*	xattrname	= XMLString::transcode (attributename.c_str ());
426 	const XMLCh* xattrval	= elem->getAttribute (xattrname);
427 	char*	charstring	= XMLString::transcode (xattrval);
428 	string	ret		= charstring;
429 
430 	XMLString::release	(&xattrname);
431 	XMLString::release	(&charstring);
432 
433 	resetError		();
434 
435 	return ret;
436 }
437 
tokenize(string path)438 Triggerconf::STRING_LIST Triggerconf::tokenize (string path)
439 {
440 	STRING_LIST ret;
441 
442 	int first = 0;
443 	int last = 0;
444 
445 	while (true) {
446 
447 		last = (int) path.find (PATH_DELIMITER, first);
448 
449 		if (last == (int)string::npos) {
450 			if (path.length () - first >= 1)
451 				ret.push_back (path.substr (first, path.length () - first));
452 			break;
453 		}
454 
455 		if (last-first >= 1)
456 			ret.push_back (path.substr (first, last-first));
457 
458 		first = last + 1;
459 	}
460 
461 	return ret;
462 }
463 
untokenize(STRING_LIST path)464 string Triggerconf::untokenize (STRING_LIST path)
465 {
466 	STRING_LIST::iterator begin = path.begin();
467 	STRING_LIST::iterator end = path.end();
468 	string ret;
469 
470 	for ( ; begin != end; begin++) {
471 
472 		if (ret.length () == 0)
473 			ret += *begin;
474 		else
475 			ret += (PATH_DELIMITER + *begin);
476 
477 	}
478 
479 	return ret;
480 }
481 
getChildOfType(DOMNode * root,short type,unsigned int index)482 DOMNode* Triggerconf::getChildOfType (DOMNode* root, short type, unsigned int index)
483 {
484 	if (root == NULL) return NULL;
485 	if (! root->hasChildNodes ()) return NULL;
486 
487 	DOMNodeList* childs = root->getChildNodes ();
488 	if (childs == NULL) return NULL;
489 
490 	unsigned int idx = 0;
491 
492 	for (unsigned int i=0; i<childs->getLength (); i++) {
493 		DOMNode* child = childs->item (i);
494 
495 		if (child->getNodeType () == type) {
496 			if (idx == index)
497 				return child;
498 			else
499 				idx++;
500 		}
501 	} // for (unsigned int i=0; i<childs->getLength (); i++)
502 
503 	return NULL;
504 }
505 
createModule(string module)506 void Triggerconf::createModule (string module)
507 {
508 	if (rootnode == NULL) return;
509 
510 	if (existsModule (module)) {
511 		resetError ();
512 		return;
513 	}
514 
515 	XMLCh* xmodulename	= XMLString::transcode (ELEMENT_MODULE_NAME);
516 	DOMElement* node	= rootnode->getOwnerDocument ()->createElement	(xmodulename);
517 	XMLString::release	(&xmodulename);
518 
519 	XMLCh* xattrname	= XMLString::transcode (ATTRIBUTE_NAME);
520 	XMLCh* xmodule		= XMLString::transcode (module.c_str ());
521 	node->setAttribute	(xattrname, xmodule);
522 
523 	XMLString::release	(&xattrname);
524 	XMLString::release	(&xmodule);
525 
526 	rootnode->appendChild (node);
527 
528 	resetError ();
529 }
530 
createSubmodule(string module,string submodule)531 void Triggerconf::createSubmodule (string module, string submodule)
532 {
533 	if (rootnode == NULL) return;
534 
535 	if (existsSubmodule (module, submodule)) {
536 		resetError ();
537 		return;
538 	}
539 
540 	DOMNode* currentModule	= selectModule (module);
541 	if (currentModule == NULL) return;
542 
543 	XMLCh* xsubmodule	= XMLString::transcode (ELEMENT_SUBMODULE_NAME);
544 	DOMElement* node	= rootnode->getOwnerDocument ()->createElement	(xsubmodule);
545 	XMLString::release	(&xsubmodule);
546 
547 	XMLCh* xattrname	= XMLString::transcode (ATTRIBUTE_NAME);
548 	XMLCh* xsubmodulename	= XMLString::transcode (submodule.c_str ());
549 	node->setAttribute	(xattrname, xsubmodulename);
550 	XMLString::release	(&xattrname);
551 	XMLString::release	(&xsubmodulename);
552 
553 	currentModule->appendChild (node);
554 
555 	resetError ();
556 }
557 
createSubmodule(string path)558 void Triggerconf::createSubmodule (string path)
559 {
560 	if (! checkItemCount (path, 2)) return;
561 
562 	STRING_LIST items = tokenize (path);
563 	createSubmodule (items [0], items [1]);
564 }
565 
createConfigElement(string module,string submodule,string configname)566 void Triggerconf::createConfigElement (string module, string submodule, string configname)
567 {
568 	if (rootnode == NULL) return;
569 
570 	if (existsConfigElement (module, submodule, configname)) {
571 		resetError ();
572 		return;
573 	}
574 
575 	DOMNode* currentSubmodule = selectSubmodule (module, submodule);
576 	if (currentSubmodule == NULL) return;
577 
578 	XMLCh* xitemname	= XMLString::transcode (ELEMENT_CONFIGITEM_NAME);
579 	DOMElement* node	= rootnode->getOwnerDocument ()->createElement	(xitemname);
580 	XMLString::release	(&xitemname);
581 
582 	XMLCh* xattrname	= XMLString::transcode (ATTRIBUTE_NAME);
583 	XMLCh* xconfigname	= XMLString::transcode (configname.c_str ());
584 	node->setAttribute	(xattrname, xconfigname);
585 	XMLString::release	(&xattrname);
586 	XMLString::release	(&xconfigname);
587 
588 	currentSubmodule->appendChild (node);
589 
590 	resetError ();
591 }
592 
createConfigElement(string path)593 void Triggerconf::createConfigElement (string path)
594 {
595 	if (! checkItemCount (path, 3))	return;
596 
597 	STRING_LIST items = tokenize (path);
598 	createConfigElement (items [0], items [1], items [2]);
599 }
600 
createConfigAttribute(string module,string submodule,string configname,string attributename)601 void Triggerconf::createConfigAttribute	(string module, string submodule, string configname, string attributename)
602 {
603 	//
604 	// attributes are always created, no matter if they exists or not
605 	// dublication is prevented by xercesc
606 	//
607 
608 	DOMNode* currentConfigElement = selectConfigElement (module, submodule, configname);
609 	if (currentConfigElement == NULL) return;
610 
611 	assert (currentConfigElement->getNodeType () == DOMNode::ELEMENT_NODE);
612 	DOMElement* elem = (DOMElement*) currentConfigElement;
613 
614 	XMLCh* xattrname	= XMLString::transcode (attributename.c_str ());
615 	XMLCh* xempty		= XMLString::transcode ("");
616 	elem->setAttribute	(xattrname, xempty);
617 	XMLString::release	(&xattrname);
618 	XMLString::release	(&xempty);
619 
620 	resetError ();
621 }
622 
createConfigAttribute(string path)623 void Triggerconf::createConfigAttribute (string path)
624 {
625 	if (! checkItemCount (path, 4)) return;
626 
627 	STRING_LIST items = tokenize (path);
628 	createConfigAttribute (items [0], items [1], items [2], items [3]);
629 }
630 
setConfigValue(string module,string submodule,string configname,string value)631 void Triggerconf::setConfigValue (string module, string submodule, string configname, string value)
632 {
633 	DOMNode* currentConfigElement = selectConfigElement (module, submodule, configname);
634 	if (currentConfigElement == NULL) return;
635 
636 	assert (currentConfigElement->getNodeType () == DOMNode::ELEMENT_NODE);
637 	DOMElement* elem = (DOMElement*) currentConfigElement;
638 
639 	XMLCh* xval		= XMLString::transcode (value.c_str ());
640 	elem->setTextContent	(xval);
641 	XMLString::release	(&xval);
642 
643 	resetError ();
644 }
645 
setConfigValue(string path,string value)646 void Triggerconf::setConfigValue (string path, string value)
647 {
648 	if (! checkItemCount (path, 3)) return;
649 
650 	STRING_LIST items = tokenize (path);
651 	setConfigValue (items[0], items[1], items[2], value);
652 }
653 
setConfigAttributeValue(string module,string submodule,string configname,string attributename,string value)654 void Triggerconf::setConfigAttributeValue (string module, string submodule, string configname, string attributename, string value)
655 {
656 	DOMNode* currentConfigElement = selectConfigElement (module, submodule, configname);
657 	if (currentConfigElement == NULL) return;
658 
659 	assert (currentConfigElement->getNodeType () == DOMNode::ELEMENT_NODE);
660 	DOMElement* elem = (DOMElement*) currentConfigElement;
661 
662 	XMLCh* xattrname	= XMLString::transcode (attributename.c_str ());
663 	XMLCh* xval		= XMLString::transcode (value.c_str ());
664 
665 	elem->setAttribute (xattrname, xval);
666 
667 	XMLString::release (&xattrname);
668 	XMLString::release (&xval);
669 
670 	resetError ();
671 }
672 
setConfigAttributeValue(string path,string value)673 void Triggerconf::setConfigAttributeValue (string path, string value)
674 {
675 	if (! checkItemCount (path, 4)) return;
676 
677 	STRING_LIST items = tokenize (path);
678 	setConfigAttributeValue (items [0], items [1], items [2], items [3], value);
679 }
680 
deleteModule(string module)681 void Triggerconf::deleteModule (string module)
682 {
683 	if (! existsModule (module)) return;
684 
685 	DOMNode* currentModule = selectModule (module);
686 	if (currentModule == NULL)	return;
687 
688 	rootnode->removeChild (currentModule);
689 	currentModule = NULL;
690 
691 	resetError ();
692 }
693 
deleteAllModules()694 void Triggerconf::deleteAllModules ()
695 {
696 	//
697 	// removed all modules. have to iterate over and over again
698 	// if we removed an item because the nodes reflect live changes
699 	// and removing an element changes the child count
700 	//
701 
702 	if (rootnode == NULL) return;
703 	resetError ();
704 
705 	DOMNodeList* childs = rootnode->getChildNodes ();
706 	if (childs == NULL) return;
707 
708 	bool removed;
709 
710 	do {
711 		removed = false;
712 
713 		for (unsigned int i=0; i<childs->getLength (); i++) {
714 
715 			DOMNode* child = childs->item (i);
716 			if (child == NULL) continue;
717 
718 			if (child->getNodeType () == DOMNode::ELEMENT_NODE ||
719 					child->getNodeType () == DOMNode::TEXT_NODE) {
720 
721 				rootnode->removeChild (child);
722 				removed = true;
723 				break;
724 			}
725 
726 		} // for (unsigned int i=0; i<childs->getLength (); i++)
727 
728 	} while (removed);
729 
730 	assert (getModuleNames().size() == 0);
731 }
732 
deleteSubmodule(string module,string submodule)733 void Triggerconf::deleteSubmodule (string module, string submodule)
734 {
735 	if (! existsSubmodule (module, submodule)) return;
736 
737 	DOMNode* currentModule = selectModule (module);
738 	if (currentModule == NULL) return;
739 
740 	DOMNode* currentSubmodule = selectSubmodule (module, submodule);
741 	if (currentSubmodule == NULL) return;
742 
743 	currentModule->removeChild (currentSubmodule);
744 	currentSubmodule = NULL;
745 
746 	resetError ();
747 }
748 
deleteSubmodule(string path)749 void Triggerconf::deleteSubmodule (string path)
750 {
751 	if (! checkItemCount (path, 2)) return;
752 
753 	STRING_LIST items = tokenize (path);
754 	deleteSubmodule (items [0], items [1]);
755 }
756 
deleteConfigElement(string module,string submodule,string configname)757 void Triggerconf::deleteConfigElement (string module, string submodule, string configname)
758 {
759 	if (! existsConfigElement (module, submodule, configname)) return;
760 
761 	DOMNode* currentSubmodule = selectSubmodule (module, submodule);
762 	if (currentSubmodule == NULL) return;
763 
764 	DOMNode* currentConfigElement = selectConfigElement (module, submodule, configname);
765 	if (currentConfigElement == NULL) return;
766 
767 	currentSubmodule->removeChild (currentConfigElement);
768 	currentConfigElement = NULL;
769 
770 	resetError ();
771 }
772 
deleteConfigElement(string path)773 void Triggerconf::deleteConfigElement (string path)
774 {
775 	if (! checkItemCount (path, 3)) return;
776 
777 	STRING_LIST items = tokenize (path);
778 	deleteConfigElement (items [0], items [1], items [2]);
779 }
780 
deleteConfigAttribute(string module,string submodule,string configname,string attributename)781 void Triggerconf::deleteConfigAttribute (string module, string submodule, string configname, string attributename)
782 {
783 	if (! existsConfigElement (module, submodule, configname)) return;
784 
785 	DOMNode* currentConfigElement = selectConfigElement (module, submodule, configname);
786 	if (currentConfigElement == NULL) return;
787 
788 	assert (currentConfigElement->getNodeType () == DOMNode::ELEMENT_NODE);
789 	DOMElement* elem = (DOMElement*) currentConfigElement;
790 
791 	XMLCh* xattrname	= XMLString::transcode (attributename.c_str ());
792 	elem->removeAttribute	(xattrname);
793 	XMLString::release	(&xattrname);
794 
795 	resetError ();
796 }
797 
deleteConfigAttribute(string path)798 void Triggerconf::deleteConfigAttribute (string path)
799 {
800 	if (! checkItemCount (path, 4)) return;
801 
802 	STRING_LIST items = tokenize (path);
803 	deleteConfigAttribute (items [0], items [1], items [2], items [3]);
804 }
805 
806 
checkItemCount(STRING_LIST path,unsigned int items)807 bool Triggerconf::checkItemCount (STRING_LIST path, unsigned int items)
808 {
809 	if (path.size () != items) {
810 
811 		char buf [16];
812 
813 #ifdef WIN32
814 		ultoa (items, buf, 10);
815 #else
816 		snprintf (buf, 10, "%d", items);
817 #endif
818 
819 		setError ("path " + untokenize (path) + " does not contain " + string(buf) + " items");
820 		return false;
821 
822 	} else
823 		return true;
824 }
825 
checkItemCount(string path,unsigned int items)826 bool Triggerconf::checkItemCount (string path, unsigned int items)
827 {
828 	return checkItemCount (tokenize (path), items);
829 }
830 
existsModule(string module)831 bool Triggerconf::existsModule (string module)
832 {
833 	bool autoval	= autocreate;
834 	autocreate	= false;
835 	bool exists	= selectModule (module) != NULL;
836 	autocreate	= autoval;
837 
838 	return exists;
839 }
840 
existsSubmodule(string module,string submodule)841 bool Triggerconf::existsSubmodule (string module, string submodule)
842 {
843 	bool autoval	= autocreate;
844 	autocreate	= false;
845 	bool exists	= selectSubmodule (module, submodule) != NULL;
846 	autocreate	= autoval;
847 
848 	return exists;
849 }
850 
existsSubmodule(string path)851 bool Triggerconf::existsSubmodule (string path)
852 {
853 	if (! checkItemCount (path, 2)) return false;
854 
855 	STRING_LIST items = tokenize (path);
856 	return existsSubmodule (items [0], items [1]);
857 }
858 
existsConfigElement(string module,string submodule,string configname)859 bool Triggerconf::existsConfigElement (string module, string submodule, string configname)
860 {
861 	bool autoval	= autocreate;
862 	autocreate	= false;
863 	bool exists	= selectConfigElement (module, submodule, configname) != NULL;
864 	autocreate	= autoval;
865 
866 	return exists;
867 }
868 
existsConfigElement(string path)869 bool Triggerconf::existsConfigElement (string path)
870 {
871 	if (! checkItemCount (path, 3)) return false;
872 
873 	STRING_LIST items = tokenize (path);
874 	return existsConfigElement (items [0], items [1], items [2]);
875 }
876 
existsConfigAttribute(string module,string submodule,string configname,string attributename)877 bool Triggerconf::existsConfigAttribute (string module, string submodule, string configname, string attributename)
878 {
879 	bool autoval	= autocreate;
880 	autocreate	= false;
881 
882 	DOMNode* currentConfigElement = selectConfigElement (module, submodule, configname);
883 	autocreate = autoval;
884 	if (currentConfigElement == NULL) return false;
885 
886 	assert (currentConfigElement->getNodeType () == DOMNode::ELEMENT_NODE);
887 	DOMElement* elem = (DOMElement*) currentConfigElement;
888 
889 	XMLCh* xattrname	= XMLString::transcode (attributename.c_str ());
890 	bool hasattr		= elem->getAttributeNode (xattrname) != NULL;
891 	XMLString::release	(&xattrname);
892 
893 	return hasattr;
894 }
895 
existsConfigAttribute(string path)896 bool Triggerconf::existsConfigAttribute	(string path)
897 {
898 	if (! checkItemCount (path, 4)) return false;
899 
900 	STRING_LIST items = tokenize (path);
901 	return existsConfigAttribute (items [0], items [1], items [2], items [3]);
902 }
903 
getChildsAttribute(DOMNode * root,string nodename,string attribute)904 Triggerconf::STRING_LIST Triggerconf::getChildsAttribute (DOMNode* root, string nodename, string attribute)
905 {
906 	STRING_LIST ret;
907 
908 	if (root == NULL) {
909 		setError ("invalid root node");
910 		return ret;
911 	}
912 
913 	DOMNodeList* childs = root->getChildNodes ();
914 	if (childs == NULL) return ret;
915 
916 	for (unsigned int i=0; i<childs->getLength (); i++) {
917 
918 		DOMNode* child = childs->item (i);
919 		if (child == NULL || child->getNodeType () != DOMNode::ELEMENT_NODE) continue;
920 
921 		const XMLCh* xnodename = child->getNodeName ();
922 		char* charname = XMLString::transcode (xnodename);
923 
924 		if (nodename.compare (charname) == 0)
925 			ret.push_back (getAttributeValue (child, attribute));
926 
927 		XMLString::release (&charname);
928 
929 	} // for (unsigned int i=0; i<childs->getLength (); i++)
930 
931 	return ret;
932 }
933 
getModuleNames()934 Triggerconf::STRING_LIST Triggerconf::getModuleNames ()
935 {
936 	return getChildsAttribute (rootnode, ELEMENT_MODULE_NAME, ATTRIBUTE_NAME);
937 }
938 
getSubmoduleNames(string module)939 Triggerconf::STRING_LIST Triggerconf::getSubmoduleNames (string module)
940 {
941 	if (! existsModule (module)) return STRING_LIST ();
942 	DOMNode* root = selectModule (module);
943 
944 	return getChildsAttribute (root, ELEMENT_SUBMODULE_NAME, ATTRIBUTE_NAME);
945 }
946 
getConfigItemNames(string module,string submodule)947 Triggerconf::STRING_LIST Triggerconf::getConfigItemNames (string module, string submodule)
948 {
949 	if (! existsSubmodule (module, submodule)) return STRING_LIST ();
950 	DOMNode* root = selectSubmodule (module, submodule);
951 	return getChildsAttribute (root, ELEMENT_CONFIGITEM_NAME, ATTRIBUTE_NAME);
952 }
953 
getConfigItemNames(string path)954 Triggerconf::STRING_LIST Triggerconf::getConfigItemNames (string path)
955 {
956 	if (! checkItemCount (path, 2)) return STRING_LIST ();
957 	STRING_LIST items = tokenize (path);
958 	return getConfigItemNames (items [0], items [1]);
959 }
960 
getConfigAttributeNames(string module,string submodule,string configname)961 Triggerconf::STRING_LIST Triggerconf::getConfigAttributeNames (string module, string submodule, string configname)
962 {
963 	STRING_LIST ret;
964 
965 	if (! existsConfigElement (module, submodule, configname)) return ret;
966 
967 	DOMNode* node = selectConfigElement (module, submodule, configname);
968 	if (node == NULL || node->getNodeType () != DOMNode::ELEMENT_NODE) return ret;
969 
970 	DOMElement* elem = (DOMElement*) node;
971 	DOMNamedNodeMap* attributes = elem->getAttributes ();
972 
973 	for (unsigned int i=0; i<attributes->getLength (); i++) {
974 		DOMNode* attr = attributes->item (i);
975 		const XMLCh* xname = attr->getNodeName ();
976 		char* cname = XMLString::transcode (xname);
977 
978 		ret.push_back (cname);
979 		XMLString::release (&cname);
980 	}
981 
982 	return ret;
983 }
984 
getConfigAttributeNames(string path)985 Triggerconf::STRING_LIST Triggerconf::getConfigAttributeNames (string path)
986 {
987 	if (! checkItemCount (path, 3)) return STRING_LIST ();
988 
989 	STRING_LIST items = tokenize (path);
990 	return getConfigAttributeNames (items [0], items [1], items [2]);
991 }
992 
getConfigItemAttributes(string module,string submodule,string configitem)993 Triggerconf::ATTRIBUTE_MAP Triggerconf::getConfigItemAttributes (string module, string submodule, string configitem)
994 {
995 	ATTRIBUTE_MAP ret;
996 	STRING_LIST names = getConfigAttributeNames (module, submodule, configitem);
997 
998 	STRING_LIST_ITERATOR	begin	= names.begin();
999 	STRING_LIST_ITERATOR	end	= names.end();
1000 
1001 	for ( ; begin != end; begin++) {
1002 
1003 		string value	= getConfigAttributeValue (module, submodule, configitem, *begin);
1004 		ret.insert	(ATTRIBUTE_PAIR (*begin, value));
1005 
1006 	} // for ( ; begin != end; begin++)
1007 
1008 	return ret;
1009 }
1010 
getConfigItemAttributes(string path)1011 Triggerconf::ATTRIBUTE_MAP Triggerconf::getConfigItemAttributes (string path)
1012 {
1013 	if (! checkItemCount (path, 3)) return ATTRIBUTE_MAP ();
1014 
1015 	STRING_LIST items = tokenize (path);
1016 	return getConfigItemAttributes (items [0], items [1], items [2]);
1017 }
1018