Cantera  2.0
XML_Writer.h
1 #ifndef CT_XML_WRITER
2 #define CT_XML_WRITER
3 
4 // Note: this class is only used by TransportFactory, and is likely to
5 // go away a a future date.
6 
7 namespace Cantera
8 {
9 
10 ////////////////////// XML_Writer //////////////////////////
11 
12 class XML_Writer
13 {
14 public:
15  XML_Writer(std::ostream& output) :
16  m_s(output), _indent(" "), _level(0) {}
17  virtual ~XML_Writer() {}
18  std::ostream& m_s;
19 
20  std::string _indent;
21  int _level;
22 
23  std::ostream& output() {
24  return m_s;
25  }
26 
27  inline std::string XML_filter(std::string name) {
28  int ns = static_cast<int>(name.size());
29  std::string nm(name);
30  for (int m = 0; m < ns; m++)
31  if (name[m] == ' '
32  || name[m] == '('
33  || name[m] == ')') {
34  nm[m] = '_';
35  }
36  return nm;
37  }
38 
39  /**
40  * XML_comment()
41  *
42  * Add a comment element to the current XML output file
43  * Comment elements start with <!-- and end with -->
44  * Comments are indented according to the current lvl,
45  * _level
46  *
47  * input
48  * ---------
49  * s : Output stream containing the XML file
50  * comment : Reference to a string containing the comment
51  */
52  inline void XML_comment(std::ostream& s, const std::string& comment) {
53  for (int n = 0; n < _level; n++) {
54  s << _indent;
55  }
56  s << "<!--" << comment << "-->" << std::endl;
57  }
58 
59  inline void XML_open(std::ostream& s, const std::string& tag, const std::string p = "") {
60  for (int n = 0; n < _level; n++) {
61  s << _indent;
62  }
63  _level++;
64  s << "<" << XML_filter(tag) << p << ">" << std::endl;
65  }
66 
67  inline void XML_close(std::ostream& s, const std::string& tag) {
68  _level--;
69  for (int n = 0; n < _level; n++) {
70  s << _indent;
71  }
72  s << "</" << XML_filter(tag) << ">" << std::endl;
73  }
74 
75  template<class T>
76  void XML_item(std::ostream& s, const std::string& tag, T value) {
77  for (int n = 0; n < _level; n++) {
78  s << _indent;
79  }
80  s << "<" << XML_filter(tag) << ">"
81  << value << "</" << tag << ">" << std::endl;
82  }
83 
84  template<class iter>
85  void XML_writeVector(std::ostream& s, const std::string& indent,
86  const std::string& name, int vsize, iter v) {
87  int ni;
88  for (ni = 0; ni < _level; ni++) {
89  s << _indent;
90  }
91  s << "<" << XML_filter(name) << "> ";
92 
93  int n = vsize;
94  int n5 = n/5;
95  int i, j, k = 0;
96  for (j = 0; j < n5; j++) {
97  for (i = 0; i < 5; i++) {
98  s << v[k] << (k < n - 1 ? ", " : "");
99  k++;
100  }
101  if (j < n5-1) {
102  s << std::endl;
103  for (ni = 0; ni < _level; ni++) {
104  s << _indent;
105  }
106  }
107  }
108  for (i = k; i < n; i++) {
109  s << v[k] << (k < n - 1 ? ", " : "");
110  k++;
111  }
112 
113  s << "</" << XML_filter(name) << ">" << std::endl;
114  }
115 };
116 
117 }
118 
119 #endif