nlib
XmlStreamWriter.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
17 #define INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
18 
20 #include "nn/nlib/exi/Types.h"
21 
22 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
23 #undef NLIB_VIS_PUBLIC
24 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
25 #endif
26 
27 NLIB_NAMESPACE_BEGIN
28 class OutputStream;
29 
30 namespace exi {
31 
32 // See:
33 // http://java.sun.com/javase/ja/6/docs/ja/api/javax/xml/stream/XMLStreamWriter.html
34 
39  bool fragment;
41  int version;
43 
44  public:
46  encodeOptions(false),
47  alignment(ALIGNMENT_BIT_PACKED),
48  fragment(false),
49  preserve(),
50  version(0),
51  processor(XML_PROCESSOR_EXI) {}
52 };
53 
55  public:
56  static XmlStreamWriter* Create(OutputStream* stream, const XmlStreamWriterSettings& settings,
57  const ExiAllocatorEx& al) NLIB_NOEXCEPT;
59  const XmlStreamWriterSettings& settings) NLIB_NOEXCEPT {
60  ExiAllocatorEx al;
61  return Create(stream, settings, al);
62  }
63  static XmlStreamWriter* Create(OutputStream* stream) NLIB_NOEXCEPT {
65  return Create(stream, settings);
66  }
67  static XmlStreamWriter* Create(OutputStream* stream, const ExiAllocatorEx& al) NLIB_NOEXCEPT {
69  return Create(stream, settings, al);
70  }
71  virtual ~XmlStreamWriter() NLIB_NOEXCEPT {
72  if (is_local_al_) ExiAllocator::Swap(al_);
73  }
74  void Close() NLIB_NOEXCEPT {
75  ExiAllocatorSwapper swapper(al_);
76  this->Close_();
77  }
78  void Flush() NLIB_NOEXCEPT {
79  ExiAllocatorSwapper swapper(al_);
80  this->Flush_();
81  }
82  const ExiChar* GetPrefix(const ExiChar* uri) NLIB_NOEXCEPT {
83  ExiAllocatorSwapper swapper(al_);
84  return this->GetPrefix_(uri ? uri : NLIB_EXI_ESTR);
85  }
86  void WriteAttribute(const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT {
87  this->WriteAttribute(NLIB_EXI_ESTR, NLIB_EXI_ESTR, local_name, value);
88  }
89  void WriteAttribute(const ExiChar* prefix, const ExiChar* namespace_uri,
90  const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT;
91  void WriteCData(const ExiChar* data) NLIB_NOEXCEPT {
92  if (NEXI_IS_ERROR) return;
93  ExiAllocatorSwapper swapper(al_);
94  this->WriteCData_(data ? data : NLIB_EXI_ESTR);
95  }
96  void WriteCharacters(const ExiChar* text) NLIB_NOEXCEPT {
97  if (NEXI_IS_ERROR) return;
98  ExiAllocatorSwapper swapper(al_);
99  this->WriteCharacters_(text ? text : NLIB_EXI_ESTR);
100  }
101  void WriteComment(const ExiChar* data) NLIB_NOEXCEPT;
102  void WriteProcessingInstruction(const ExiChar* target, const ExiChar* data) NLIB_NOEXCEPT;
103  void WriteNamespace(const ExiChar* prefix, const ExiChar* namespace_uri) NLIB_NOEXCEPT {
104  if (NEXI_IS_ERROR) return;
105  ExiAllocatorSwapper swapper(al_);
106  this->WriteNamespace_(prefix ? prefix : NLIB_EXI_ESTR,
107  namespace_uri ? namespace_uri : NLIB_EXI_ESTR);
108  }
109  void WriteDefaultNamespace(const ExiChar* namespace_uri) NLIB_NOEXCEPT {
110  this->WriteNamespace(NLIB_EXI_ESTR, namespace_uri);
111  }
112  // virtual void WriteDtd(const ExiChar* dtd)=0;
113  void WriteEmptyElement(const ExiChar* local_name) NLIB_NOEXCEPT {
114  this->WriteEmptyElement(NLIB_EXI_ESTR, NLIB_EXI_ESTR, local_name);
115  }
116  void WriteEmptyElement(const ExiChar* prefix, const ExiChar* namespace_uri,
117  const ExiChar* local_name) NLIB_NOEXCEPT;
118  void WriteStartDocument() NLIB_NOEXCEPT {
119  if (NEXI_IS_ERROR) return;
120  ExiAllocatorSwapper swapper(al_);
121  this->WriteStartDocument_();
122  }
123  // virtual void WriteStartDocument(const ExiChar* version)=0;
124  // virtual void WriteStartDocument(const ExiChar* encoding, const ExiChar* version)=0;
125  void WriteEndDocument() NLIB_NOEXCEPT {
126  if (NEXI_IS_ERROR) return;
127  ExiAllocatorSwapper swapper(al_);
128  this->WriteEndDocument_();
129  }
130  void WriteEndElement() NLIB_NOEXCEPT {
131  if (NEXI_IS_ERROR) return;
132  ExiAllocatorSwapper swapper(al_);
133  this->WriteEndElement_();
134  }
135  // virtual void WriteEntityRef(const ExiChar* name)=0;
136  void WriteStartElement(const ExiChar* local_name) NLIB_NOEXCEPT {
137  this->WriteStartElement(NLIB_EXI_ESTR, NLIB_EXI_ESTR, local_name);
138  }
139  void WriteStartElement(const ExiChar* prefix, const ExiChar* namespace_uri,
140  const ExiChar* local_name) NLIB_NOEXCEPT;
141  ExiErrorStatus::ErrorValue GetError() const NLIB_NOEXCEPT {
142  return error_status_->GetError();
143  }
144  ExiErrorStatus* GetErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
145  NLIB_SAFE_BOOL(XmlStreamWriter, !!*error_status_)
146 
147  private:
148  virtual void Close_() NLIB_NOEXCEPT = 0;
149  virtual void Flush_() NLIB_NOEXCEPT = 0;
150  virtual const ExiChar* GetPrefix_(const ExiChar* uri) NLIB_NOEXCEPT = 0;
151  virtual void WriteAttribute_(const ExiChar* prefix, const ExiChar* namespace_uri,
152  const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT = 0;
153  virtual void WriteCData_(const ExiChar* data) NLIB_NOEXCEPT = 0;
154  virtual void WriteCharacters_(const ExiChar* text) NLIB_NOEXCEPT = 0;
155  virtual void WriteComment_(const ExiChar* data) NLIB_NOEXCEPT = 0;
156  virtual void WriteProcessingInstruction_(const ExiChar* target,
157  const ExiChar* data) NLIB_NOEXCEPT = 0;
158  virtual void WriteStartElement_(const ExiChar* prefix, const ExiChar* namespace_uri,
159  const ExiChar* local_name) NLIB_NOEXCEPT = 0;
160  virtual void WriteEmptyElement_(const ExiChar* prefix, const ExiChar* namespace_uri,
161  const ExiChar* local_name) NLIB_NOEXCEPT {
162  this->WriteStartElement_(prefix, namespace_uri, local_name);
163  this->WriteEndElement();
164  }
165  virtual void WriteNamespace_(const ExiChar* prefix,
166  const ExiChar* namespace_uri) NLIB_NOEXCEPT = 0;
167  virtual void WriteStartDocument_() NLIB_NOEXCEPT = 0;
168  virtual void WriteEndDocument_() NLIB_NOEXCEPT = 0;
169  virtual void WriteEndElement_() NLIB_NOEXCEPT = 0;
170 
171  protected:
172  static bool IsNullOrEmptyString(const ExiChar* p) NLIB_NOEXCEPT { return (!p) || (*p == '\0'); }
173  void SetExiErrorStatus(ExiErrorStatus* p) NLIB_NOEXCEPT { error_status_ = p; }
174  ExiErrorStatus* GetExiErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
175  explicit XmlStreamWriter(const ExiAllocatorEx& al) NLIB_NOEXCEPT : error_status_(nullptr),
176  al_(al),
177  is_local_al_(al) {}
178 
179  private:
180  ExiErrorStatus* error_status_;
181 
182  protected:
183  ExiAllocatorEx al_;
184  bool is_local_al_;
186 };
187 
188 } // namespace exi
189 NLIB_NAMESPACE_END
190 
191 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
192 #undef NLIB_VIS_PUBLIC
193 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
194 #endif
195 
196 #endif // INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
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.
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:179
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:194
XmlProcessor processor
Specifies whether to write binary XML or text-based XML.
ExiErrorStatus::ErrorValue GetError() const noexcept
Gets the error value.
Set of options that preserve the binary XML to read or write.
Definition: Types.h:47
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:62
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
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.
ExiErrorStatus * GetErrorStatus() const noexcept
Gets an error status object.
constexpr XmlStreamWriterSettings() noexcept
Configures the default settings.
bool encodeOptions
Sets whether to write information used to automatically recognize the settings.
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:45
Allocator that can be set for each instance of XmlStreamReader and XmlStreamWriter.
Definition: ExiAllocator.h:35
#define NLIB_EXI_ESTR
Defines an empty string. "" or L"".
Definition: Types.h:41
XmlProcessor
Option that specifies which XML processor to use.
Definition: Types.h:62
ErrorValue
Enumerates error statuses.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
Alignment alignment
Sets the alignment of the binary XML.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
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.
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:45
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:30
void WriteStartDocument() noexcept
Declares the start of an XML document and writes data including the XML declaration.
nlib_utf8_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:40