NCBI C++ ToolKit
objistrxml.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef OBJISTRXML__HPP
2 #define OBJISTRXML__HPP
3 
4 /* $Id: objistrxml.hpp 97030 2022-06-10 12:57:23Z ludwigf $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 * Decode input data in XML format
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/objistr.hpp>
37 #include <corelib/tempstr.hpp>
38 
39 
40 /** @addtogroup ObjStreamSupport
41  *
42  * @{
43  */
44 
45 
47 
48 class CItemsInfo;
49 
50 /////////////////////////////////////////////////////////////////////////////
51 ///
52 /// CObjectIStreamXml --
53 ///
54 /// Decode input data in XML format
56 {
57 public:
58  CObjectIStreamXml(void);
59  ~CObjectIStreamXml(void);
60 
61  /// Constructor.
62  ///
63  /// @param in
64  /// input stream
65  /// @param deleteIn
66  /// When eTakeOwnership, the input stream will be deleted automatically
67  /// when the reader is deleted
69 
70  /// Check if there is still some meaningful data that can be read;
71  /// this function will skip white spaces and comments
72  ///
73  /// @return
74  /// TRUE if there is no more data
75  virtual bool EndOfData(void) override;
76 
77  /// Get current stream location as tuple (positiontype:string, size_t).
78  /// Useful for customized diagnostic and information messages.
79  /// The meaning of positiontype depends on the implementation and coincides
80  /// with the prefix of the string returned by GetPosition().
81  ///
82  virtual void Location(string&, size_t&) const override;
83 
84  /// Get XML character encoding
85  ///
86  /// @return
87  /// Encoding
88  EEncoding GetEncoding(void) const;
89 
90  /// Set default encoding of 'string' objects
91  /// If XML data encoding is different, string will be converted to
92  /// this encoding
93  ///
94  /// @param enc
95  /// Encoding
96  void SetDefaultStringEncoding(EEncoding enc);
97 
98  /// Get default encoding of 'string' objects
99  ///
100  /// @return
101  /// Encoding
102  EEncoding GetDefaultStringEncoding(void) const;
103 
104  /// Set up scope prefixes handling.
105  /// Historically, using scope prefixes is the default for C++ data objects
106  /// generated by ASN.1 specification.
107  /// For objects generated by DTD, the default is NOT using prefixes.
108  ///
109  /// @param set
110  /// When TRUE, the stream omits 'scope prefixes'.
111  void SetEnforcedStdXml(bool set = true);
112 
113  /// Get scope prefixes handling parameter.
114  ///
115  /// @return
116  /// TRUE (omit scope prefixes) or FALSE
117  bool GetEnforcedStdXml(void) {
118  return m_StdXml ? false :
119  (GetFlags()&fFlagEnforcedStdXml) != 0;
120  }
121 
122  virtual string ReadFileHeader(void) override;
123  virtual string PeekNextTypeName(void) override;
124  void FindFileHeader(bool find_XMLDecl = true);
125 
126 protected:
127  EPointerType ReadPointerType(void) override;
128  TObjectIndex ReadObjectPointer(void) override;
129  string ReadOtherPointer(void) override;
130 
131  virtual bool ReadBool(void) override;
132  virtual char ReadChar(void) override;
133  virtual Int4 ReadInt4(void) override;
134  virtual Uint4 ReadUint4(void) override;
135  virtual Int8 ReadInt8(void) override;
136  virtual Uint8 ReadUint8(void) override;
137  virtual double ReadDouble(void) override;
138  virtual void ReadNull(void) override;
139  virtual void ReadString(string& s,EStringType type = eStringTypeVisible) override;
140  virtual char* ReadCString(void) override;
141  TEnumValueType ReadEnum(const CEnumeratedTypeValues& values) override;
142 
143  bool ReadAnyContent(const string& ns_prefix, string& value);
144  virtual void ReadAnyContentObject(CAnyContentObject& obj) override;
145  bool SkipAnyContent(void);
146  virtual void SkipAnyContentObject(void) override;
147 
148  virtual void ReadBitString(CBitString& obj) override;
149  virtual void SkipBitString(void) override;
150 
151  virtual void StartDelayBuffer(void) override;
152  virtual CRef<CByteSource> EndDelayBuffer(void) override;
153 
154  virtual void SkipBool(void) override;
155  virtual void SkipChar(void) override;
156  virtual void SkipSNumber(void) override;
157  virtual void SkipUNumber(void) override;
158  virtual void SkipFNumber(void) override;
159  virtual void SkipString(EStringType type = eStringTypeVisible) override;
160  virtual void SkipNull(void) override;
161  virtual void SkipByteBlock(void) override;
162 
163  CTempString SkipTagName(CTempString tag, const char* s, size_t length);
166  CTempString SkipStackTagName(CTempString tag, size_t level);
167  CTempString SkipStackTagName(CTempString tag, size_t level, char c);
168 
169  bool HasAttlist(void);
170  bool NextIsTag(void);
171  bool NextTagIsClosing(void);
172  bool ThisTagIsSelfClosed(void);
173  void OpenTag(const string& e);
174  void CloseTag(const string& e);
175  void OpenStackTag(size_t level);
176  void CloseStackTag(size_t level);
179  void OpenTagIfNamed(TTypeInfo type);
180  void CloseTagIfNamed(TTypeInfo type);
181  bool WillHaveName(TTypeInfo elementType);
182 
183  TMemberIndex HasAnyContent(const CClassTypeInfoBase* classType, TMemberIndex pos = kInvalidMember);
184  bool HasMoreElements(TTypeInfo elementType);
185  TMemberIndex FindDeep(TTypeInfo type, const CTempString& name) const;
186 #ifdef VIRTUAL_MID_LEVEL_IO
187  virtual void ReadNamedType(TTypeInfo namedTypeInfo,
188  TTypeInfo typeInfo,
189  TObjectPtr object) override;
190 
191  virtual void ReadContainer(const CContainerTypeInfo* containerType,
192  TObjectPtr containerPtr) override;
193  virtual void SkipContainer(const CContainerTypeInfo* containerType) override;
194 #endif
195  void ReadContainerContents(const CContainerTypeInfo* containerType,
196  TObjectPtr containerPtr);
197  void SkipContainerContents(const CContainerTypeInfo* containerType);
198 
199  // low level I/O
200  virtual void BeginNamedType(TTypeInfo namedTypeInfo) override;
201  virtual void EndNamedType(void) override;
202 
203  virtual void BeginContainer(const CContainerTypeInfo* containerType) override;
204  virtual void EndContainer(void) override;
205  virtual bool BeginContainerElement(TTypeInfo elementType) override;
206  virtual void EndContainerElement(void) override;
207  void BeginArrayElement(TTypeInfo elementType);
208  void EndArrayElement(void);
209 
210  void CheckStdXml(TTypeInfo classType);
211 
212  virtual void BeginClass(const CClassTypeInfo* classInfo) override;
213  virtual void EndClass(void) override;
214  virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType) override;
215  virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType,
216  TMemberIndex pos) override;
217  virtual void EndClassMember(void) override;
218  virtual void UndoClassMember(void) override;
219 
220  virtual void BeginChoice(const CChoiceTypeInfo* choiceType) override;
221  virtual void EndChoice(void) override;
222  virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo* choiceType) override;
223  virtual void EndChoiceVariant(void) override;
224 
225  virtual void BeginBytes(ByteBlock& ) override;
226  int GetHexChar(void);
227  int GetBase64Char(void);
228  virtual size_t ReadBytes(ByteBlock& block, char* dst, size_t length) override;
229 
230  virtual void BeginChars(CharBlock& ) override;
231  virtual size_t ReadChars(CharBlock& block, char* dst, size_t length) override;
232 
233  virtual void ResetState(void) override;
234 
235 private:
236  bool OutsideTag(void) const;
237  bool InsideTag(void) const;
238  bool InsideOpeningTag(void) const;
239  bool InsideClosingTag(void) const;
240  bool SelfClosedTag(void) const;
241 
242  void Found_lt(void);
243  void Back_lt(void);
244  void Found_lt_slash(void);
245  void Found_gt(void);
246  void Found_slash_gt(void);
247 
248  void EndSelfClosedTag(void);
249 
250  void EndTag(void);
251  void EndOpeningTag(void);
252  bool EndOpeningTagSelfClosed(void); // true if '/>' found, false if '>'
253  void EndClosingTag(void);
254  char BeginOpeningTag(void);
255  char BeginClosingTag(void);
256  void BeginData(void);
257  bool UseSpecialCaseRead(void);
258 
259  int ReadEscapedChar(char endingChar, bool* encoded=0);
260  int ReadEncodedChar(char endingChar, EStringType type, bool& encoded);
261  TUnicodeSymbol ReadUtf8Char(char ch);
262  bool ReadCDSection(string& s);
263  void ReadTagData(string& s, EStringType type = eStringTypeVisible);
264  void ReadWord(string& s, EStringType type = eStringTypeVisible);
265 
266  CTempString ReadName(char c);
267  CTempString RejectedName(void);
268  CTempString ReadAttributeName(void);
269  void ReadAttributeValue(string& value, bool skipClosing=false);
270  char ReadUndefinedAttributes(void);
271 
272  void SkipAttributeValue(char c);
273  void SkipQDecl(void);
274 
275  char SkipWS(void);
276  char SkipWSAndComments(void);
277 
278  void UnexpectedMember(const CTempString& id, const CItemsInfo& items);
279  bool x_IsStdXml(void) {return m_StdXml || (GetFlags()&fFlagEnforcedStdXml);}
280  void x_EndTypeNamespace(void);
281 
282  template<typename Type>
283  Type x_UseMemberDefault(void);
284  int x_VerifyChar(int);
285  int x_ReadEncodedChar(char endingChar, EStringType type, bool& encoded);
286 
287  enum ETagState {
291  eTagSelfClosed
292  };
294  string m_LastTag;
297  bool m_Attlist;
298  bool m_StdXml;
300  bool m_IsNil;
308  CStringUTF8::const_iterator m_Utf8Pos;
310 };
311 
312 
313 /* @} */
314 
315 
317 
319 
320 #endif /* OBJISTRXML__HPP */
int GetHexChar(CStreamBuffer &b)
Definition: test.cpp:235
#define false
Definition: bool.h:36
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:264
CObjectIStreamXml –.
Definition: objistrxml.hpp:56
CObjectIStream –.
Definition: objistr.hpp:93
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Definition: svg.hpp:69
Definition: set.hpp:45
char value[7]
Definition: config.c:431
Include a standard set of the NCBI C++ Toolkit most basic headers.
void ReadWord(CNcbiIstream &is, word_t &data)
Read a word from the input stream.
Definition: dbindex_sp.hpp:55
void * TObjectPtr
Definition: serialdef.hpp:55
int TEnumValueType
Definition: serialdef.hpp:232
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
const TMemberIndex kInvalidMember
Special value returned from FindMember.
Definition: serialdef.hpp:237
EStringType
String type.
Definition: serialdef.hpp:185
@ eStringTypeVisible
VisibleString (in ASN.1 sense)
Definition: serialdef.hpp:186
virtual void ReadString(string &s, EStringType type=eStringTypeVisible)=0
virtual EPointerType ReadPointerType(void)=0
virtual bool ReadBool(void)=0
bool InsideClosingTag(void) const
virtual void EndClassMember(void)
Definition: objistr.cpp:1372
virtual void Location(string &, size_t &) const
Get current stream location as tuple (positiontype:string, size_t).
Definition: objistr.cpp:815
bool SelfClosedTag(void) const
virtual void EndContainerElement(void)
Definition: objistr.cpp:1314
bool InsideOpeningTag(void) const
bool GetEnforcedStdXml(void)
Get scope prefixes handling parameter.
Definition: objistrxml.hpp:117
virtual void ReadNull(void)=0
virtual void UndoClassMember(void)
Definition: objistr.hpp:995
virtual void EndContainer(void)=0
void EndClosingTag(void)
virtual void SkipByteBlock(void)=0
virtual bool EndOfData(void)
Check if there is still some meaningful data that can be read; in text streams this function will ski...
Definition: objistr.cpp:588
virtual TMemberIndex BeginClassMember(const CClassTypeInfo *classType)=0
virtual size_t ReadChars(CharBlock &block, char *buffer, size_t count)=0
virtual string ReadOtherPointer(void)=0
virtual void SkipString(EStringType type=eStringTypeVisible)=0
MLIOVIR void ReadContainer(const CContainerTypeInfo *containerType, TObjectPtr containerPtr)
Definition: objistr.cpp:1318
EEncoding m_StringEncoding
Definition: objistrxml.hpp:303
void OpenTag(TTypeInfo type)
virtual void SkipFNumber(void)=0
CStringUTF8 m_Utf8Buf
Definition: objistrxml.hpp:307
map< string, string > m_NsNameToPrefix
Definition: objistrxml.hpp:306
virtual void BeginBytes(ByteBlock &block)=0
virtual void BeginNamedType(TTypeInfo namedTypeInfo)
Definition: objistr.cpp:1276
TFlags GetFlags(void) const
void Found_slash_gt(void)
virtual double ReadDouble(void)=0
virtual string ReadFileHeader(void)
Read file header.
Definition: objistr.cpp:1121
virtual void ResetState(void) override
Definition: objistr.cpp:486
virtual void SkipBool(void)=0
bool InsideTag(void) const
void EndSelfClosedTag(void)
void Found_lt_slash(void)
virtual void EndNamedType(void)
Definition: objistr.cpp:1280
virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo *choiceType)=0
virtual void SkipSNumber(void)=0
virtual void BeginContainer(const CContainerTypeInfo *containerType)=0
virtual void SkipUNumber(void)=0
virtual Uint4 ReadUint4(void)
Definition: objistr.cpp:1724
virtual char * ReadCString(void)
Definition: objistr.cpp:1750
void Found_gt(void)
virtual void SkipNull(void)=0
CTempString SkipTagName(CTempString tag, const string &s)
virtual void BeginChars(CharBlock &block)=0
virtual string PeekNextTypeName(void)
Peek next data type name in XML stream.
Definition: objistr.cpp:1128
virtual char ReadChar(void)=0
void Back_lt(void)
ETagState m_TagState
Definition: objistrxml.hpp:293
virtual void ReadAnyContentObject(CAnyContentObject &obj)=0
EEncoding m_Encoding
Definition: objistrxml.hpp:302
virtual size_t ReadBytes(ByteBlock &block, char *buffer, size_t count)=0
virtual void ReadBitString(CBitString &obj)=0
bool OutsideTag(void) const
virtual void SkipBitString(void)=0
void BeginData(void)
virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues &values)=0
void Found_lt(void)
MLIOVIR void ReadNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo, TObjectPtr object)
Definition: objistr.cpp:1284
virtual bool BeginContainerElement(TTypeInfo elementType)=0
virtual Int8 ReadInt8(void)=0
virtual TObjectIndex ReadObjectPointer(void)=0
CStringUTF8::const_iterator m_Utf8Pos
Definition: objistrxml.hpp:308
MLIOVIR void SkipContainer(const CContainerTypeInfo *containerType)
Definition: objistr.cpp:1349
virtual Int4 ReadInt4(void)
Definition: objistr.cpp:1715
void CloseTag(TTypeInfo type)
virtual void SkipAnyContentObject(void)=0
virtual Uint8 ReadUint8(void)=0
map< string, string > m_NsPrefixToName
Definition: objistrxml.hpp:305
virtual void EndClass(void)
Definition: objistr.cpp:1368
virtual void StartDelayBuffer(void)
Definition: objistr.cpp:1014
bool x_IsStdXml(void)
Definition: objistrxml.hpp:279
virtual void EndChoiceVariant(void)
Definition: objistr.cpp:1478
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
virtual void EndChoice(void)
Definition: objistr.cpp:1475
CTempString SkipTagName(CTempString tag, const char *s)
virtual CRef< CByteSource > EndDelayBuffer(void)
Definition: objistr.cpp:1019
void EndOpeningTag(void)
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objistr.cpp:1472
virtual void SkipChar(void)=0
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
std::string CStringUTF8
Definition: ncbistl.hpp:254
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
EEncoding
Definition: ncbistr.hpp:199
Uint4 TUnicodeSymbol
Unicode character.
Definition: ncbistr.hpp:141
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
const char * tag
std::istream & in(std::istream &in_, double &x_)
Definition: type.c:6
#define Type
Modified on Fri Mar 01 10:07:14 2024 by modify_doxy.py rev. 669887