nlib
XmlStreamReader.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_EXI_XMLSTREAMREADER_H_
4 #define INCLUDE_NN_NLIB_EXI_XMLSTREAMREADER_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 InputStream;
16 
17 namespace exi {
18 
19 // See:
20 // http://java.sun.com/javase/ja/6/docs/ja/api/javax/xml/stream/XMLStreamReader.html
21 
25  bool fragment;
29 
30  public:
32  alignment(ALIGNMENT_BIT_PACKED),
33  fragment(false),
34  isVersionIgnored(false),
35  processor(XML_PROCESSOR_EXI),
36  tempStringMaxLength(1023) {}
37 };
38 
39 namespace core {
40 class ExiStringAllocator;
41 }
42 
44  public:
46  NONE = -1,
47  START_ELEMENT = 1,
48  END_ELEMENT = 2,
49  PROCESSING_INSTRUCTION = 3,
50  CHARACTERS = 4,
51  COMMENT = 5,
52  // SPACE = 6,
53  START_DOCUMENT = 7,
54  END_DOCUMENT = 8,
55  // ENTITY_REFERENCE = 9,
56  // ATTRIBUTE = 10,
57  // DTD = 11,
58  CDATA = 12
59  // NAMESPACE = 13,
60  // NOTATION_DECLARATION = 14,
61  // ENTITY_DECLARATION = 15
62  };
63  static XmlStreamReader* Create(InputStream* stream, const XmlStreamReaderSettings& settings,
64  const ExiAllocatorEx& al) NLIB_NOEXCEPT;
66  const XmlStreamReaderSettings& settings) NLIB_NOEXCEPT {
67  ExiAllocatorEx al;
68  return Create(stream, settings, al);
69  }
70  static XmlStreamReader* Create(InputStream* stream) NLIB_NOEXCEPT {
72  return Create(stream, settings);
73  }
74  static XmlStreamReader* Create(InputStream* stream, const ExiAllocatorEx& al) NLIB_NOEXCEPT {
76  return Create(stream, settings, al);
77  }
78 
79  public:
80  virtual ~XmlStreamReader() NLIB_NOEXCEPT {
81  if (is_local_al_) ExiAllocator::Swap(al_);
82  }
83  void Close() NLIB_NOEXCEPT {
84  ExiAllocatorSwapper swapper(al_);
85  this->CloseV();
86  }
87  size_t GetAttributeCount() NLIB_NOEXCEPT {
88  ExiAllocatorSwapper swapper(al_);
89  return this->GetAttributeCountV();
90  }
91  const ExiChar* GetAttributeLocalName(size_t index) NLIB_NOEXCEPT {
92  ExiAllocatorSwapper swapper(al_);
93  return this->GetAttributeLocalNameV(index);
94  }
95  const ExiChar* GetAttributeNamespaceUri(size_t index) NLIB_NOEXCEPT {
96  ExiAllocatorSwapper swapper(al_);
97  return this->GetAttributeNamespaceUriV(index);
98  }
99  const ExiChar* GetAttributePrefix(size_t index) NLIB_NOEXCEPT {
100  ExiAllocatorSwapper swapper(al_);
101  return this->GetAttributePrefixV(index);
102  }
103  const ExiChar* GetAttributeType(size_t index) NLIB_NOEXCEPT {
104  ExiAllocatorSwapper swapper(al_);
105  return this->GetAttributeTypeV(index);
106  }
107  const ExiChar* GetAttributeValue(size_t index) NLIB_NOEXCEPT {
108  ExiAllocatorSwapper swapper(al_);
109  return this->GetAttributeValueV(index);
110  }
111  const ExiChar* GetAttributeValue(const ExiChar* namespace_uri,
112  const ExiChar* local_name) NLIB_NOEXCEPT {
113  ExiAllocatorSwapper swapper(al_);
114  return this->GetAttributeValueV(namespace_uri, local_name);
115  }
116  size_t GetNamespaceCount() NLIB_NOEXCEPT {
117  ExiAllocatorSwapper swapper(al_);
118  return this->GetNamespaceCountV();
119  }
120  const ExiChar* GetNamespacePrefix(size_t index) NLIB_NOEXCEPT {
121  ExiAllocatorSwapper swapper(al_);
122  return this->GetNamespacePrefixV(index);
123  }
124  const ExiChar* GetNamespaceUri(size_t index) NLIB_NOEXCEPT {
125  ExiAllocatorSwapper swapper(al_);
126  return this->GetNamespaceUriV(index);
127  }
128  const ExiChar* GetElementText() NLIB_NOEXCEPT {
129  ExiAllocatorSwapper swapper(al_);
130  return this->GetElementTextV();
131  }
132  const ExiChar* GetLocalName() NLIB_NOEXCEPT {
133  ExiAllocatorSwapper swapper(al_);
134  return this->GetLocalNameV();
135  }
136  const ExiChar* GetNamespaceUri() NLIB_NOEXCEPT {
137  ExiAllocatorSwapper swapper(al_);
138  return this->GetNamespaceUriV();
139  }
140  const ExiChar* GetPrefix() NLIB_NOEXCEPT {
141  ExiAllocatorSwapper swapper(al_);
142  return this->GetPrefixV();
143  }
144  const ExiChar* GetText() NLIB_NOEXCEPT {
145  ExiAllocatorSwapper swapper(al_);
146  return this->GetTextV();
147  }
148  int GetLineNo() const NLIB_NOEXCEPT { return this->GetLineNoV(); }
149  int GetColumnNo() const NLIB_NOEXCEPT { return this->GetColumnNoV(); }
150  XmlStreamConstants Next() NLIB_NOEXCEPT {
151  ExiAllocatorSwapper swapper(al_);
152  return this->NextV();
153  }
154  XmlStreamConstants GetEventType() const NLIB_NOEXCEPT {
155  return static_cast<XmlStreamConstants>(state_);
156  }
157  bool HasName() const NLIB_NOEXCEPT {
158  return state_ == START_ELEMENT || state_ == END_ELEMENT ||
159  state_ == PROCESSING_INSTRUCTION;
160  }
161  bool HasNext() const NLIB_NOEXCEPT { return state_ != END_DOCUMENT && state_ != NONE; }
162  bool HasText() const NLIB_NOEXCEPT {
163  return state_ == CHARACTERS || state_ == CDATA || state_ == COMMENT ||
164  state_ == PROCESSING_INSTRUCTION;
165  }
166  bool IsCharacters() const NLIB_NOEXCEPT { return state_ == CHARACTERS || state_ == CDATA; }
167  bool IsEndElement() const NLIB_NOEXCEPT { return state_ == END_ELEMENT; }
168  bool IsStartElement() const NLIB_NOEXCEPT { return state_ == START_ELEMENT; }
169  ExiErrorStatus::ErrorValue GetError() const NLIB_NOEXCEPT {
170  return error_status_->GetError();
171  }
172  ExiErrorStatus* GetErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
173  NLIB_SAFE_BOOL(XmlStreamReader, !!*error_status_)
174 
175  protected:
176  explicit XmlStreamReader(const ExiAllocatorEx& al) NLIB_NOEXCEPT : error_status_(NULL),
177  al_(al),
178  is_local_al_(al),
179  state_(START_DOCUMENT) {}
180 
181  void SetExiErrorStatus(ExiErrorStatus* p) NLIB_NOEXCEPT { error_status_ = p; }
182  ExiErrorStatus* GetExiErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
183 
184  // template functions to override virtual functions:
185  template <class DECODER>
186  size_t GetAttributeCount_(DECODER* decoder) NLIB_NOEXCEPT;
187  template <class DECODER>
188  const ExiChar* GetAttributeLocalName_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
189  template <class DECODER>
190  const ExiChar* GetAttributeNamespaceUri_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
191  template <class DECODER>
192  const ExiChar* GetAttributePrefix_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
193  template <class DECODER>
194  const ExiChar* GetAttributeValue_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
195  template <class DECODER>
196  const ExiChar* GetAttributeValue_(DECODER* decoder, const ExiChar* namespace_uri,
197  const ExiChar* local_name) NLIB_NOEXCEPT;
198  template <class DECODER>
199  size_t GetNamespaceCount_(DECODER* decoder) NLIB_NOEXCEPT;
200  template <class DECODER>
201  const ExiChar* GetNamespacePrefix_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
202  template <class DECODER>
203  const ExiChar* GetNamespaceUri_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
204 
205  template <class DECODER>
206  const ExiChar* GetLocalName_(DECODER* decoder) NLIB_NOEXCEPT;
207  template <class DECODER>
208  const ExiChar* GetNamespaceUri_(DECODER* decoder) NLIB_NOEXCEPT;
209  template <class DECODER>
210  const ExiChar* GetPrefix_(DECODER* decoder) NLIB_NOEXCEPT;
211  template <class DECODER>
212  const ExiChar* GetText_(DECODER* decoder) NLIB_NOEXCEPT;
213  const ExiChar* GetElementText_(core::ExiStringAllocator* alloc) NLIB_NOEXCEPT;
214 
215  private:
216  virtual void CloseV() NLIB_NOEXCEPT = 0;
217  virtual size_t GetAttributeCountV() NLIB_NOEXCEPT = 0;
218  virtual const ExiChar* GetAttributeLocalNameV(size_t index) NLIB_NOEXCEPT = 0;
219  virtual const ExiChar* GetAttributeNamespaceUriV(size_t index) NLIB_NOEXCEPT = 0;
220  virtual const ExiChar* GetAttributePrefixV(size_t index) NLIB_NOEXCEPT = 0;
221  virtual const ExiChar* GetAttributeTypeV(size_t index) NLIB_NOEXCEPT = 0;
222  virtual const ExiChar* GetAttributeValueV(size_t index) NLIB_NOEXCEPT = 0;
223  virtual const ExiChar* GetAttributeValueV(const ExiChar* namespace_uri,
224  const ExiChar* local_name) NLIB_NOEXCEPT = 0;
225  virtual size_t GetNamespaceCountV() NLIB_NOEXCEPT = 0;
226  virtual const ExiChar* GetNamespacePrefixV(size_t index) NLIB_NOEXCEPT = 0;
227  virtual const ExiChar* GetNamespaceUriV(size_t index) NLIB_NOEXCEPT = 0;
228  virtual const ExiChar* GetElementTextV() NLIB_NOEXCEPT = 0;
229  virtual const ExiChar* GetLocalNameV() NLIB_NOEXCEPT = 0;
230  virtual const ExiChar* GetNamespaceUriV() NLIB_NOEXCEPT = 0;
231  virtual const ExiChar* GetPrefixV() NLIB_NOEXCEPT = 0;
232  virtual const ExiChar* GetTextV() NLIB_NOEXCEPT = 0;
233  virtual int GetLineNoV() const NLIB_NOEXCEPT { return 0; }
234  virtual int GetColumnNoV() const NLIB_NOEXCEPT { return 0; }
235  virtual XmlStreamConstants NextV() NLIB_NOEXCEPT = 0;
236 
237  private:
238  ExiErrorStatus* error_status_;
239 
240  protected:
241  ExiAllocatorEx al_;
242  bool is_local_al_;
243  int state_; // XmlStreamConstants
245 };
246 
247 } // namespace exi
248 NLIB_NAMESPACE_END
249 
250 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
251 #undef NLIB_VIS_PUBLIC
252 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
253 #endif
254 
255 #endif // INCLUDE_NN_NLIB_EXI_XMLSTREAMREADER_H_
XmlStreamConstants Next() noexcept
Reads the next XML stream event from the stream.
static XmlStreamReader * Create(InputStream *stream, const ExiAllocatorEx &al) noexcept
Creates an XmlStreamReader instance by specifying an objectwise allocator.
size_t GetAttributeCount() noexcept
Gets the number of attributes.
Defines constructs such as string-type typedef statements and utility macros.
static XmlStreamReader * Create(InputStream *stream, const XmlStreamReaderSettings &settings) noexcept
Creates an instance of XmlStreamReader.
#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:145
const ExiChar * GetNamespacePrefix(size_t index) noexcept
Gets the prefix of a namespace declaration.
const ExiChar * GetLocalName() noexcept
Gets the local name.
#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:160
const ExiChar * GetAttributePrefix(size_t index) noexcept
Gets the prefix of an attribute.
Set of options that preserve the binary XML to read or write.
Definition: Types.h:34
const ExiChar * GetAttributeValue(const ExiChar *namespace_uri, const ExiChar *local_name) noexcept
Gets the value of the attribute based on a particular namespace URI and local name.
const ExiChar * GetAttributeLocalName(size_t index) noexcept
Gets the local name of an attribute.
const ExiChar * GetNamespaceUri(size_t index) noexcept
Gets the namespace URI of a namespace declaration.
const ExiChar * GetPrefix() noexcept
Gets the namespace prefix of the XML.
Use binary XML.
Definition: Types.h:49
const ExiChar * GetAttributeValue(size_t index) noexcept
Gets the value of an attribute.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
constexpr XmlStreamReaderSettings() noexcept
Configures the default settings.
static XmlStreamReader * Create(InputStream *stream) noexcept
Creates an XmlStreamReader instance using default settings for XmlStreamReaderSettings.
virtual ~XmlStreamReader() noexcept
Destructor.
const ExiChar * GetText() noexcept
Gets text data.
Sets and stores the error status of the XML parser.
const ExiChar * GetElementText() noexcept
Reads a series of text events and returns a single text element.
int GetColumnNo() const noexcept
Gets the column number indicated by the XmlStreamReader.
Preserve preserve
Sets the fidelity options.
const ExiChar * GetNamespaceUri() noexcept
Gets the URI of the XML namespace.
Abstract class that reads from an XML stream.
ExiErrorStatus::ErrorValue GetError() const noexcept
Gets the error value.
ExiErrorStatus * GetErrorStatus() const noexcept
Gets an error status object.
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:16
int GetLineNo() const noexcept
Gets the line number indicated by the XmlStreamReader.
Alignment
Specifies the alignment of the binary XML to read or write.
Definition: Types.h:32
const ExiChar * GetAttributeNamespaceUri(size_t index) noexcept
Gets the namespace URI of an attribute.
Allocator that can be set for each instance of XmlStreamReader and XmlStreamWriter.
Definition: ExiAllocator.h:22
size_t GetNamespaceCount() noexcept
Gets the number of newly defined namespaces.
XmlProcessor
Option that specifies which XML processor to use.
Definition: Types.h:49
size_t tempStringMaxLength
Specifies the length of the string that stores the temporary buffer used when reading XML comments an...
ErrorValue
Enumerates error statuses.
XmlProcessor processor
Specifies whether to read binary XML or text-based XML.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:80
Alignment alignment
Specifies the alignment of the binary XML to read.
bool IsStartElement() const noexcept
Gets whether the data that was read is an opening tag.
XmlStreamConstants GetEventType() const noexcept
Returns the value for the most recent XML stream event.
Defines the errors returned by the XML parser.
bool HasName() const noexcept
Returns whether data like a local name can be obtained.
bool isVersionIgnored
By setting this option to true during read operations, you can ignore the version check when reading ...
bool IsCharacters() const noexcept
Gets whether the data that was read is a text node.
XmlStreamConstants
Values of XML stream events that occurred while reading an XML stream.
bool IsEndElement() const noexcept
Gets whether the data that was read is a closing tag.
void Close() noexcept
Closes an XmlStreamReader. The original stream is not actually closed; it merely stops being referenc...
bool fragment
Reads binary XML that supports multiple root nodes (aka XML fragments).
bool HasNext() const noexcept
Returns whether the next XML stream event can be obtained.
Reads from and writes to a bit-packed EXI stream (default).
Definition: Types.h:32
bool HasText() const noexcept
Returns whether text is ready to be obtained using GetText().
wchar_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:23
Structure used as the initialization options for XmlStreamReader.