Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLDOM Namespace Reference

Namespaces

 ExpatHandlers
 

Classes

class  VSFileXMLSerializer
 
struct  XMLParserContext
 
class  XMLElement
 
class  XMLDocument
 
class  XMLSerializer
 
class  XMLProcessor
 

Functions

bool _exportXML (std::ostream &stream, XMLElement *elem)
 
bool _exportXML (std::ostream &stream, XMLDocument *doc)
 

Function Documentation

bool XMLDOM::_exportXML ( std::ostream &  stream,
XMLElement elem 
)

Definition at line 517 of file XMLDocument.cpp.

References XMLDOM::XMLElement::attributesBegin(), XMLDOM::XMLElement::attributesEnd(), XMLDOM::XMLElement::contents(), XMLDOM::XMLElement::getChild(), i, XMLDOM::XMLElement::numChildren(), XMLDOM::XMLElement::tagName(), XMLDOM::XMLElement::type(), XMLDOM::XMLElement::XET_CDATA, XMLDOM::XMLElement::XET_COMMENT, XMLDOM::XMLElement::XET_ROOT, and XMLDOM::XMLElement::XET_TAG.

Referenced by _exportXML(), and XMLDOM::XMLSerializer::exportXML().

518 {
519  if (!elem)
520  return false;
521  XMLElement::attribute_iterator ait;
522  unsigned int i;
523  //Prolog
524  switch ( elem->type() )
525  {
526  case XMLElement::XET_TAG:
527  stream<<"<"<<elem->tagName().c_str();
528  for (ait = elem->attributesBegin(); ait != elem->attributesEnd(); ++ait)
529  if (ait->second.find( "\"" ) != std::string::npos)
530  stream<<" "<<ait->first.c_str()<<"=\'"<<ait->second.c_str()<<"\'";
531 
532  else
533  stream<<" "<<ait->first.c_str()<<"=\""<<ait->second.c_str()<<"\"";
534  if (elem->numChildren() > 0)
535  stream<<">";
536 
537  else
538  stream<<"/>";
539  break;
540  case XMLElement::XET_CDATA:
541  //To-do: Find out if it needs to be enveloped within '<![CDATA['/']]>' constructs.
542  stream<<elem->contents().c_str();
543  return !stream.fail(); //No children
544 
545  case XMLElement::XET_COMMENT:
546  stream<<"<!--"<<elem->contents().c_str()<<"-->";
547  return !stream.fail(); //No children
548 
549  case XMLElement::XET_ROOT:
550  //WTF?
551  return true;
552  }
553  if ( stream.fail() )
554  return false;
555  //Children
556  for (i = 0; i < elem->numChildren(); ++i)
557  if ( !_exportXML( stream, elem->getChild( i ) ) )
558  return false;
559  if ( stream.fail() )
560  return false;
561  //Epilog
562  switch ( elem->type() )
563  {
564  case XMLElement::XET_TAG:
565  if (elem->numChildren() > 0)
566  stream<<"</"<<elem->tagName().c_str()<<">";
567  break;
568  case XMLElement::XET_COMMENT:
569  case XMLElement::XET_CDATA:
570  case XMLElement::XET_ROOT:
571  //WTF?
572  return true;
573  }
574  if ( stream.fail() )
575  return false;
576  return true;
577 }
bool XMLDOM::_exportXML ( std::ostream &  stream,
XMLDocument *  doc 
)

Definition at line 579 of file XMLDocument.cpp.

References _exportXML(), XMLDOM::XMLDocument::docType, XMLDOM::XMLElement::getChild(), i, XMLDOM::XMLElement::numChildren(), XMLDOM::XMLDocument::pubId, XMLDOM::XMLDocument::root, and XMLDOM::XMLDocument::sysId.

580 {
581  if (!doc)
582  return false;
583  //Output xml && <!DOCTYPE...> tags
584  if ( doc->sysId.empty() )
585  stream<<"<?xml version=\"1.0\"?>\n";
586 
587  else
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()<<"\"";
593 
594  else
595  stream<<" SYSTEM";
596  if (doc->sysId.find( '\"' ) != std::string::npos)
597  stream<<" \'"<<doc->sysId.c_str()<<"\'";
598 
599  else
600  stream<<" \""<<doc->sysId.c_str()<<"\"";
601  stream<<">\n";
602  }
603  //Did we fail?
604  if ( stream.fail() )
605  return false;
606  //Output the rest of the document
607  //NOTE: Although XML 1.1 requires that 'root' may only have one child,
608  //we cant't make that assumption because we count as elements comments
609  //and other productions referred to as 'misc' by the standard.
610  for (unsigned int i = 0; i < doc->root.numChildren(); ++i)
611  if ( !_exportXML( stream, doc->root.getChild( i ) ) )
612  return false;
613  return true;
614 }