Pyrogenesis  trunk
XMLWriter.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_XMLWRITER
19 #define INCLUDED_XMLWRITER
20 
21 /*
22 
23 System for writing simple XML files, with human-readable formatting.
24 
25 Example usage:
26 
27  XML_Start();
28 
29  {
30  XML_Element("Scenario");
31  {
32  XML_Element("Entities");
33  for (...)
34  {
35  XML_Element("Entity");
36 
37  {
38  XML_Element("Template");
39  XML_Text(entity.name);
40  }
41  // Or equivalently:
42  XML_Setting("Template", entity.name);
43 
44  {
45  XML_Element("Position");
46  XML_Attribute("x", entity.x);
47  XML_Attribute("y", entity.y);
48  XML_Attribute("z", entity.z);
49  }
50 
51  {
52  XML_Element("Orientation");
53  XML_Attribute("angle", entity.angle);
54  }
55  }
56  }
57  }
58 
59  Handle h = vfs_open("/test.xml", FILE_WRITE|FILE_NO_AIO);
60  XML_StoreVFS(h);
61 
62 In general, "{ XML_Element(name); ... }" means "<name> ... </name>" -- the
63 scoping braces are important to indicate where an element ends.
64 
65 XML_Attribute/XML_Setting are templated. To support more types, alter the
66 end of XMLWriter.cpp.
67 
68 */
69 
70 // Starts generating a new XML file.
71 #define XML_Start() XMLWriter_File xml_file_
72 
73 // Set pretty printing (newlines, tabs). Defaults to true.
74 #define XML_SetPrettyPrint(enabled) xml_file_.SetPrettyPrint(false)
75 
76 // Add a comment to the XML file: <!-- text -->
77 #define XML_Comment(text) xml_file_.Comment(text)
78 
79 // Start a new element: <name ...>
80 #define XML_Element(name) XMLWriter_Element xml_element_ (xml_file_, name)
81 
82 // Add text to the interior of the current element: <...>text</...>
83 #define XML_Text(text) xml_element_.Text(text, false)
84 
85 // Add CDATA-escaped text to the interior of the current element: <...><![CDATA[text]]></...>
86 #define XML_CDATA(text) xml_element_.Text(text, true)
87 
88 // Add an attribute to the current element: <... name="value" ...>
89 #define XML_Attribute(name, value) xml_element_.Attribute(name, value)
90 
91 // Add a 'setting': <name>value</name>
92 #define XML_Setting(name, value) xml_element_.Setting(name, value)
93 
94 #define XML_WriteXMB(xero) xml_file_.XMB(xero)
95 
96 // Create a VFS file from the XML data.
97 // Returns true on success, false (and logs an error) on failure.
98 #define XML_StoreVFS(vfs, pathname) xml_file_.StoreVFS(vfs, pathname)
99 
100 // Returns the contents of the XML file as a UTF-8 byte stream in a const CStr&
101 // string. (Use CStr::FromUTF8 to get a Unicode string back.)
102 #define XML_GetOutput() xml_file_.GetOutput()
103 
104 
105 #include "ps/CStr.h"
106 #include "lib/file/vfs/vfs.h"
107 
108 class XMBElement;
109 class XMBFile;
110 class XMLWriter_Element;
111 
113 {
114 public:
115  XMLWriter_File();
116 
117  void SetPrettyPrint(bool enabled) { m_PrettyPrint = enabled; }
118 
119  void Comment(const char* text);
120 
121  void XMB(const XMBFile& file);
122 
123  bool StoreVFS(const PIVFS& vfs, const VfsPath& pathname);
124  const CStr& GetOutput();
125 
126 private:
127 
128  friend class XMLWriter_Element;
129 
130  void ElementXMB(const XMBFile& file, XMBElement el);
131 
132  void ElementStart(XMLWriter_Element* element, const char* name);
133  void ElementText(const char* text, bool cdata);
134  template <typename T> void ElementAttribute(const char* name, const T& value, bool newelement);
135  void ElementClose();
136  void ElementEnd(const char* name, int type);
137 
138  CStr Indent();
139 
141 
142  CStr m_Data;
143  int m_Indent;
145 };
146 
148 {
149 public:
150  XMLWriter_Element(XMLWriter_File& file, const char* name);
152 
153  template <typename constCharPtr> void Text(constCharPtr text, bool cdata);
154  template <typename T> void Attribute(const char* name, T value) { m_File->ElementAttribute(name, value, false); }
155  template <typename T> void Setting(const char* name, T value) { m_File->ElementAttribute(name, value, true); }
156  void Close(int type);
157 
158 private:
159 
160  friend class XMLWriter_File;
161 
163  CStr m_Name;
164  int m_Type;
165 };
166 
167 #endif // INCLUDED_XMLWRITER
XMLWriter_File()
Definition: XMLWriter.cpp:84
const CStr & GetOutput()
Definition: XMLWriter.cpp:111
int m_Indent
Definition: XMLWriter.h:143
XMLWriter_File * m_File
Definition: XMLWriter.h:162
shared_ptr< IVFS > PIVFS
Definition: vfs.h:226
void XMB(const XMBFile &file)
Definition: XMLWriter.cpp:117
void ElementStart(XMLWriter_Element *element, const char *name)
Definition: XMLWriter.cpp:146
Definition: XeroXMB.h:160
CStr m_Name
Definition: XMLWriter.h:163
int m_Type
Definition: XMLWriter.h:164
void Setting(const char *name, T value)
Definition: XMLWriter.h:155
CStr m_Data
Definition: XMLWriter.h:142
void ElementAttribute(const char *name, const T &value, bool newelement)
Definition: path.h:77
Definition: XMLWriter.h:112
XMLWriter_Element * m_LastElement
Definition: XMLWriter.h:144
CStr Indent()
Definition: XMLWriter.cpp:141
Definition: XMLWriter.h:147
void Comment(const char *text)
Definition: XMLWriter.cpp:133
#define T(string_literal)
Definition: secure_crt.cpp:76
Definition: XeroXMB.h:115
void SetPrettyPrint(bool enabled)
Definition: XMLWriter.h:117
void ElementEnd(const char *name, int type)
Definition: XMLWriter.cpp:167
void ElementClose()
Definition: XMLWriter.cpp:162
void Attribute(const char *name, T value)
Definition: XMLWriter.h:154
bool m_PrettyPrint
Definition: XMLWriter.h:140
Definition: vfs_util.cpp:39
friend class XMLWriter_Element
Definition: XMLWriter.h:128
void ElementText(const char *text, bool cdata)
Definition: XMLWriter.cpp:197
void ElementXMB(const XMBFile &file, XMBElement el)
Definition: XMLWriter.cpp:122
bool StoreVFS(const PIVFS &vfs, const VfsPath &pathname)
Definition: XMLWriter.cpp:94