nlib
XmlStreamWriter.h
[詳解]
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 (is_local_al_) ExiAllocator::Swap(al_);
60  }
61  void Close() NLIB_NOEXCEPT {
62  ExiAllocatorSwapper swapper(al_);
63  this->Close_();
64  }
65  void Flush() NLIB_NOEXCEPT {
66  ExiAllocatorSwapper swapper(al_);
67  this->Flush_();
68  }
69  const ExiChar* GetPrefix(const ExiChar* uri) NLIB_NOEXCEPT {
70  ExiAllocatorSwapper swapper(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(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(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(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(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(al_);
115  this->WriteEndDocument_();
116  }
117  void WriteEndElement() NLIB_NOEXCEPT {
118  if (NEXI_IS_ERROR) return;
119  ExiAllocatorSwapper swapper(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  ExiErrorStatus::ErrorValue GetError() const NLIB_NOEXCEPT {
129  return error_status_->GetError();
130  }
131  ExiErrorStatus* GetErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
132  NLIB_SAFE_BOOL(XmlStreamWriter, !!*error_status_)
133 
134  private:
135  virtual void Close_() NLIB_NOEXCEPT = 0;
136  virtual void Flush_() NLIB_NOEXCEPT = 0;
137  virtual const ExiChar* GetPrefix_(const ExiChar* uri) NLIB_NOEXCEPT = 0;
138  virtual void WriteAttribute_(const ExiChar* prefix, const ExiChar* namespace_uri,
139  const ExiChar* local_name, const ExiChar* value) NLIB_NOEXCEPT = 0;
140  virtual void WriteCData_(const ExiChar* data) NLIB_NOEXCEPT = 0;
141  virtual void WriteCharacters_(const ExiChar* text) NLIB_NOEXCEPT = 0;
142  virtual void WriteComment_(const ExiChar* data) NLIB_NOEXCEPT = 0;
143  virtual void WriteProcessingInstruction_(const ExiChar* target,
144  const ExiChar* data) NLIB_NOEXCEPT = 0;
145  virtual void WriteStartElement_(const ExiChar* prefix, const ExiChar* namespace_uri,
146  const ExiChar* local_name) NLIB_NOEXCEPT = 0;
147  virtual void WriteEmptyElement_(const ExiChar* prefix, const ExiChar* namespace_uri,
148  const ExiChar* local_name) NLIB_NOEXCEPT {
149  this->WriteStartElement_(prefix, namespace_uri, local_name);
150  this->WriteEndElement();
151  }
152  virtual void WriteNamespace_(const ExiChar* prefix,
153  const ExiChar* namespace_uri) NLIB_NOEXCEPT = 0;
154  virtual void WriteStartDocument_() NLIB_NOEXCEPT = 0;
155  virtual void WriteEndDocument_() NLIB_NOEXCEPT = 0;
156  virtual void WriteEndElement_() NLIB_NOEXCEPT = 0;
157 
158  protected:
159  static bool IsNullOrEmptyString(const ExiChar* p) NLIB_NOEXCEPT { return (!p) || (*p == '\0'); }
160  void SetExiErrorStatus(ExiErrorStatus* p) NLIB_NOEXCEPT { error_status_ = p; }
161  ExiErrorStatus* GetExiErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
162  explicit XmlStreamWriter(const ExiAllocatorEx& al) NLIB_NOEXCEPT : error_status_(NULL),
163  al_(al),
164  is_local_al_(al) {}
165 
166  private:
167  ExiErrorStatus* error_status_;
168 
169  protected:
170  ExiAllocatorEx al_;
171  bool is_local_al_;
173 };
174 
175 } // namespace exi
176 NLIB_NAMESPACE_END
177 
178 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
179 #undef NLIB_VIS_PUBLIC
180 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
181 #endif
182 
183 #endif // INCLUDE_NN_NLIB_EXI_XMLSTREAMWRITER_H_
bool encodeCookie
データの先頭に識別用の文字列を書きこむかを設定します。
void WriteDefaultNamespace(const ExiChar *namespace_uri) noexcept
デフォルト名前空間を宣言します。
void WriteStartElement(const ExiChar *local_name) noexcept
名前空間なしの要素で開始タグを書き込みます。
文字列型のtypedefやユーティリティマクロ等が定義されています。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:145
void WriteNamespace(const ExiChar *prefix, const ExiChar *namespace_uri) noexcept
XML名前空間を宣言します。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:160
XmlProcessor processor
バイナリXMLを書きだすかテキストXMLを書きだすか指定します。
ExiErrorStatus::ErrorValue GetError() const noexcept
エラー値を取得します。
読み書きするバイナリXMLのデータ保持オプションです。
Definition: Types.h:34
int version
バイナリXMLに記録されるバージョン番号を指定します。
void WriteEndElement() noexcept
終了タグを書き込みます。
バイナリXMLの利用
Definition: Types.h:49
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:61
void WriteEmptyElement(const ExiChar *local_name) noexcept
名前空間なしの要素で開始タグと終了タグをを書き込みます。
XMLパーサーのエラー状態を設定・格納します。
ExiErrorStatus * GetErrorStatus() const noexcept
エラー状態オブジェクトを取得します。
constexpr XmlStreamWriterSettings() noexcept
デフォルト設定がセットされます。
bool encodeOptions
設定の自動認識のための情報を書き込むかを設定します。
void WriteEndDocument() noexcept
開いている任意の要素を全て閉じ、XMLライタをクローズします。
Alignment
読み書きするバイナリXMLのアライメントを指定します。
Definition: Types.h:32
XmlStreamReader, XmlStreamWriter の各インスタンス毎に設定できるアロケータです。
Definition: ExiAllocator.h:22
#define NLIB_EXI_ESTR
空文字列が定義されています。""又はL""です。
Definition: Types.h:24
XmlProcessor
利用するXmlプロセッサの指定オプションです。
Definition: Types.h:49
ErrorValue
エラー状態を示す列挙型です。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:86
Alignment alignment
バイナリXMLのアライメントを設定します。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:80
bool fragment
複数のルートノードを持つXML(XMLフラグメント)の書き込みをサポートします。デフォルトはfalseです。 ...
static XmlStreamWriter * Create(OutputStream *stream) noexcept
XmlStreamWriterSettingsをデフォルト設定でXmlStreamReaderのインスタンスを作成します。 ...
Preserve preserve
各種フィデリティオプションを設定します。
XMLパーサーが返すエラーを定義しています。
static XmlStreamWriter * Create(OutputStream *stream, const XmlStreamWriterSettings &settings) noexcept
XmlStreamWriterのインスタンスを作成します。
XmlStreamWriter の初期化オプションとなる構造体です。
void WriteAttribute(const ExiChar *local_name, const ExiChar *value) noexcept
名前空間なしの属性と値を書き込みます。
void WriteCData(const ExiChar *data) noexcept
引数で指定したテキストを書き込みます。
static XmlStreamWriter * Create(OutputStream *stream, const ExiAllocatorEx &al) noexcept
オブジェクト単位のアロケータを指定してXmlStreamWriterのインスタンスを作成します。 ...
void Flush() noexcept
XMLライタをフラッシュします。
XMLのストリームへの書き出しを行う抽象クラスです。
void WriteCharacters(const ExiChar *text) noexcept
引数で指定したテキストを書き込みます。
void Close() noexcept
XmlStreamWriterをクローズします。基となるストリームは切り離されるだけでクローズされません。 ...
const ExiChar * GetPrefix(const ExiChar *uri) noexcept
名前空間URIに対応するプレフィックスを取得します。
ビット単位にデータが詰められたEXIストリームを読み書きします(デフォルト)。
Definition: Types.h:32
出力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: OutputStream.h:17
void WriteStartDocument() noexcept
XMLドキュメントの開始を宣言し、XML宣言等を書き込みます。
wchar_t ExiChar
XMLパーサーの内部文字列型のtypedefです。
Definition: Types.h:23