indent_xml.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "indent_xml.hpp"
  2. /// boost headers ///
  3. #include <boost/algorithm/string.hpp>
  4. #include <boost/algorithm/string/split.hpp>
  5. namespace xios
  6. {
  7. /// ////////////////////// Définitions ////////////////////// ///
  8. unsigned int CIndent::Indent = 0;
  9. StdString CIndent::Increm = StdString(" ");
  10. bool CIndent::WithLine = false;
  11. StdOStream & CIndent::NIndent(StdOStream& out)
  12. {
  13. static unsigned int LineNB = 1;
  14. if (CIndent::WithLine) out << LineNB++ << ". ";
  15. for(unsigned int i = 0; i < CIndent::Indent; out << CIndent::Increm , i++){}
  16. return (out);
  17. }
  18. StdOStream & CIndent::IncIndent(StdOStream& out)
  19. { CIndent::Indent++; return (CIndent::NIndent(out)); }
  20. StdOStream & CIndent::DecEndl (StdOStream& out)
  21. { CIndent::Indent--; return (out); }
  22. ///----------------------------------------
  23. StdString CIndentedXml::Indented(const StdString & content)
  24. {
  25. StdOStringStream retvalue;
  26. std::vector<StdString> str;
  27. boost::split(str, content, boost::is_any_of("\n"));
  28. std::vector<StdString>::iterator it = str.begin(), end = str.end();
  29. for (; it != end; it++)
  30. {
  31. StdString & line = *it;
  32. if (line.find("<? ") != StdString::npos ||
  33. line.find(xml::CXMLNode::GetRootName()) != StdString::npos)
  34. retvalue << CIndent::NIndent << line << std::endl;
  35. else if (line.find("</") != StdString::npos)
  36. retvalue << CIndent::NIndent << line << CIndent::DecEndl << std::endl;
  37. else if (line.find(" />") != StdString::npos)
  38. retvalue << CIndent::IncIndent << line << CIndent::DecEndl << std::endl;
  39. else
  40. retvalue << CIndent::IncIndent << line << std::endl;
  41. }
  42. return (retvalue.str());
  43. }
  44. } // namespace xios