8 #define PARSING_BUFFER_SIZE 4096
25 , mIdAttribute( mAttributes.end() )
26 , mNameAttribute( mAttributes.end() )
35 , mIdAttribute( mAttributes.end() )
36 , mNameAttribute( mAttributes.end() )
41 , mContents( (type == XET_CDATA || type == XET_COMMENT) ? data : std::string() ),
45 mIdAttribute( mAttributes.end() ),
46 mNameAttribute( mAttributes.end() )
55 , mIdAttribute( mAttributes.end() )
56 , mNameAttribute( mAttributes.end() )
58 for (; (nAttr >= 2 && attrValuePairList[0] && attrValuePairList[1]); attrValuePairList += 2)
59 setAttribute( attrValuePairList[0], attrValuePairList[1] );
68 , mIdAttribute( mAttributes.end() )
69 , mNameAttribute( mAttributes.end() )
71 for (; (attrValuePairList[0] && attrValuePairList[1]); attrValuePairList += 2)
72 setAttribute( attrValuePairList[0], attrValuePairList[1] );
81 , mIdAttribute( mAttributes.end() )
82 , mNameAttribute( mAttributes.end() )
84 for (std::vector< std::string >::size_type
i = 0;
i+1 < attrValuePairList.size();
i += 2)
88 XMLElement::XMLElement(
const std::string &tagName,
const std::map< std::string, std::string > &attrValuePairList ) :
91 , mAttributes( attrValuePairList )
94 , mIdAttribute( mAttributes.find(
"id" ) )
95 , mNameAttribute( mAttributes.find(
"name" ) )
115 mIdAttribute = mNameAttribute = mAttributes.end();
120 static std::string empty;
144 mDocument->
dirty =
true;
150 ElementMap::const_iterator cit = mById.find(
id );
151 if ( cit == mById.end() )
160 ElementMap::iterator cit = mById.find(
id );
161 if ( cit == mById.end() )
170 ElementMap::const_iterator cit = mByName.find( name );
171 if ( cit == mByName.end() )
180 ElementMap::iterator cit = mByName.find( name );
181 if ( cit == mByName.end() )
192 mDocument->
dirty =
true;
193 mChildren.push_back( newElem );
196 return mChildren.size()-1;
201 if (idx < mChildren.size() && mChildren[idx]) {
204 mDocument->
dirty =
true;
205 delete mChildren[idx];
206 mChildren.erase( mChildren.begin()+idx );
212 removeChild( std::find( mChildren.begin(), mChildren.end(), which )-mChildren.begin() );
217 mAttributes.erase( name );
222 static std::string _id(
"id" );
223 static std::string _name(
"name" );
226 mDocument->
dirty =
true;
227 std::pair< attribute_iterator, bool >rv =
228 mAttributes.insert( std::pair< std::string, std::string > ( name, value ) );
231 mIdAttribute = rv.first;
233 else if (name == _name)
234 mNameAttribute = rv.first;
246 mDocument->
dirty =
true;
268 mDocument = document;
270 (*cit)->setDocument( document );
275 if ( newType !=
type() ) {
292 void XMLElement::JoinMaps( ElementMap &dst,
const ElementMap &src )
294 ElementMap::iterator dit = dst.begin();
295 ElementMap::const_iterator sit = src.begin();
296 while ( sit != src.end() ) {
297 dit = dst.insert( dit, *sit );
307 (*it)->rebuildNamedBindings( deepScan );
309 JoinMaps( mById, (*it)->mById );
310 JoinMaps( mByName, (*it)->mByName );
313 if ( iit != (*it)->attributesEnd() )
314 mById.insert( std::pair< std::string, XMLElement* > ( iit->second, *it ) );
316 if ( nit != (*it)->attributesEnd() )
317 mByName.insert( std::pair< std::string, XMLElement* > ( nit->second, *it ) );
323 std::string::size_type sep =
id.find(
'/' );
324 if (sep != std::string::npos) {
334 std::string::size_type sep =
id.find(
'/' );
335 if (sep != std::string::npos) {
345 std::string::size_type sep = name.find(
'/' );
346 if (sep != std::string::npos) {
356 std::string::size_type sep = name.find(
'/' );
357 if (sep != std::string::npos) {
385 namespace ExpatHandlers
396 const XML_Char *doctypeName,
397 const XML_Char *sysid,
398 const XML_Char *pubid,
399 int has_internal_subset )
411 static void StartElement(
void *userData,
const XML_Char *name,
const XML_Char **atts )
421 (
const char*
const*) atts
427 static void EndElement(
void *userData,
const XML_Char *name )
436 static void CData(
void *userData,
const XML_Char *s,
int len )
448 static void PI(
void *userData,
const XML_Char *target,
const XML_Char *data )
452 XMLParserContext::ProcessorMap::const_iterator it = internals->
processors.find( std::string( target ) );
458 std::string( data ) );
462 static void Comment(
void *userData,
const XML_Char *data )
483 options( OPT_DEFAULT )
497 return XML_Parse( ctxt->
parser, (
const XML_Char*) buf, len,
false ) != 0;
502 std::ifstream fi( path.c_str() );
507 while ( bok && !fi.fail() && !fi.eof() ) {
509 fi.read( buffer,
sizeof (buffer) );
510 bok =
parse( buffer, fi.gcount() );
524 switch ( elem->
type() )
527 stream<<
"<"<<elem->
tagName().c_str();
529 if (ait->second.find(
"\"" ) != std::string::npos)
530 stream<<
" "<<ait->first.c_str()<<
"=\'"<<ait->second.c_str()<<
"\'";
533 stream<<
" "<<ait->first.c_str()<<
"=\""<<ait->second.c_str()<<
"\"";
543 return !stream.fail();
546 stream<<
"<!--"<<elem->
contents().c_str()<<
"-->";
547 return !stream.fail();
562 switch ( elem->
type() )
566 stream<<
"</"<<elem->
tagName().c_str()<<
">";
584 if ( doc->
sysId.empty() )
585 stream<<
"<?xml version=\"1.0\"?>\n";
588 stream<<
"<?xml version=\"1.1\"?>\n";
589 if ( !doc->
sysId.empty() ) {
590 stream<<
"<!DOCTYPE "<<doc->
docType.c_str();
591 if ( !doc->
pubId.empty() )
592 stream<<
" PUBLIC \""<<doc->
pubId.c_str()<<
"\"";
596 if (doc->
sysId.find(
'\"' ) != std::string::npos)
597 stream<<
" \'"<<doc->
sysId.c_str()<<
"\'";
600 stream<<
" \""<<doc->
sysId.c_str()<<
"\"";
618 return !stream.fail()
624 std::ofstream of( filename.c_str() );
633 ctxt->
processors.insert( std::pair< std::string, XMLProcessor* > ( proc->
getTarget(), proc ) );
640 assert( !(!doc && elem) );
649 ctxt->
parser = XML_ParserCreate( (
const XML_Char*) encoding );
650 XML_SetUserData( ctxt->
parser, ctxt );
665 XML_ParserFree( ctxt->
parser );