nlib
XmlStreamReader.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_XMLSTREAMREADER_H_
17 #define INCLUDE_NN_NLIB_EXI_XMLSTREAMREADER_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 InputStream;
29 
30 namespace exi {
31 
32 // See:
33 // http://java.sun.com/javase/ja/6/docs/ja/api/javax/xml/stream/XMLStreamReader.html
34 
38  bool fragment;
42 
43  public:
45  alignment(ALIGNMENT_BIT_PACKED),
46  fragment(false),
47  isVersionIgnored(false),
48  processor(XML_PROCESSOR_EXI),
49  tempStringMaxLength(1023) {}
50 };
51 
52 namespace core {
53 class ExiStringAllocator;
54 }
55 
57  public:
59  kNone = -1,
60  kStartElement = 1,
61  kEndElement = 2,
62  kProcessingInstruction = 3,
63  kCharacters = 4,
64  kComment = 5,
65  kStartDocument = 7,
66  kEndDocument = 8,
67  kCdata = 12,
68  NONE = -1,
69  START_ELEMENT = 1,
70  END_ELEMENT = 2,
71  PROCESSING_INSTRUCTION = 3,
72  CHARACTERS = 4,
73  COMMENT = 5,
74  // SPACE = 6,
75  START_DOCUMENT = 7,
76  END_DOCUMENT = 8,
77  // ENTITY_REFERENCE = 9,
78  // ATTRIBUTE = 10,
79  // DTD = 11,
80  CDATA = 12
81  // NAMESPACE = 13,
82  // NOTATION_DECLARATION = 14,
83  // ENTITY_DECLARATION = 15
84  };
85  static XmlStreamReader* Create(InputStream* stream, const XmlStreamReaderSettings& settings,
86  const ExiAllocatorEx& al) NLIB_NOEXCEPT;
87  static XmlStreamReader*
89  ExiAllocatorEx al;
90  return Create(stream, settings, al);
91  }
94  return Create(stream, settings);
95  }
98  return Create(stream, settings, al);
99  }
100 
101  public:
103  if (is_local_al_) ExiAllocator::Swap(al_);
104  }
106  ExiAllocatorSwapper swapper(al_);
107  this->CloseV();
108  }
110  ExiAllocatorSwapper swapper(al_);
111  return this->GetAttributeCountV();
112  }
114  ExiAllocatorSwapper swapper(al_);
115  return this->GetAttributeLocalNameV(index);
116  }
118  ExiAllocatorSwapper swapper(al_);
119  return this->GetAttributeNamespaceUriV(index);
120  }
121  const ExiChar* GetAttributePrefix(size_t index) NLIB_NOEXCEPT {
122  ExiAllocatorSwapper swapper(al_);
123  return this->GetAttributePrefixV(index);
124  }
125  const ExiChar* GetAttributeType(size_t index) NLIB_NOEXCEPT {
126  ExiAllocatorSwapper swapper(al_);
127  return this->GetAttributeTypeV(index);
128  }
129  const ExiChar* GetAttributeValue(size_t index) NLIB_NOEXCEPT {
130  ExiAllocatorSwapper swapper(al_);
131  return this->GetAttributeValueV(index);
132  }
133  const ExiChar*
134  GetAttributeValue(const ExiChar* namespace_uri, const ExiChar* local_name) NLIB_NOEXCEPT {
135  ExiAllocatorSwapper swapper(al_);
136  return this->GetAttributeValueV(namespace_uri, local_name);
137  }
139  ExiAllocatorSwapper swapper(al_);
140  return this->GetNamespaceCountV();
141  }
142  const ExiChar* GetNamespacePrefix(size_t index) NLIB_NOEXCEPT {
143  ExiAllocatorSwapper swapper(al_);
144  return this->GetNamespacePrefixV(index);
145  }
146  const ExiChar* GetNamespaceUri(size_t index) NLIB_NOEXCEPT {
147  ExiAllocatorSwapper swapper(al_);
148  return this->GetNamespaceUriV(index);
149  }
151  ExiAllocatorSwapper swapper(al_);
152  return this->GetElementTextV();
153  }
155  ExiAllocatorSwapper swapper(al_);
156  return this->GetLocalNameV();
157  }
159  ExiAllocatorSwapper swapper(al_);
160  return this->GetNamespaceUriV();
161  }
163  ExiAllocatorSwapper swapper(al_);
164  return this->GetPrefixV();
165  }
167  ExiAllocatorSwapper swapper(al_);
168  return this->GetTextV();
169  }
170  int GetLineNo() const NLIB_NOEXCEPT { return this->GetLineNoV(); }
171  int GetColumnNo() const NLIB_NOEXCEPT { return this->GetColumnNoV(); }
173  ExiAllocatorSwapper swapper(al_);
174  return this->NextV();
175  }
177  return static_cast<XmlStreamConstants>(state_);
178  }
179  bool HasName() const NLIB_NOEXCEPT {
180  return state_ == START_ELEMENT || state_ == END_ELEMENT || state_ == PROCESSING_INSTRUCTION;
181  }
182  bool HasNext() const NLIB_NOEXCEPT { return state_ != END_DOCUMENT && state_ != NONE; }
183  bool HasText() const NLIB_NOEXCEPT {
184  return state_ == CHARACTERS || state_ == CDATA || state_ == COMMENT ||
185  state_ == PROCESSING_INSTRUCTION;
186  }
187  bool IsCharacters() const NLIB_NOEXCEPT { return state_ == CHARACTERS || state_ == CDATA; }
188  bool IsEndElement() const NLIB_NOEXCEPT { return state_ == END_ELEMENT; }
189  bool IsStartElement() const NLIB_NOEXCEPT { return state_ == START_ELEMENT; }
190  ExiErrorStatus::ErrorValue GetError() const NLIB_NOEXCEPT { return error_status_->GetError(); }
191  ExiErrorStatus* GetErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
192  NLIB_SAFE_BOOL(XmlStreamReader, !!*error_status_)
193 
194  protected:
195  explicit XmlStreamReader(const ExiAllocatorEx& al) NLIB_NOEXCEPT : error_status_(nullptr),
196  al_(al),
197  is_local_al_(al),
198  state_(START_DOCUMENT) {}
199 
200  void SetExiErrorStatus(ExiErrorStatus* p) NLIB_NOEXCEPT { error_status_ = p; }
201  ExiErrorStatus* GetExiErrorStatus() const NLIB_NOEXCEPT { return error_status_; }
202 
203  // template functions to override virtual functions:
204  template<class DECODER>
205  size_t GetAttributeCount_(DECODER* decoder) NLIB_NOEXCEPT;
206  template<class DECODER>
207  const ExiChar* GetAttributeLocalName_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
208  template<class DECODER>
209  const ExiChar* GetAttributeNamespaceUri_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
210  template<class DECODER>
211  const ExiChar* GetAttributePrefix_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
212  template<class DECODER>
213  const ExiChar* GetAttributeValue_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
214  template<class DECODER>
215  const ExiChar* GetAttributeValue_(DECODER* decoder, const ExiChar* namespace_uri,
216  const ExiChar* local_name) NLIB_NOEXCEPT;
217  template<class DECODER>
218  size_t GetNamespaceCount_(DECODER* decoder) NLIB_NOEXCEPT;
219  template<class DECODER>
220  const ExiChar* GetNamespacePrefix_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
221  template<class DECODER>
222  const ExiChar* GetNamespaceUri_(DECODER* decoder, size_t index) NLIB_NOEXCEPT;
223 
224  template<class DECODER>
225  const ExiChar* GetLocalName_(DECODER* decoder) NLIB_NOEXCEPT;
226  template<class DECODER>
227  const ExiChar* GetNamespaceUri_(DECODER* decoder) NLIB_NOEXCEPT;
228  template<class DECODER>
229  const ExiChar* GetPrefix_(DECODER* decoder) NLIB_NOEXCEPT;
230  template<class DECODER>
231  const ExiChar* GetText_(DECODER* decoder) NLIB_NOEXCEPT;
232  const ExiChar* GetElementText_(core::ExiStringAllocator* alloc) NLIB_NOEXCEPT;
233 
234  private:
235  virtual void CloseV() NLIB_NOEXCEPT = 0;
236  virtual size_t GetAttributeCountV() NLIB_NOEXCEPT = 0;
237  virtual const ExiChar* GetAttributeLocalNameV(size_t index) NLIB_NOEXCEPT = 0;
238  virtual const ExiChar* GetAttributeNamespaceUriV(size_t index) NLIB_NOEXCEPT = 0;
239  virtual const ExiChar* GetAttributePrefixV(size_t index) NLIB_NOEXCEPT = 0;
240  virtual const ExiChar* GetAttributeTypeV(size_t index) NLIB_NOEXCEPT = 0;
241  virtual const ExiChar* GetAttributeValueV(size_t index) NLIB_NOEXCEPT = 0;
242  virtual const ExiChar*
243  GetAttributeValueV(const ExiChar* namespace_uri, const ExiChar* local_name) NLIB_NOEXCEPT = 0;
244  virtual size_t GetNamespaceCountV() NLIB_NOEXCEPT = 0;
245  virtual const ExiChar* GetNamespacePrefixV(size_t index) NLIB_NOEXCEPT = 0;
246  virtual const ExiChar* GetNamespaceUriV(size_t index) NLIB_NOEXCEPT = 0;
247  virtual const ExiChar* GetElementTextV() NLIB_NOEXCEPT = 0;
248  virtual const ExiChar* GetLocalNameV() NLIB_NOEXCEPT = 0;
249  virtual const ExiChar* GetNamespaceUriV() NLIB_NOEXCEPT = 0;
250  virtual const ExiChar* GetPrefixV() NLIB_NOEXCEPT = 0;
251  virtual const ExiChar* GetTextV() NLIB_NOEXCEPT = 0;
252  virtual int GetLineNoV() const NLIB_NOEXCEPT { return 0; }
253  virtual int GetColumnNoV() const NLIB_NOEXCEPT { return 0; }
254  virtual XmlStreamConstants NextV() NLIB_NOEXCEPT = 0;
255 
256  private:
257  ExiErrorStatus* error_status_;
258 
259  protected:
260  ExiAllocatorEx al_;
261  bool is_local_al_;
262  int state_; // XmlStreamConstants
263  NLIB_DISALLOW_COPY_AND_ASSIGN(XmlStreamReader);
264 };
265 
266 } // namespace exi
267 NLIB_NAMESPACE_END
268 
269 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
270 #undef NLIB_VIS_PUBLIC
271 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
272 #endif
273 
274 #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:183
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:199
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:52
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:67
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:87
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:29
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:45
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:35
size_t GetNamespaceCount() noexcept
Gets the number of newly defined namespaces.
XmlProcessor
Option that specifies which XML processor to use.
Definition: Types.h:67
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:109
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
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:46
bool HasText() const noexcept
Returns whether text is ready to be obtained using GetText().
nlib_utf8_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:40
Structure used as the initialization options for XmlStreamReader.