nlib
XmlStreamWriter.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
4 #define INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
5 
7 #include "nn/nlib/exi/Types.h"
8 
9 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
10 #undef NLIB_VIS_PUBLIC
11 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
12 #endif
13 
14 NLIB_NAMESPACE_BEGIN
15 class OutputStream;
16 
17 namespace exi {
18 
19 // See:
20 // http://java.sun.com/javase/ja/6/docs/ja/api/javax/xml/stream/XMLStreamWriter.html
21 
26  bool fragment;
28  int version;
30 
31  public:
33  encodeOptions(false),
34  alignment(ALIGNMENT_BIT_PACKED),
35  fragment(false),
36  preserve(),
37  version(0),
38  processor(XML_PROCESSOR_EXI) {}
39 };
40 
42  public:
43  static XmlStreamWriter* Create(OutputStream* stream, const XmlStreamWriterSettings& settings,
44  const ExiAllocatorEx& al) NLIB_NOEXCEPT;
46  const XmlStreamWriterSettings& settings) NLIB_NOEXCEPT {
47  ExiAllocatorEx al;
48  return Create(stream, settings, al);
49  }
50  static XmlStreamWriter* Create(OutputStream* stream) NLIB_NOEXCEPT {
52  return Create(stream, settings);
53  }
54  static XmlStreamWriter* Create(OutputStream* stream, const ExiAllocatorEx& al) NLIB_NOEXCEPT {
56  return Create(stream, settings, al);
57  }
58  virtual ~XmlStreamWriter() NLIB_NOEXCEPT {
59  if (m_IsLocalAl) ExiAllocator::Swap(m_Al);
60  }
61  void Close() NLIB_NOEXCEPT {
62  ExiAllocatorSwapper swapper(m_Al);
63  this->Close_();
64  }
65  void Flush() NLIB_NOEXCEPT {
66  ExiAllocatorSwapper swapper(m_Al);
67  this->Flush_();
68  }
69  const ExiChar* GetPrefix(const ExiChar* uri) NLIB_NOEXCEPT {
70  ExiAllocatorSwapper swapper(m_Al);
71  return this->GetPrefix_(uri ? uri : NLIB_EXI_ESTR);
72  }
73  void WriteAttribute(const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT {
74  this->WriteAttribute(NLIB_EXI_ESTR, NLIB_EXI_ESTR, local_name, value);
75  }
76  void WriteAttribute(const ExiChar* prefix, const ExiChar* namespace_uri,
77  const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT;
78  void WriteCData(const ExiChar* data) NLIB_NOEXCEPT {
79  if (NEXI_IS_ERROR) return;
80  ExiAllocatorSwapper swapper(m_Al);
81  this->WriteCData_(data ? data : NLIB_EXI_ESTR);
82  }
83  void WriteCharacters(const ExiChar* text) NLIB_NOEXCEPT {
84  if (NEXI_IS_ERROR) return;
85  ExiAllocatorSwapper swapper(m_Al);
86  this->WriteCharacters_(text ? text : NLIB_EXI_ESTR);
87  }
88  void WriteComment(const ExiChar* data) NLIB_NOEXCEPT;
89  void WriteProcessingInstruction(const ExiChar* target, const ExiChar* data) NLIB_NOEXCEPT;
90  void WriteNamespace(const ExiChar* prefix, const ExiChar* namespace_uri) NLIB_NOEXCEPT {
91  if (NEXI_IS_ERROR) return;
92  ExiAllocatorSwapper swapper(m_Al);
93  this->WriteNamespace_(prefix ? prefix : NLIB_EXI_ESTR,
94  namespace_uri ? namespace_uri : NLIB_EXI_ESTR);
95  }
96  void WriteDefaultNamespace(const ExiChar* namespace_uri) NLIB_NOEXCEPT {
97  this->WriteNamespace(NLIB_EXI_ESTR, namespace_uri);
98  }
99  // virtual void WriteDtd(const ExiChar* dtd)=0;
100  void WriteEmptyElement(const ExiChar* local_name) NLIB_NOEXCEPT {
101  this->WriteEmptyElement(NLIB_EXI_ESTR, NLIB_EXI_ESTR, local_name);
102  }
103  void WriteEmptyElement(const ExiChar* prefix, const ExiChar* namespace_uri,
104  const ExiChar* local_name) NLIB_NOEXCEPT;
105  void WriteStartDocument() NLIB_NOEXCEPT {
106  if (NEXI_IS_ERROR) return;
107  ExiAllocatorSwapper swapper(m_Al);
108  this->WriteStartDocument_();
109  }
110  // virtual void WriteStartDocument(const ExiChar* version)=0;
111  // virtual void WriteStartDocument(const ExiChar* encoding, const ExiChar* version)=0;
112  void WriteEndDocument() NLIB_NOEXCEPT {
113  if (NEXI_IS_ERROR) return;
114  ExiAllocatorSwapper swapper(m_Al);
115  this->WriteEndDocument_();
116  }
117  void WriteEndElement() NLIB_NOEXCEPT {
118  if (NEXI_IS_ERROR) return;
119  ExiAllocatorSwapper swapper(m_Al);
120  this->WriteEndElement_();
121  }
122  // virtual void WriteEntityRef(const ExiChar* name)=0;
123  void WriteStartElement(const ExiChar* local_name) NLIB_NOEXCEPT {
124  this->WriteStartElement(NLIB_EXI_ESTR, NLIB_EXI_ESTR, local_name);
125  }
126  void WriteStartElement(const ExiChar* prefix, const ExiChar* namespace_uri,
127  const ExiChar* local_name) NLIB_NOEXCEPT;
128  bool IsError() const NLIB_NOEXCEPT { return !m_ErrorStatus->IsOk(); }
129  bool IsOk() const NLIB_NOEXCEPT { return m_ErrorStatus->IsOk(); }
130  ExiErrorStatus::ErrorValue GetError() const NLIB_NOEXCEPT {
131  return m_ErrorStatus->GetErrorValue();
132  }
133  ExiErrorStatus* GetErrorStatus() const NLIB_NOEXCEPT { return m_ErrorStatus; }
135 
136  private:
137  virtual void Close_() NLIB_NOEXCEPT = 0;
138  virtual void Flush_() NLIB_NOEXCEPT = 0;
139  virtual const ExiChar* GetPrefix_(const ExiChar* uri) NLIB_NOEXCEPT = 0;
140  virtual void WriteAttribute_(const ExiChar* prefix, const ExiChar* namespace_uri,
141  const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT = 0;
142  virtual void WriteCData_(const ExiChar* data) NLIB_NOEXCEPT = 0;
143  virtual void WriteCharacters_(const ExiChar* text) NLIB_NOEXCEPT = 0;
144  virtual void WriteComment_(const ExiChar* data) NLIB_NOEXCEPT = 0;
145  virtual void WriteProcessingInstruction_(const ExiChar* target,
146  const ExiChar* data) NLIB_NOEXCEPT = 0;
147  virtual void WriteStartElement_(const ExiChar* prefix, const ExiChar* namespace_uri,
148  const ExiChar* local_name) NLIB_NOEXCEPT = 0;
149  virtual void WriteEmptyElement_(const ExiChar* prefix, const ExiChar* namespace_uri,
150  const ExiChar* local_name) NLIB_NOEXCEPT {
151  this->WriteStartElement_(prefix, namespace_uri, local_name);
152  this->WriteEndElement();
153  }
154  virtual void WriteNamespace_(const ExiChar* prefix,
155  const ExiChar* namespace_uri) NLIB_NOEXCEPT = 0;
156  virtual void WriteStartDocument_() NLIB_NOEXCEPT = 0;
157  virtual void WriteEndDocument_() NLIB_NOEXCEPT = 0;
158  virtual void WriteEndElement_() NLIB_NOEXCEPT = 0;
159 
160  protected:
161  static bool IsNullOrEmptyString(const ExiChar* p) NLIB_NOEXCEPT { return (!p) || (*p == '\0'); }
162  void SetExiErrorStatus(ExiErrorStatus* p) NLIB_NOEXCEPT { m_ErrorStatus = p; }
163  ExiErrorStatus* GetExiErrorStatus() const NLIB_NOEXCEPT { return m_ErrorStatus; }
164  explicit XmlStreamWriter(const ExiAllocatorEx& al) NLIB_NOEXCEPT : m_ErrorStatus(NULL),
165  m_Al(al),
166  m_IsLocalAl(al) {}
167 
168  private:
169  ExiErrorStatus* m_ErrorStatus;
170 
171  protected:
172  ExiAllocatorEx m_Al;
173  bool m_IsLocalAl;
174  NLIB_DISALLOW_COPY_AND_ASSIGN(XmlStreamWriter);
175 };
176 
177 } // namespace exi
178 NLIB_NAMESPACE_END
179 
180 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
181 #undef NLIB_VIS_PUBLIC
182 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
183 #endif
184 
185 #endif // INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
bool encodeCookie
Sets whether to write identification strings at the beginning of the data.
void WriteDefaultNamespace(const ExiChar *namespace_uri) noexcept
Declares the default namespace.
bool IsError() const noexcept
Returns true if an error has occurred.
void WriteStartElement(const ExiChar *local_name) noexcept
Writes an opening tag of an element without a namespace.
Defines constructs such as string-type typedef statements and utility macros.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
void WriteNamespace(const ExiChar *prefix, const ExiChar *namespace_uri) noexcept
Declares the XML namespace.
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:141
XmlProcessor processor
Specifies whether to write binary XML or text-based XML.
bool IsOk() const noexcept
Returns true if an error has not occurred.
Set of options that preserve the binary XML to read or write.
Definition: Types.h:34
int version
Specifies the version number to record in the binary XML.
void WriteEndElement() noexcept
Writes a closing tag.
Use binary XML.
Definition: Types.h:49
ExiErrorStatus::ErrorValue GetError() const noexcept
Gets the error value.
void WriteEmptyElement(const ExiChar *local_name) noexcept
Writes opening tags and closing tags for elements without a namespace.
Sets and stores the error status of the XML parser.
constexpr XmlStreamWriterSettings() noexcept
Configures the default settings.
bool encodeOptions
Sets whether to write information used to automatically recognize the settings.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
void WriteEndDocument() noexcept
Closes all open elements and closes the XML writer.
Alignment
Specifies the alignment of the binary XML to read or write.
Definition: Types.h:32
Allocator that can be set for each instance of XmlStreamReader and XmlStreamWriter.
Definition: ExiAllocator.h:22
ExiErrorStatus * GetErrorStatus() const noexcept
Gets an error status object.
#define NLIB_EXI_ESTR
Defines an empty string. "" or L"".
Definition: Types.h:24
XmlProcessor
Option that specifies which XML processor to use.
Definition: Types.h:49
ErrorValue
Enumerates error statuses.
Alignment alignment
Sets the alignment of the binary XML.
bool fragment
Supports writing binary XML that has multiple root nodes (aka XML fragments). The default is false...
static XmlStreamWriter * Create(OutputStream *stream) noexcept
Creates an XmlStreamWriter instance using default settings for XmlStreamWriterSettings.
Preserve preserve
Sets the fidelity options.
Defines the errors returned by the XML parser.
static XmlStreamWriter * Create(OutputStream *stream, const XmlStreamWriterSettings &settings) noexcept
Creates an instance of XmlStreamWriter.
Structure used as the initialization options for XmlStreamWriter.
void WriteAttribute(const ExiChar *local_name, const ExiChar *value) noexcept
Writes an attribute and value without a namespace.
void WriteCData(const ExiChar *data) noexcept
Writes the text specified as an argument.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
static XmlStreamWriter * Create(OutputStream *stream, const ExiAllocatorEx &al) noexcept
Creates an XmlStreamWriter instance by specifying an objectwise allocator.
void Flush() noexcept
Flushes the XML writer cache.
Abstract class that writes to an XML stream.
void WriteCharacters(const ExiChar *text) noexcept
Writes the text specified as an argument.
void Close() noexcept
Closes an XmlStreamWriter. The original stream is not actually closed; it merely is detached...
const ExiChar * GetPrefix(const ExiChar *uri) noexcept
Gets the prefix corresponding to the namespace URI.
Reads from and writes to a bit-packed EXI stream (default).
Definition: Types.h:32
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17
void WriteStartDocument() noexcept
Declares the start of an XML document and writes data including the XML declaration.
wchar_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:23