16 #undef DEBUG_XML_PARSE
27 while ( *data != 0 ) {
29 case '<': _stream <<
"<";
break;
30 case '>': _stream <<
">";
break;
31 case '&': _stream <<
"&";
break;
32 case '"': _stream <<
""";
break;
33 default: _stream << *data;
break;
42 _stream << endl <<
"<" << name;
44 Configuration::attribute_list::const_iterator nvi = nvlist.begin();
45 if ( nvi != nvlist.end() ) {
47 while ( nvi != nvlist.end() ) {
48 _stream <<
" " << nvi->first <<
"=\"";
49 escapeXml( nvi->second.c_str(), _stream );
65 _stream <<
"</" << name <<
">";
74 string value = config->getValue();
75 bool empty = subsects.empty() && value.empty();
77 writeXmlStartTag( config->getName(), config->getAllAttributes(), empty, _stream );
82 if ( ! value.empty() )
85 for ( Configuration::subsection_list::const_iterator subsecti = subsects.begin();
86 subsecti != subsects.end();
88 writeXml( *subsecti, _stream );
97 ofstream file( filename );
99 writeXml( config, file );
121 void addChars(
char const * chars,
int len );
127 expatState* makeChild(
string name,
const char **attribs );
129 void setName(
string name );
130 void setAttributes(
const char **attribs );
147 #if defined(DEBUG_XML_PARSE)
148 cout <<
"expatState::expatState( ): " <<
this << endl;
157 #if defined(DEBUG_XML_PARSE)
158 cout <<
"expatState::expatState( expatState* ): " <<
this << endl;
167 #if defined(DEBUG_XML_PARSE)
168 cout <<
"expatState::~expatState(): " <<
this << endl;
175 #if defined(DEBUG_XML_PARSE)
176 cout <<
"expatState::makeChild(): " <<
this << endl;
183 this->config->addSubsection( child->
config );
194 #if defined(DEBUG_XML_PARSE)
195 cout <<
"expatState::setName(): " <<
this << endl;
196 cout <<
" name: " << name << endl;
198 config->setName( name );
202 #if defined(DEBUG_XML_PARSE)
203 cout <<
"expatState::setAttributes(): " <<
this << endl;
204 cout <<
" attribs: " << attribs << endl;
207 const char **name = attribs++;
208 const char **value = attribs;
210 for( ; *name != NULL ; ) {
211 #if defined(DEBUG_XML_PARSE)
212 cout <<
" name: " << *name << endl;
213 cout <<
" value: " << *value << endl;
215 config->addNameValue( *name, *value );
223 #if defined(DEBUG_XML_PARSE)
224 cout <<
"expatState::finalize(): " <<
this << endl;
241 #if defined(DEBUG_XML_PARSE)
242 cout <<
"expatState::addChars(): " <<
this << endl;
243 cout <<
" prev = " <<
tmpChars <<endl;
247 #if defined(DEBUG_XML_PARSE)
248 cout <<
" new = " <<
tmpChars <<endl;
262 #if defined(DEBUG_XML_PARSE)
263 cout <<
"firstStartElementHandler: " << name << endl;
265 XML_Parser parser = (XML_Parser) state;
276 #if defined(DEBUG_XML_PARSE)
277 cout <<
"startElementHandler: " << name << endl;
279 XML_Parser parser = (XML_Parser) state;
286 XML_SetUserData( parser, (
void *)childState );
291 #if defined(DEBUG_XML_PARSE)
292 cout <<
"endElementHandler: " << name << endl;
294 XML_Parser parser = (XML_Parser) state;
298 XML_SetUserData( parser, (
void*)parent );
302 #if defined(DEBUG_XML_PARSE)
303 cout <<
"charHandler: " << endl;
304 cout <<
" chars: " << string( name, len ) << endl;
306 XML_Parser parser = (XML_Parser) state;
320 ifstream file( filename, ios::in );
334 operator XML_Parser() {
350 XML_SetUserData( parser, state );
353 auto_ptr<expatState> apstate( state );
355 XML_UseParserAsHandlerArg( parser );
357 XML_SetCharacterDataHandler( parser,
charHandler );
359 const int BUFF_SIZE = 1024;
365 char *buf = (
char *)XML_GetBuffer( parser, BUFF_SIZE );
368 std::string msg =
"Unable to allocate buffer during XML read";
375 file.read( buf, BUFF_SIZE);
378 bytes_read = file.gcount();
386 if ( !XML_ParseBuffer( parser, bytes_read, bytes_read == 0 ) ) {
388 XML_Error x = XML_GetErrorCode( parser );
389 stringstream errormsg( ios::out );
390 errormsg << XML_ErrorString(x)
392 << XML_GetCurrentLineNumber( parser )
394 << XML_GetCurrentColumnNumber( parser )
402 if ( bytes_read == 0 ) {
static ConfigurationPtr readXml(char const *filename)
void firstStartElementHandler(void *state, const XML_Char *name, const XML_Char **atts)
std::vector< ConfigurationPtr > subsection_list
void setName(string name)
void charHandler(void *state, const XML_Char *name, int len)
ConfigurationPtr getConfig()
static Log * instance()
Returns the instance pointer, creating the object on the first call.
static void writeXmlStartTag(const string &name, Configuration::attribute_list nvlist, bool empty, ostream &_stream)
expatState * makeChild(string name, const char **attribs)
static void escapeXml(const char *data, ostream &_stream)
static void writeXmlEndTag(const string &name, ostream &_stream)
void error(const char *format,...)
'Error' level.
void setAttributes(const char **attribs)
void endElementHandler(void *, const XML_Char *)
static void writeXml(const ConstConfigurationPtr &config, char const *fileaname)
void startElementHandler(void *state, const XML_Char *name, const XML_Char **atts)
std::vector< attribute > attribute_list
expatState * finalize(string name)
void addChars(char const *chars, int len)
ExpatAutoPtr(XML_Parser p)
void debug(const char *format,...)
'Debug' level.