-
Notifications
You must be signed in to change notification settings - Fork 1
/
XMLConfig.cpp
172 lines (145 loc) · 5.53 KB
/
XMLConfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include "XMLConfig.h"
using namespace xercesc;
using namespace std;
// The default and only constructor available is implemented below.
XMLConfig::XMLConfig(const string& inputConfigFileName, const string& applicationSettings, const string& rootSectionName)
: m_InputConfigFileName(inputConfigFileName) ,
m_ApplicationSettings(applicationSettings) ,
m_rootSectionName(rootSectionName)
{
try
{
XMLPlatformUtils::Initialize(); // Initialize Xerces infrastructure
}
catch( XMLException& e )
{
char* message = XMLString::transcode( e.getMessage() );
cout << "XML toolkit initialization error: " << message << endl;
XMLString::release( &message );
}
if(m_rootSectionName.empty())
m_rootSectionName = "root";
TAG_root = XMLString::transcode(m_rootSectionName.c_str());
TAG_ApplicationSettings = XMLString::transcode(m_ApplicationSettings.c_str());
m_ConfigFileParser = make_unique<xercesc::XercesDOMParser>();
}
// The readConfigFile Method implements reading from the supplied config file name. It parsers through the XML document and only initializes the map with key / values from the
// sub section of m_ApplicationSettings.
void XMLConfig::readConfigFile()
{
// Test to see if the file is ok.
struct stat fileStatus;
errno = 0;
if(stat(m_InputConfigFileName.c_str(), &fileStatus) == -1) // ==0 ok; ==-1 error
{
if( errno == ENOENT ) // errno declared by include file errno.h
throw ( runtime_error("Path file_name does not exist, or path is an empty string.") );
else if( errno == ENOTDIR )
throw ( runtime_error("A component of the path is not a directory."));
else if( errno == ELOOP )
throw ( runtime_error("Too many symbolic links encountered while traversing the path."));
else if( errno == EACCES )
throw ( runtime_error("Permission denied."));
else if( errno == ENAMETOOLONG )
throw ( runtime_error("File can not be read\n"));
}
// Configure DOM parser.
m_ConfigFileParser->setValidationScheme( XercesDOMParser::Val_Never );
m_ConfigFileParser->setDoNamespaces( false );
m_ConfigFileParser->setDoSchema( false );
m_ConfigFileParser->setLoadExternalDTD( false );
try
{
m_ConfigFileParser->parse(m_InputConfigFileName.c_str());
// no need to free this pointer - owned by the parent parser object
DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
// Get the top-level element: NAme is "root". No attributes for "root"
DOMElement* elementRoot = xmlDoc->getDocumentElement();
if( !elementRoot ) throw(runtime_error( "empty XML document" ));
// Parse XML file for tags of interest: "ApplicationSettings"
// Look one level nested within "root". (child of root)
DOMNodeList* children = elementRoot->getChildNodes();
const XMLSize_t nodeCount = children->getLength();
// For all nodes, children of "root" in the XML tree.
for( XMLSize_t xx = 0; xx < nodeCount; ++xx )
{
DOMNode* currentNode = children->item(xx);
if(XMLString::equals(currentNode->getNodeName(), TAG_ApplicationSettings))
{
if( currentNode->getNodeType() && // true is not NULL
currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) // is element
{
// First traverse and print all values/names pairs.
DOMNamedNodeMap* map = currentNode->getAttributes();
int i, len = map ? map->getLength() : 0;
for (i=0; i<len; ++i)
{
DOMNode *attr = map->item(i);
const XMLCh* xmlch_nodeName = attr->getNodeName();
const XMLCh* xmlch_nodeValue = attr->getNodeValue();
// The pointer returned by transcode needs to be saved and released explicitly. Else leaks memory.
char* nodeNamePtr = XMLString::transcode(xmlch_nodeName);
char* nodeValPtr = XMLString::transcode(xmlch_nodeValue);
m_InternalMap[nodeNamePtr] = nodeValPtr;
// Release the pointers.
XMLString::release(&nodeNamePtr);
XMLString::release(&nodeValPtr);
}
}
}
}
}
catch( xercesc::XMLException& e )
{
char* message = xercesc::XMLString::transcode( e.getMessage() );
ostringstream errBuf;
errBuf << "Error parsing file: " << message << flush;
XMLString::release( &message );
}
}
// Indicates if the map is empty and indicates validity if the map has elements.
bool XMLConfig::isConfigValid()
{
return(m_InternalMap.size()>0);
}
// Return The map to the end user if required.
map<string, string> XMLConfig::getConfigMap()
{
return(m_InternalMap);
}
// Destructor. Most important release all the resources here.
XMLConfig::~XMLConfig()
{
// This is critical. Leaving to expire at the end causes segmentation fault. Seems all cleanup should be done before shutting down the Parser via XMLPlatformUtils::Terminate().
m_ConfigFileParser.reset();
// Free memory
try
{
XMLString::release( &TAG_root );
XMLString::release( &TAG_ApplicationSettings );
}
catch( ... )
{
cerr << "Unknown exception encountered in TagNamesdtor" << endl;
}
// Terminate Xerces
try
{
XMLPlatformUtils::Terminate(); // Terminate after release of memory
}
catch( xercesc::XMLException& e )
{
char* message = xercesc::XMLString::transcode( e.getMessage() );
cerr << "XML ttolkit teardown error: " << message << endl;
XMLString::release( &message );
}
}