Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
XMLBase.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 #include <Base/Common/XMLBase.hh>
22 
23 using namespace std;
24 using namespace BIAS;
25 
26 xmlNodePtr XMLBase::XMLAdd(const xmlNodePtr Node, XMLIO& XMLObject) const {
27  xmlNodePtr theNode;
28  string TopLevelName;
29  double Version = 0.0;
30  XMLGetClassName(TopLevelName, Version);
31  theNode = XMLObject.addChildNode(Node, TopLevelName);
32  if (Version > 0.0) {
33  XMLObject.addAttribute(theNode, "Version", Version);
34  }
35  XMLOut(theNode, XMLObject);
36  return theNode;
37 }
38 
39 
40 int XMLBase::XMLWrite(const std::string& Filename,
41  int CompressionLevel,
42  bool AutoAddCompressionSuffix,
43  string encoding) const
44 {
45  XMLIO myXML;
46  myXML.SetEncoding(encoding);
47  string TopLevelName;
48  double Version;
49  XMLGetClassName(TopLevelName, Version);
50  xmlNodePtr rootNode;
51  rootNode = myXML.create(TopLevelName);
52  myXML.addAttribute(rootNode, "Version", Version);
53  XMLOut(rootNode, myXML);
54  myXML.SetCompression(CompressionLevel);
55  int res = myXML.write(Filename, AutoAddCompressionSuffix);
56  myXML.clear();
57  return res;
58 }
59 
60 
61 int XMLBase::XMLWriteToString(string &str,string encoding) const
62 {
63  XMLIO myXML;
64  myXML.SetEncoding(encoding);
65  string TopLevelName;
66  double Version;
67  XMLGetClassName(TopLevelName, Version);
68  xmlNodePtr rootNode;
69  rootNode = myXML.create(TopLevelName);
70  myXML.addAttribute(rootNode, "Version", Version);
71  XMLOut(rootNode, myXML);
72  myXML.WriteToString(str);
73  myXML.clear();
74  return 0;
75 }
76 
77 
78 int XMLBase::XMLRead(const std::string& Filename)
79 {
80  int thereturn = 0;
81  XMLIO myXML;
82  xmlNodePtr rootNode;
83  string TopLevelName;
84  double Version;
85  XMLGetClassName(TopLevelName, Version);
86  rootNode = myXML.read(Filename);
87  if (rootNode == NULL) return -3;
88  string rootNodeName = myXML.getNodeName(rootNode);
89  if (myXML.getNodeName(rootNode) != TopLevelName) {
90  BIASERR("Wrong XML file type (found " << TopLevelName << ", expected "
91  << myXML.getNodeName(rootNode) << ")!");
92  //BIASBREAK; // no abort because we will try to read projection from rig
93  thereturn = -1;
94  } else if ((myXML.getAttributeValueDouble(rootNode, "Version") != Version) &&
95  Version > 0.0) {
96  BIASERR("Wrong XML file version (found "
97  << myXML.getAttributeValueDouble(rootNode, "Version")
98  << "), expected " << Version << ")!");
99  //BIASBREAK;
100  thereturn = -2;
101  }
102  else
103  {
104  thereturn = XMLIn(rootNode, myXML);
105  }
106  myXML.clear();
107  return thereturn;
108 }
109 
110 
111 int XMLBase::XMLReadFromString(const std::string& str)
112 {
113  int thereturn = 0;
114  XMLIO myXML;
115  xmlNodePtr rootNode;
116  string TopLevelName;
117  double Version;
118  XMLGetClassName(TopLevelName, Version);
119  rootNode = myXML.ReadFromString(str);
120  if (myXML.getNodeName(rootNode) != TopLevelName) {
121  BIASERR("Wrong XML file type (found " << TopLevelName << ", expected "
122  << myXML.getNodeName(rootNode) << ")!");
123  //BIASBREAK;
124  thereturn = -1;
125  } else if (myXML.getAttributeValueDouble(rootNode, "Version") != Version &&
126  Version>0.0) {
127  BIASERR("Wrong XML file version (found "
128  << myXML.getAttributeValueDouble(rootNode, "Version")
129  << "), expected " << Version << ")!");
130  //BIASBREAK;
131  thereturn = -2;
132  } else thereturn = XMLIn(rootNode, myXML);
133  myXML.clear();
134  return thereturn;
135 }
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
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
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
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
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
void SetEncoding(const std::string &encoding)
Definition: XMLIO.hh:308
int WriteToString(std::string &str)
serialize tree to string
Definition: XMLIO.cpp:886
xmlNodePtr ReadFromString(const std::string &str)
construct tree from string
Definition: XMLIO.cpp:905
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
Definition: XMLIO.cpp:736
void clear()
Definition: XMLIO.cpp:74
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