Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
XMLIO.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 // must be first:
25 //#include "precompiled.h"
26 
27 #include "XMLIO.hh"
28 #include <Base/Common/W32Compat.hh>
29 
30 //#include <Base/Common/BIASpragma.hh>
31 
32 #include <sstream>
33 #include <vector>
34 #include <algorithm>
35 #include <cctype>
36 #include <string>
37 
38 #include <string>
39 #include <stdlib.h> // getenv
40 
41 #include <libxml/tree.h>
42 #include <libxml/encoding.h>
43 #include <libxml/parser.h>
44 
45 #include <Base/Common/FileHandling.hh>
46 
47 
48 using namespace BIAS;
49 using namespace std;
50 
51 
52 
54 
55  isInitialized_ = false;
56  RootNode_ = NULL;
57  CurrNode_ = NULL;
58  //Encoding_ = "ISO-8859-1";
59  Encoding_ = "UTF-8";
60 }
61 
62 
64 
65  if (isInitialized_) {
66  // do NOT use xmlCleanupParser() here when concurrent threads use XMLIO!
67  // see: http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser
68  //xmlCleanupParser();
69  xmlFreeDoc(xmlDoc_);
70  }
71 }
72 
73 
74 void XMLIO::clear() {
75 
76  if (isInitialized_) {
77  // do NOT use xmlCleanupParser() here when concurrent threads use XMLIO!
78  // see: http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser
79  //xmlCleanupParser();
80  xmlFreeDoc(xmlDoc_);
81  isInitialized_ = false;
82  }
83  RootNode_ = NULL;
84  CurrNode_ = NULL;
85 }
86 
87 
88 xmlNodePtr XMLIO::create(const std::string& RootNodeName) {
89 
90  if (xmlValidateName(BAD_CAST RootNodeName.c_str(), 0)!=0) {
91  BIASERR("'" << RootNodeName <<
92  "'is no valid node name, maybe contains blanks??\n");
93  return NULL;
94  }
95 
96  // Free the all existing XML pointers associated to the current document
97  if (isInitialized_) {
98  // do NOT use xmlCleanupParser() here when concurrent threads use XMLIO!
99  // see: http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser
100  //xmlCleanupParser();
101  xmlFreeDoc(xmlDoc_);
102  isInitialized_ = false;
103  }
104 
105  // reate a new XML DOM tree, to which the XML document will be written
106  xmlDoc_ = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
107  if (xmlDoc_ == NULL) {
108  BIASERR("Error creating the xml document tree\n");
109  return NULL;
110  }
111 
112  // Create a new XML node, to which the XML document will be appended
113  CurrNode_ = RootNode_ =
114  xmlNewDocNode(xmlDoc_, NULL, BAD_CAST RootNodeName.c_str(), NULL);
115  if (RootNode_ == NULL) {
116  xmlFreeDoc(xmlDoc_);
117  BIASERR("Error creating the root node\n");
118  return NULL;
119  }
120 
121  // Make ELEMENT the root node of the tree
122  xmlDocSetRootElement(xmlDoc_, RootNode_);
123 
124  isInitialized_ = true;
125 
126  return RootNode_;
127 
128 }
129 
130 
131 xmlNodePtr XMLIO::addChildNode(const xmlNodePtr ParentNode,
132  const std::string& NewNodeName) {
133 
134  if (xmlValidateName(BAD_CAST NewNodeName.c_str(), 0)!=0) {
135  BIASERR("'" << NewNodeName <<
136  "'is no valid node name, maybe contains blanks??\n");
137  return NULL;
138  }
139 
140  if (!isInitialized_) {
141  BIASERR("Not initialized: Please call createXML/loadXML first\n");
142  return NULL;
143  }
144 
145  CurrNode_ = xmlNewChild(ParentNode, NULL,
146  BAD_CAST NewNodeName.c_str(), NULL);
147  if (CurrNode_ == NULL) {
148  BIASERR("Error creating the child node\n");
149  return NULL;
150  }
151 
152  return CurrNode_;
153 
154 }
155 
156 void XMLIO::addAttribute(const xmlNodePtr Node,
157  const std::string& AttributeName,
158  const bool AttributeValue) {
159  stringstream stst;
160  if (AttributeValue) {
161  stst << "true";
162  } else {
163  stst << "false";
164  }
165  addAttribute(Node,AttributeName,stst.str());
166 
167 }
168 
169 void XMLIO::addAttribute(const xmlNodePtr Node,
170  const std::string& AttributeName,
171  const int AttributeValue) {
172  stringstream stst;
173  stst << AttributeValue;
174  addAttribute(Node,AttributeName,stst.str());
175 
176 }
177 
178 void XMLIO::addAttribute(const xmlNodePtr Node,
179  const std::string& AttributeName,
180  const double AttributeValue) {
181  stringstream stst;
182  LOCALE_C_ON
183  stst << setprecision(10);
184  stst << AttributeValue;
185  LOCALE_C_OFF
186  addAttribute(Node,AttributeName,stst.str());
187 
188 }
189 
190 void XMLIO::addAttribute(const xmlNodePtr Node,
191  const std::string& AttributeName,
192  const std::vector<double> &AttributeValue) {
193  stringstream stst;
194  LOCALE_C_ON
195  stst << setprecision(10);
196  for (int i=0; i<(int)AttributeValue.size()-1; i++){
197  stst << AttributeValue[i] << " ";
198  }
199  if (AttributeValue.size()>0)
200  stst << AttributeValue[AttributeValue.size()-1];
201  addAttribute(Node,AttributeName,stst.str());
202  LOCALE_C_OFF
203 
204 }
205 
206 void XMLIO::addAttribute(const xmlNodePtr Node,
207  const std::string& AttributeName,
208  const std::vector<int> &AttributeValue) {
209  stringstream stst;
210  for (int i=0; i<(int)AttributeValue.size()-1; i++){
211  stst << AttributeValue[i] << " ";
212  }
213  if (AttributeValue.size()>0)
214  stst << AttributeValue[AttributeValue.size()-1];
215 
216  addAttribute(Node,AttributeName,stst.str());
217 
218 }
219 
220 void XMLIO::addAttribute(const xmlNodePtr Node,
221  const std::string& AttributeName,
222  const std::string& AttributeValue) {
223 
224  if (xmlValidateName(BAD_CAST AttributeName.c_str(), 0)!=0) {
225  BIASERR("'" << AttributeName <<
226  "'is no valid attribute name, maybe contains blanks??\n");
227  return;
228  }
229 
230  if (!isInitialized_) {
231  BIASERR("Not initialized: Please call createXML/loadXML first\n");
232  return;
233  }
234 
235  xmlAttrPtr attPtr;
236  attPtr = xmlSetProp(Node, BAD_CAST AttributeName.c_str(),
237  BAD_CAST AttributeValue.c_str());
238  if (attPtr == NULL) {
239  BIASERR("Error creating the child node\n");
240  return;
241  }
242 
243 }
244 
245 void XMLIO::addAttribute(const xmlNodePtr Node,
246  const std::string& AttributeName,
247  const char* AttributeValue)
248 {
249  std::string toStr(AttributeValue);
250  addAttribute(Node, AttributeName, toStr);
251 }
252 
253 
254 void XMLIO::addContent(const xmlNodePtr Node, const std::string& Content) {
255 
256  if (!isInitialized_) {
257  BIASERR("Not initialized: Please call createXML/loadXML first\n");
258  return;
259  }
260 
261  xmlNodeAddContent(Node, BAD_CAST Content.c_str());
262 
263 }
264 
265 void XMLIO::addContent(const xmlNodePtr Node, const double Content) {
266 
267  if (!isInitialized_) {
268  BIASERR("Not initialized: Please call createXML/loadXML first\n");
269  return;
270  }
271 
272  stringstream ss;
273  LOCALE_C_ON
274  ss << setprecision(10);
275  ss << Content;
276  LOCALE_C_OFF
277  xmlNodeAddContent(Node, BAD_CAST ss.str().c_str());
278 
279 }
280 
281 void XMLIO::addContent(const xmlNodePtr Node, const int Content) {
282 
283  if (!isInitialized_) {
284  BIASERR("Not initialized: Please call createXML/loadXML first\n");
285  return;
286  }
287 
288  stringstream ss;
289  ss << Content;
290  xmlNodeAddContent(Node, BAD_CAST ss.str().c_str());
291 
292 }
293 
294 void XMLIO::addContent(const xmlNodePtr Node, const vector<int> Content) {
295 
296  if (!isInitialized_) {
297  BIASERR("Not initialized: Please call createXML/loadXML first\n");
298  return;
299  }
300 
301  stringstream ss;
302  for (unsigned int i=0; i<Content.size(); i++) {
303  ss << Content[i] << " ";
304  }
305  xmlNodeAddContent(Node, BAD_CAST ss.str().c_str());
306 
307 }
308 
309 void XMLIO::addComment(const xmlNodePtr Node, const std::string& Comment) {
310 
311  if (!isInitialized_) {
312  BIASERR("Not initialized: Please call createXML/loadXML first\n");
313  return;
314  }
315  xmlNodePtr myNode;
316  myNode = xmlNewComment(BAD_CAST Comment.c_str());
317  xmlAddChild(Node, myNode);
318 
319 }
320 
321 void XMLIO::setContent(const xmlNodePtr Node, const std::string& Content) {
322 
323  if (!isInitialized_) {
324  BIASERR("Not initialized: Please call createXML/loadXML first\n");
325  return;
326  }
327 
328  xmlNodeSetContent(Node, BAD_CAST Content.c_str());
329 
330 }
331 
332 
333 void XMLIO::setContent(const xmlNodePtr Node, const double Content) {
334 
335  if (!isInitialized_) {
336  BIASERR("Not initialized: Please call createXML/loadXML first\n");
337  return;
338  }
339 
340  stringstream ss;
341  LOCALE_C_ON
342  ss << setprecision(10);
343  ss << Content;
344  LOCALE_C_OFF
345  xmlNodeSetContent(Node, BAD_CAST ss.str().c_str());
346 
347 }
348 
349 
350 void XMLIO::setContent(const xmlNodePtr Node, const int Content) {
351 
352  if (!isInitialized_) {
353  BIASERR("Not initialized: Please call createXML/loadXML first\n");
354  return;
355  }
356 
357  stringstream ss;
358  ss << Content;
359  xmlNodeSetContent(Node, BAD_CAST ss.str().c_str());
360 
361 }
362 
363 
364 void XMLIO::setContent(const xmlNodePtr Node, const vector<int> Content) {
365 
366  if (!isInitialized_) {
367  BIASERR("Not initialized: Please call createXML/loadXML first\n");
368  return;
369  }
370 
371  stringstream ss;
372  for (unsigned int i=0; i<Content.size(); i++) {
373  ss << Content[i] << " ";
374  }
375  xmlNodeSetContent(Node, BAD_CAST ss.str().c_str());
376 
377 }
378 
379 int XMLIO::write(const std::string& FilenameConst,
380  bool AutoAddCompressionSuffix) const {
381  if (!isInitialized_) {
382  BIASERR("Not initialized: Please call createXML/loadXML first\n");
383  return -1;
384  }
385 
386  string FileName( FilenameConst ); // const const to add prefix and suffix
387 
388  // replace tilde prefix?
389  FileHandling::FilenameExpansion(FilenameConst, FileName);
390 
391  // add compression suffix?
392  if ((xmlGetDocCompressMode(xmlDoc_)>0) && AutoAddCompressionSuffix)
393  FileName+=".z" ;
394 
395 #ifdef BIAS_DEBUG
396  //BIASASSERT(this->xmlDoc_!=NULL);
397 #endif // BIAS_DEBUG
398 
399  BIASASSERT(this->xmlDoc_!=NULL);
400  LOCALE_C_ON
401 
402  int nBytes = xmlSaveFormatFileEnc(FileName.c_str(), this->xmlDoc_,
403  Encoding_.c_str(), 2 );
404  LOCALE_C_OFF
405 
406  if (nBytes < 0) {
407  BIASERR("Error saving XML file " << FileName << " ("
408  << FilenameConst << "), wrote " << nBytes << " bytes");
409  return -1;
410  }
411 
412  return 0; // OK
413 }
414 
415 
416 xmlNodePtr XMLIO::read(const std::string& FilenameConst) {
417  // needed to ensure consistent ,/. behavior in different envs
418 
419  std::string Filename(FilenameConst);
420 
421  // replace tilde prefix?
422  FileHandling::FilenameExpansion(FilenameConst, Filename);
423 
424  // Free the all existing XML pointers associated to the current document
425  if (isInitialized_) {
426  // do NOT use xmlCleanupParser() here when concurrent threads use XMLIO!
427  // see: http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser
428  //xmlCleanupParser();
429  xmlFreeDoc(xmlDoc_);
430  isInitialized_ = false;
431  }
432 
433  LOCALE_C_ON
434  xmlDoc_ = xmlParseFile(Filename.c_str());
435  LOCALE_C_OFF
436 
437  if (xmlDoc_ == NULL) {
438  BIASERR("Error parsing the xml document: '"<<Filename
439  <<"' XML syntax error/not well formed? Or file not found? \n");
440  //BIASBREAK;
441  return NULL;
442  }
443 
444  RootNode_ = CurrNode_ = xmlDocGetRootElement(xmlDoc_);
445  isInitialized_=true;
446 
447  return RootNode_;
448 
449 }
450 
451 
452 xmlNodePtr XMLIO::getFirstChild(const xmlNodePtr ParentNode) {
453 
454  CurrNode_ = ParentNode->children;
455  while (CurrNode_!=NULL) {
456  if (CurrNode_->type == XML_ELEMENT_NODE) {
457  return CurrNode_;
458  }
459  CurrNode_ = CurrNode_->next;
460  }
461  return NULL;
462 
463 }
464 
465 
466 xmlNodePtr XMLIO::getNextChild() {
467  return getNextChild(NULL);
468 }
469 
470 
471 xmlNodePtr XMLIO::getNextChild(const xmlNodePtr SiblingNode) {
472 
473  if (SiblingNode!=NULL) {
474  CurrNode_ = SiblingNode;
475  }
476 
477  while (CurrNode_->next!=NULL) {
478  CurrNode_ = CurrNode_->next;
479  if (CurrNode_->type == XML_ELEMENT_NODE) {
480  return CurrNode_;
481  }
482  }
483 
484  return NULL;
485 
486 }
487 
488 
489 xmlNodePtr XMLIO::getChild(const xmlNodePtr ParentNode,
490  const std::string& ChildName) {
491 
492  xmlNodePtr myNode;
493  myNode = getFirstChild(ParentNode);
494  while(myNode!=NULL) {
495  if (ChildName==getNodeName(myNode)) {
496  return myNode;
497  }
498  myNode = getNextChild();
499  }
500 
501  return NULL;
502 
503 }
504 
505 void
506 XMLIO::GetChildren(const xmlNodePtr ParentNode,
507  const std::string& ChildName,
508  std::vector<xmlNodePtr>& childrenWithSameName)
509 {
510  childrenWithSameName.clear();
511  xmlNodePtr currentChildNode;
512  currentChildNode = getFirstChild(ParentNode);
513  while(currentChildNode!=NULL) {
514  if (ChildName==getNodeName(currentChildNode)) {
515  childrenWithSameName.push_back(currentChildNode);
516  }
517  currentChildNode = getNextChild();
518  }
519 }
520 
521 xmlNodePtr XMLIO::GetChildWithAttributeValueString(const xmlNodePtr ParentNode,
522  const std::string& ChildName,
523  const std::string& AttributeName,
524  const std::string& AttributeValue) {
525 
526  xmlNodePtr myNode;
527  myNode = getFirstChild(ParentNode);
528  while(myNode!=NULL) {
529  if (ChildName==getNodeName(myNode)) {
530  if(getAttributeValueString(myNode, AttributeName) == AttributeValue) {
531  return myNode;
532  }
533  }
534  myNode = getNextChild();
535  }
536 
537  return NULL;
538 
539 }
540 
541 
542 
543 std::string XMLIO::getNodeName(const xmlNodePtr Node) const {
544 
545  std::string ret="";
546 
547  if ((Node!=NULL) && (Node->name!=NULL)) {
548  ret = (char*) Node->name;
549  }
550  return ret;
551 
552 }
553 
554 std::string XMLIO::getNodeContentString(const xmlNodePtr Node) const {
555 
556  std::string ret="";
557  std::string temp="";
558  if (Node->children!=NULL){
559  if(Node->children->content!=NULL){
560  temp = (char*) Node->children->content;
561  int front=0;
562  int rear=(int)temp.size()-1;
563  while(temp[front]==' ' || (int)temp[front]==32 || (int)temp[front]==10)
564  { front++; }
565  if (front >=(int)temp.size()) return "";
566  while(temp[rear]==' ' || (int)temp[rear]==32 || (int)temp[rear]==10)
567  { rear--; }
568  if (rear < 0) return "";
569  if (front>rear) return "";
570  ret.resize(rear-front+1);
571  for(int k=front; k<=rear; k++)
572  ret[k-front]=temp[k];
573  }
574  }
575 
576  return ret;
577 }
578 
579 int XMLIO::getNodeContentInt(const xmlNodePtr Node) const {
580 
581  std::string s;
582  s = getNodeContentString(Node);
583  return atoi(s.c_str());
584 
585 }
586 
587 vector<int> XMLIO::getNodeContentVectorInt(const xmlNodePtr Node) const {
588 
589  stringstream s(getNodeContentString(Node));
590  std::vector<int> ret;
591  int temp;
592  while (s >> temp){
593  ret.push_back(temp);
594  }
595  return ret;
596 }
597 
598 
599 vector<double> XMLIO::getNodeContentVectorDouble(const xmlNodePtr Node) const {
600 
601  stringstream s(getNodeContentString(Node));
602  std::vector<double> ret;
603  double temp;
604  LOCALE_C_ON
605  while (s >> temp){
606  ret.push_back(temp);
607  }
608  LOCALE_C_OFF
609  return ret;
610 }
611 
612 
613 double XMLIO::getNodeContentDouble(const xmlNodePtr Node) const {
614 
615  std::string s;
616  s = getNodeContentString(Node);
617  LOCALE_C_ON
618  double d = atof(s.c_str());
619  LOCALE_C_OFF;
620  return d;
621 
622 }
623 
624 xmlAttrPtr XMLIO::getFirstAttribute(const xmlNodePtr Node)
625 {
626  CurrAttr_ = Node->properties;
627  while (CurrAttr_!=NULL) {
628  if (CurrAttr_->type == XML_ATTRIBUTE_NODE) {
629  return CurrAttr_;
630  }
631  CurrAttr_ = CurrAttr_->next;
632  }
633  return NULL;
634 
635 }
636 
637 
639 
640  while (CurrAttr_->next!=NULL) {
641  CurrAttr_ = CurrAttr_->next;
642  if (CurrAttr_->type == XML_ATTRIBUTE_NODE) {
643  return CurrAttr_;
644  }
645  }
646  return NULL;
647 
648 }
649 
650 xmlAttrPtr XMLIO::getAttributeByName(const xmlNodePtr Node,
651  const std::string& attribute_name)
652 {
653 
654  //find first attribute for node
655  xmlAttrPtr result = getFirstAttribute(Node);
656  //while AttrPtr is valid search for requested name
657  while (result != NULL){
658  if (getAttributeName(result) == attribute_name) return result;
659  //not found, go to next
660  result = getNextAttribute();
661  }
662  //not found, returning NULL
663  return NULL;
664 }
665 
666 void XMLIO::
667 GetChildren(const xmlNodePtr ParentNode,
668  std::vector<xmlNodePtr>& children)
669 {
670  children.clear();
671  xmlNodePtr child = getFirstChild(ParentNode);
672  while (child != NULL){
673  children.push_back(child);
674  child = getNextChild();
675  }
676 }
677 
678 void XMLIO::
679 GetAttributes(const xmlNodePtr ParentNode,
680  std::vector<xmlAttrPtr>& attributes)
681 {
682  attributes.clear();
683  xmlAttrPtr attr = getFirstAttribute(ParentNode);
684  while (attr != NULL){
685  attributes.push_back(attr);
686  attr = getNextAttribute();
687  }
688 }
689 
690 std::string XMLIO::getAttributeName(const xmlAttrPtr Attribute) const {
691 
692  std::string ret="";
693  if (Attribute->name!=NULL) {
694  ret = (char*) Attribute->name;
695  }
696  return ret;
697 }
698 
699 
700 bool XMLIO::getAttributeValueBool(const xmlAttrPtr Attribute) const {
701 
702  std::string s;
703  s = getAttributeValueString(Attribute);
704  // transfrom to lower case letters for comparison
706  if (s == "true") {
707  return true;
708  } else if (s == "false" || s.empty()) {
709  return false;
710  } else {
711  BIASERR("Undefined boolean value \"" << s << "\" found! Using \"false\" instead!");
712  return false;
713  }
714 }
715 
716 std::string XMLIO::getAttributeValueString(const xmlAttrPtr Attribute) const {
717 
718  std::string ret="";
719  if (Attribute->children!=NULL) {
720  if (Attribute->children->content!=NULL) {
721  ret = (char*) Attribute->children->content;
722  }
723  }
724  return ret;
725 }
726 
727 
728 int XMLIO::getAttributeValueInt(const xmlAttrPtr Attribute) const {
729 
730  std::string s;
731  s = getAttributeValueString(Attribute);
732  return atoi(s.c_str());
733 }
734 
735 
736 double XMLIO::getAttributeValueDouble(const xmlAttrPtr Attribute) const {
737 
738  std::string s;
739  s = getAttributeValueString(Attribute);
740  LOCALE_C_ON
741  double d = atof(s.c_str());
742  LOCALE_C_OFF
743  return d;
744 }
745 
746 std::vector<double> XMLIO::
747 getAttributeValueVecDbl(const xmlAttrPtr Attribute) const {
748 
749  stringstream s(getAttributeValueString(Attribute));
750  std::vector<double> tempVec;
751  LOCALE_C_ON
752  double temp;
753  while (s >> temp){
754  tempVec.push_back(temp);
755  }
756  LOCALE_C_OFF
757  return tempVec;
758 }
759 
760 std::vector<int> XMLIO::
761 getAttributeValueVecInt(const xmlAttrPtr Attribute) const {
762 
763  stringstream s(getAttributeValueString(Attribute));
764  std::vector<int> tempVec;
765  int temp;
766  while (s >> temp){
767  tempVec.push_back(temp);
768  }
769  return tempVec;
770 }
771 
772 std::string XMLIO::getAttributeValueString(const xmlNodePtr Node,
773  const std::string& AttributeName) const{
774  std::string ret="";
775  char* pc;
776  pc = (char*) xmlGetProp(Node, BAD_CAST AttributeName.c_str());
777  if (pc!=NULL) {
778  ret=pc;
779  xmlFree(pc);
780  }
781  return ret;
782 }
783 
784 
785 bool XMLIO::getAttributeValueBool(const xmlNodePtr Node,
786  const std::string& AttributeName) const {
787  std::string temp="";
788  char* pc;
789  pc = (char*) xmlGetProp(Node, BAD_CAST AttributeName.c_str());
790  if (pc!=NULL) {
791  temp=pc;
792  xmlFree(pc);
793  }
794  // transfrom to lower case letters for comparison
795  temp = FileHandling::LowerCase(temp);
796  if (temp == "true") {
797  return true;
798  } else if (temp == "false" || temp.empty()) {
799  return false;
800  } else {
801  //BIASERR("Undefined boolean value \"" << temp << "\" found for attribute \""
802  // << AttributeName << "\"! Using \"false\" instead!");
803  return false;
804  }
805 }
806 
807 int XMLIO::getAttributeValueInt(const xmlNodePtr Node,
808  const std::string& AttributeName) const {
809  std::string ret="";
810  char* pc;
811  pc = (char*) xmlGetProp(Node, BAD_CAST AttributeName.c_str());
812  if (pc!=NULL) {
813  ret=pc;
814  xmlFree(pc);
815  }
816  return atoi(ret.c_str());
817 }
818 
819 
820 double XMLIO::getAttributeValueDouble(const xmlNodePtr Node,
821  const std::string& AttributeName) const {
822  std::string ret="";
823  char* pc;
824  pc = (char*) xmlGetProp(Node, BAD_CAST AttributeName.c_str());
825  if (pc!=NULL) {
826  ret=pc;
827  xmlFree(pc);
828  }
829  LOCALE_C_ON
830  double retValue = atof(ret.c_str());
831  LOCALE_C_OFF
832  return retValue;
833 }
834 
835 std::vector<double> XMLIO::
836 getAttributeValueVecDbl(const xmlNodePtr Node,
837  const std::string& AttributeName) const {
838  stringstream s("");
839  char* pc;
840  std::vector<double> tempVec;
841  double temp;
842  pc = (char*) xmlGetProp(Node, BAD_CAST AttributeName.c_str());
843  if (pc!=NULL) {
844  s.str(pc);
845  xmlFree(pc);
846  }
847  while (s >> temp){
848  tempVec.push_back(temp);
849  }
850  return tempVec;
851 }
852 
853 std::vector<int> XMLIO::
854 getAttributeValueVecInt(const xmlNodePtr Node,
855  const std::string& AttributeName) const {
856  stringstream s("");
857  char* pc;
858  std::vector<int> tempVec;
859  int temp;
860  pc = (char*) xmlGetProp(Node, BAD_CAST AttributeName.c_str());
861  if (pc!=NULL) {
862  s.str(pc);
863  xmlFree(pc);
864  }
865  while (s >> temp){
866  tempVec.push_back(temp);
867  }
868  return tempVec;
869 }
870 
871 int XMLIO::SetCompression(int level) {
872  int ret = -1;
873  if (isInitialized_) {
874  // Cleanup function for the XML library.
875  ret = xmlGetDocCompressMode(xmlDoc_);
876  if (level>9) level = 9;
877  if (level<0) level = 0;
878  xmlSetDocCompressMode(xmlDoc_, level);
879  } else {
880  BIASERR("No document yet! Ignored compression level");
881  }
882  return ret;
883 }
884 
885 
886 int XMLIO::WriteToString(string &str)
887 {
888 
889  if (!isInitialized_) {
890  BIASERR("Not initialized: Please call createXML/loadXML first\n");
891  return -1;
892  }
893 
894  xmlChar *xmlbuff;
895  int buffersize;
896  LOCALE_C_ON
897  xmlDocDumpFormatMemory(xmlDoc_, &xmlbuff, &buffersize, 1);
898  LOCALE_C_OFF
899  str.replace(0, buffersize, (char *) xmlbuff);
900  xmlFree(xmlbuff);
901  return buffersize;
902 }
903 
904 
905 xmlNodePtr XMLIO::ReadFromString(const string &str)
906 {
907 
908  if (isInitialized_) {
909  // do NOT use xmlCleanupParser() here when concurrent threads use XMLIO!
910  // see: http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser
911  //xmlCleanupParser();
912 
913  xmlFreeDoc(xmlDoc_);
914  isInitialized_ = false;
915  }
916  LOCALE_C_ON
917  xmlDoc_ = xmlReadMemory(str.c_str(), (int)str.length(), "noname.xml", NULL, 0);
918  LOCALE_C_OFF
919  if (xmlDoc_ == NULL) {
920  BIASERR("Error parsing the xml document, was not well formed (ReadFromString)");
921  return NULL;
922  }
923 
924  RootNode_ = CurrNode_ = xmlDocGetRootElement(xmlDoc_);
925  isInitialized_=true;
926 
927  return RootNode_;
928 
929 }
930 
931 
932 int XMLIO::IsoLatin1ToUtf8(const std::string& isoLatin1, std::string& utf8)
933 {
934  utf8 = "";
935  int res = 0;
936  int inlen = (int)isoLatin1.length();
937  int outlen = 2*inlen;
938  unsigned char *outbuf = new unsigned char[outlen+1];
939  res = isolat1ToUTF8(outbuf, &outlen, BAD_CAST isoLatin1.c_str(), &inlen);
940  if (res >= 0) {
941  for (int i = 0; i < outlen; i++) { utf8 += outbuf[i]; }
942  res = 0;
943  } else {
944  res = -1;
945  }
946  delete[] outbuf;
947  return res;
948 }
949 
950 
951 int XMLIO::Utf8ToIsoLatin1(const std::string& utf8, std::string& isoLatin1)
952 {
953  isoLatin1 = "";
954  int res = 0;
955  int inlen = (int)utf8.length();
956  int outlen = 2*inlen;
957  unsigned char *outbuf = new unsigned char[outlen+1];
958  res = UTF8Toisolat1(outbuf, &outlen, BAD_CAST utf8.c_str(), &inlen);
959  if (res >= 0) {
960  for (int i = 0; i < outlen; i++) { isoLatin1 += outbuf[i]; }
961  res = 0;
962  } else {
963  res = -1;
964  }
965  delete[] outbuf;
966  return res;
967 }
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
std::string getAttributeName(const xmlAttrPtr Attribute) const
Get the name of a given Attribute.
Definition: XMLIO.cpp:690
xmlNodePtr getNextChild()
Get the next child of the parent specified in the last getFirstChild() call, the class remembers the ...
Definition: XMLIO.cpp:466
int write(const std::string &Filename, bool AutoAddCompressionSuffix=true) const
Write the whole tree that was constructed in memory to disk.
Definition: XMLIO.cpp:379
xmlNodePtr read(const std::string &Filename)
Read and parse an XML file from disk, DtD validation is not yet implemented.
Definition: XMLIO.cpp:416
void addComment(const xmlNodePtr Node, const std::string &Comment)
Add comment to a node.
Definition: XMLIO.cpp:309
xmlNodePtr getChild(const xmlNodePtr ParentNode, const std::string &ChildName)
Get a child of a Parent node by specifying the childs name, NULL is returned if the ParentNode has no...
Definition: XMLIO.cpp:489
xmlNodePtr create(const std::string &RootNodeName)
Create the base of a new XML-Tree in memory, already with a one and only root node.
Definition: XMLIO.cpp:88
static int IsoLatin1ToUtf8(const std::string &isoLatin1, std::string &utf8)
Convert character string from UTF-8 format to ISO 8895-1.
Definition: XMLIO.cpp:932
bool getAttributeValueBool(const xmlAttrPtr Attribute) const
Get the value of a given Attribute, with type-cast overloads for different attribute types...
Definition: XMLIO.cpp:700
xmlAttrPtr getFirstAttribute(const xmlNodePtr Node)
Get the first attribute of a given parent, or NULL for no attributes.
Definition: XMLIO.cpp:624
std::vector< double > getAttributeValueVecDbl(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:747
std::string getAttributeValueString(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:716
int getAttributeValueInt(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:728
std::vector< double > getNodeContentVectorDouble(const xmlNodePtr Node) const
Definition: XMLIO.cpp:599
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Definition: XMLIO.cpp:254
double getNodeContentDouble(const xmlNodePtr Node) const
Definition: XMLIO.cpp:613
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
Definition: XMLIO.cpp:131
int WriteToString(std::string &str)
serialize tree to string
Definition: XMLIO.cpp:886
void GetChildren(const xmlNodePtr ParentNode, const std::string &ChildName, std::vector< xmlNodePtr > &childrenWithSameName)
Convenience wrapper: returns a vector of pointors to every (direct) child with the specified name...
Definition: XMLIO.cpp:506
void GetAttributes(const xmlNodePtr Node, std::vector< xmlAttrPtr > &attributes)
accesor functions for recursive usage
Definition: XMLIO.cpp:679
xmlNodePtr ReadFromString(const std::string &str)
construct tree from string
Definition: XMLIO.cpp:905
void setContent(const xmlNodePtr Node, const std::string &Content)
Set content of a node.
Definition: XMLIO.cpp:321
xmlNodePtr getFirstChild(const xmlNodePtr ParentNode)
Get the first child of a given parent, or NULL for no childs.
Definition: XMLIO.cpp:452
static int FilenameExpansion(const std::string &in, std::string &out)
Expand filename, e.g.
xmlAttrPtr getAttributeByName(const xmlNodePtr Node, const std::string &attribute_name)
search for a specific attribute
Definition: XMLIO.cpp:650
xmlAttrPtr getNextAttribute()
Get the next attribute of the parent specified in the last getFirstAttribute() call, the class remembers the last returned attribute.
Definition: XMLIO.cpp:638
xmlNodePtr GetChildWithAttributeValueString(const xmlNodePtr ParentNode, const std::string &ChildName, const std::string &AttributeName, const std::string &AttributeValue)
Finds the &#39;first&#39; child with name ChildName and an attribute named AttributeName that has the string ...
Definition: XMLIO.cpp:521
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:736
void clear()
Definition: XMLIO.cpp:74
std::vector< int > getNodeContentVectorInt(const xmlNodePtr Node) const
Definition: XMLIO.cpp:587
std::string getNodeName(const xmlNodePtr Node) const
Get the name of a given Node.
Definition: XMLIO.cpp:543
int SetCompression(int level=9)
set level of compression, needs zlib
Definition: XMLIO.cpp:871
int getNodeContentInt(const xmlNodePtr Node) const
Definition: XMLIO.cpp:579
std::vector< int > getAttributeValueVecInt(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:761
static std::string LowerCase(const std::string &input)
Return lower case string of input.
std::string getNodeContentString(const xmlNodePtr Node) const
Get the content of a given Node.
Definition: XMLIO.cpp:554
static int Utf8ToIsoLatin1(const std::string &utf8, std::string &isoLatin1)
Convert character string from ISO 8895-1 format to UTF-8.
Definition: XMLIO.cpp:951