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

Go to the SVN repository for this file.

1 #ifndef OBJOSTRXML__HPP
2 #define OBJOSTRXML__HPP
3 
4 /* $Id: objostrxml.hpp 84120 2018-10-17 18:15:31Z gouriano $
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 * Encode data object using XML format
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/objostr.hpp>
37 #include <deque>
38 
39 
40 /** @addtogroup ObjStreamSupport
41  *
42  * @{
43  */
44 
45 
47 
48 /////////////////////////////////////////////////////////////////////////////
49 ///
50 /// CObjectOStreamXml --
51 ///
52 /// Encode serial data object using XML format
54 {
55 public:
56 
57  /// Constructor.
58  ///
59  /// @param out
60  /// Output stream
61  /// @param deleteOut
62  /// when TRUE, the output stream will be deleted automatically
63  /// when the writer is deleted
64  /// @deprecated
65  /// Use one with EOwnership enum instead
67 
68  /// Constructor.
69  ///
70  /// @param out
71  /// Output stream
72  /// @param deleteOut
73  /// When eTakeOwnership, the output stream will be deleted automatically
74  /// when the writer is deleted
76 
77  /// Destructor.
78  virtual ~CObjectOStreamXml(void);
79 
80  /// Get current stream position as string.
81  /// Useful for diagnostic and information messages.
82  ///
83  /// @return
84  /// string
85  virtual string GetPosition(void) const override;
86 
87  /// Set XML character encoding
88  ///
89  /// @return
90  /// Encoding
91  void SetEncoding(EEncoding enc);
92 
93  /// Get XML character encoding
94  ///
95  /// @return
96  /// Encoding
97  EEncoding GetEncoding(void) const;
98 
99  /// Set default encoding of 'string' objects
100  /// If XML data encoding is different, string will be converted to
101  /// this encoding
102  ///
103  /// @param enc
104  /// Encoding
105  void SetDefaultStringEncoding(EEncoding enc);
106 
107  /// Get default encoding of 'string' objects
108  ///
109  /// @return
110  /// Encoding
111  EEncoding GetDefaultStringEncoding(void) const;
112 
113  /// Set up scope prefixes handling.
114  /// Historically, using scope prefixes is the default for C++ data objects
115  /// generated by ASN.1 specification.
116  /// For objects generated by DTD, the default is NOT using prefixes.
117  ///
118  /// @param set
119  /// When TRUE, the stream omits 'scope prefixes'.
120  void SetEnforcedStdXml(bool set = true);
121 
122  /// Get scope prefixes handling parameter.
123  ///
124  /// @return
125  /// TRUE (omit scope prefixes) or FALSE
126  bool GetEnforcedStdXml(void) {return m_StdXml ? false : m_EnforcedStdXml;}
127 
128  /// Make generated XML document reference XML schema
129  ///
130  /// @param use_schema
131  /// When TRUE, the generated document will reference Schema
132  void SetReferenceSchema(bool use_schema = true);
133 
134  /// Get Schema referencing parameter
135  ///
136  /// @return
137  /// TRUE or FALSE
138  bool GetReferenceSchema(void) const;
139 
140  /// Make generated XML document reference DTD
141  ///
142  /// @param use_dtd
143  /// When TRUE, the generated document will reference DTD
144  void SetReferenceDTD(bool use_dtd = true);
145 
146  /// Get DTD referencing parameter
147  ///
148  /// @return
149  /// TRUE or FALSE
150  bool GetReferenceDTD(void) const;
151 
152  /// Put Schema location information into generated XML document
153  ///
154  /// @param use_loc
155  /// When TRUE, the generated document will have schemaLocation attribute
156  void SetUseSchemaLocation(bool use_loc = true);
157 
158  /// Get Schema location output parameter
159  ///
160  /// @return
161  /// TRUE or FALSE
162  bool GetUseSchemaLocation(void) const;
163 
164  /// Set default value of namespace name of generated DTD documents
165  ///
166  /// @param schema_ns
167  /// namespace name
168  void SetDefaultSchemaNamespace(const string& schema_ns);
169 
170  /// Get default value of namespace name of generated DTD documents
171  ///
172  /// @return
173  /// namespace name
175 
176  /// Set DTD or schema file prefix.
177  /// Reference to DTD or schema in XML document has the form
178  /// [DTDFilePrefix][DTDFileName].[dtd|xsd]
179  /// If "DTDFilePrefix" has never been set for this stream, then
180  /// the global "DefaultDTDFilePrefix" will be used.
181  /// If it has been set to any value (including empty string), then
182  /// that value will be used.
183  ///
184  /// @param prefix
185  /// File prefix
186  void SetDTDFilePrefix(const string& prefix);
187 
188  /// Get DTD or schema file prefix.
189  ///
190  /// @return
191  /// File prefix
192  string GetDTDFilePrefix(void) const;
193 
194  /// Set default (global) DTD file prefix.
195  ///
196  /// @param prefix
197  /// File prefix
198  static void SetDefaultDTDFilePrefix(const string& prefix);
199 
200  /// Get default (global) DTD file prefix.
201  ///
202  /// @param prefix
203  /// File prefix
204  static string GetDefaultDTDFilePrefix(void);
205 
206  /// Set DTD or schema file name.
207  /// Reference to DTD or schema in XML document has the form
208  /// [DTDFilePrefix][DTDFileName].[dtd|xsd]
209  /// If "DTDFileName" is not set or set to empty string for this stream,
210  /// then module name (in ASN.1 sense) will be used as the file name.
211  ///
212  /// @param filename
213  /// File name
214  void SetDTDFileName(const string& filename);
215 
216  /// Get DTD or schema file name.
217  ///
218  /// @return
219  /// File name
220  string GetDTDFileName(void) const;
221 
222  /// Enable DTD public identifier.
223  /// If disabled (it is ENABLED by default), only system identifier
224  /// will be written in the output XML stream
225  void EnableDTDPublicId(void);
226 
227  /// Disable DTD public identifier.
228  /// If disabled (it is ENABLED by default), only system identifier
229  /// will be written in the output XML stream
230  void DisableDTDPublicId(void);
231 
232  /// Set DTD public identifier.
233  /// If set to a non-empty string, the stream will write this into the
234  /// output XML file. Otherwise the "default" public id
235  /// will be generated
236  ///
237  /// @param publicId
238  /// Public ID
239  void SetDTDPublicId(const string& publicId);
240 
241  /// Get DTD public identifier.
242  ///
243  /// @return
244  /// Public ID
245  string GetDTDPublicId(void) const;
246 
247  /// formatting of values of type 'real' ('double')
249  eRealFixedFormat, ///< use 'f' formatting type
250  eRealScientificFormat ///< use 'g' formatting type
251  };
252 
253  /// Get formatting of values of type real
254  ///
255  /// @return
256  /// Formatting flag
257  ERealValueFormat GetRealValueFormat(void) const;
258 
259  /// Set formatting of values of type real
260  /// The method is provided for convenience only.
261  ///
262  /// @param fmt
263  /// Formatting flag
264  void SetRealValueFormat(ERealValueFormat fmt);
265 
266  /// Set output formatting flags
267  ///
268  /// @param flags
269  /// Formatting flag
270  virtual void SetFormattingFlags(TSerial_Format_Flags flags) override;
271 
272  virtual void WriteFileHeader(TTypeInfo type) override;
273  virtual void EndOfWrite(void) override;
274 
275 protected:
276  virtual void WriteBool(bool data) override;
277  virtual void WriteChar(char data) override;
278  virtual void WriteInt4(Int4 data) override;
279  virtual void WriteUint4(Uint4 data) override;
280  virtual void WriteInt8(Int8 data) override;
281  virtual void WriteUint8(Uint8 data) override;
282  virtual void WriteFloat(float data) override;
283  virtual void WriteDouble(double data) override;
284  void WriteDouble2(double data, unsigned digits);
285  virtual void WriteCString(const char* str) override;
286  virtual void WriteString(const string& s,
287  EStringType type = eStringTypeVisible) override;
288  virtual void WriteStringStore(const string& s) override;
289  virtual void CopyString(CObjectIStream& in,
290  EStringType type = eStringTypeVisible) override;
291  virtual void CopyStringStore(CObjectIStream& in) override;
292 
293  virtual void WriteNullPointer(void) override;
294  virtual void WriteObjectReference(TObjectIndex index) override;
295  virtual void WriteOtherBegin(TTypeInfo typeInfo) override;
296  virtual void WriteOtherEnd(TTypeInfo typeInfo) override;
297  virtual void WriteOther(TConstObjectPtr object, TTypeInfo typeInfo) override;
298 
299  virtual void WriteNull(void) override;
300  virtual void WriteAnyContentObject(const CAnyContentObject& obj) override;
301  virtual void CopyAnyContentObject(CObjectIStream& in) override;
302 
303  virtual void WriteBitString(const CBitString& obj) override;
304  virtual void CopyBitString(CObjectIStream& in) override;
305 
306  void WriteEscapedChar(char c);
307  void WriteEncodedChar(const char*& src,
309 
310  virtual void WriteEnum(const CEnumeratedTypeValues& values,
311  TEnumValueType value) override;
312  virtual void CopyEnum(const CEnumeratedTypeValues& values,
313  CObjectIStream& in) override;
314  void WriteEnum(const CEnumeratedTypeValues& values,
315  TEnumValueType value, const string& valueName);
316 
317 #ifdef VIRTUAL_MID_LEVEL_IO
318  virtual void WriteNamedType(TTypeInfo namedTypeInfo,
319  TTypeInfo typeInfo, TConstObjectPtr object) override;
320  virtual void CopyNamedType(TTypeInfo namedTypeInfo,
321  TTypeInfo typeInfo,
322  CObjectStreamCopier& copier) override;
323 
324  virtual void WriteContainer(const CContainerTypeInfo* containerType,
325  TConstObjectPtr containerPtr) override;
326 
327  virtual void WriteClass(const CClassTypeInfo* objectType,
328  TConstObjectPtr objectPtr) override;
329  virtual void WriteClassMember(const CMemberId& memberId,
330  TTypeInfo memberType,
331  TConstObjectPtr memberPtr) override;
332  virtual bool WriteClassMember(const CMemberId& memberId,
333  const CDelayBuffer& buffer) override;
334 
335  virtual void WriteClassMemberSpecialCase(
336  const CMemberId& memberId, TTypeInfo memberType,
337  TConstObjectPtr memberPtr, ESpecialCaseWrite how) override;
338 #endif
339  void WriteContainerContents(const CContainerTypeInfo* containerType,
340  TConstObjectPtr containerPtr);
341  void WriteChoiceContents(const CChoiceTypeInfo* choiceType,
342  TConstObjectPtr choicePtr);
343  // low level I/O
344  virtual void BeginNamedType(TTypeInfo namedTypeInfo) override;
345  virtual void EndNamedType(void) override;
346 
347  virtual void BeginContainer(const CContainerTypeInfo* containerType) override;
348  virtual void EndContainer(void) override;
349  virtual void BeginContainerElement(TTypeInfo elementType) override;
350  virtual void EndContainerElement(void) override;
351  void BeginArrayElement(TTypeInfo elementType);
352  void EndArrayElement(void);
353 
354  void CheckStdXml(TTypeInfo classType);
355 
356  virtual void BeginClass(const CClassTypeInfo* classInfo) override;
357  virtual void EndClass(void) override;
358  virtual void BeginClassMember(const CMemberId& id) override;
359  void BeginClassMember(TTypeInfo memberType,
360  const CMemberId& id);
361  virtual void EndClassMember(void) override;
362 
363  virtual void BeginChoice(const CChoiceTypeInfo* choiceType) override;
364  virtual void EndChoice(void) override;
365  virtual void BeginChoiceVariant(const CChoiceTypeInfo* choiceType,
366  const CMemberId& id) override;
367  virtual void EndChoiceVariant(void) override;
368 
369  virtual void WriteBytes(const ByteBlock& block,
370  const char* bytes, size_t length) override;
371  virtual void WriteChars(const CharBlock& block,
372  const char* chars, size_t length) override;
373 
374  // Write current separator to the stream
375  virtual void WriteSeparator(void) override;
376 
377 #if defined(NCBI_SERIAL_IO_TRACE)
378  void TraceTag(const string& name);
379 #endif
380 
381 private:
382  void WriteBase64Bytes(const char* bytes, size_t length);
383  void WriteBytes(const char* bytes, size_t length);
384  void WriteString(const char* str, size_t length);
385 
386  void OpenTagStart(void);
387  void OpenTagEnd(void);
388  void OpenTagEndBack(void);
389  void SelfCloseTagEnd(void);
390  void CloseTagStart(void);
391  void CloseTagEnd(void);
392 
393  void WriteTag(const string& name);
394  void OpenTag(const string& name);
395  void EolIfEmptyTag(void);
396  void CloseTag(const string& name);
397  void OpenStackTag(size_t level);
398  void CloseStackTag(size_t level);
403  void PrintTagName(size_t level);
404  bool WillHaveName(TTypeInfo elementType);
405  string GetModuleName(TTypeInfo type);
406  bool x_IsStdXml(void) {return m_StdXml || m_EnforcedStdXml;}
407 
408  void x_WriteClassNamespace(TTypeInfo type);
409  bool x_ProcessTypeNamespace(TTypeInfo type);
410  void x_EndTypeNamespace(void);
411  bool x_BeginNamespace(const string& ns_name, const string& ns_prefix);
412  void x_EndNamespace(const string& ns_name);
413  bool x_SpecialCaseWrite(void);
414  char x_VerifyChar(char);
415 
416  enum ETagAction {
420  eAttlistTag
421  };
427  eSpecRefDTD
428  };
430  bool m_EndTag;
431 
432  // DTD file name and prefix
437  string m_PublicId;
438  static string sm_DefaultDTDFilePrefix;
439  bool m_Attlist;
440  bool m_StdXml;
451  deque<string> m_NsPrefixes;
454 };
455 
456 
457 /* @} */
458 
459 
461 
463 
464 #endif
#define false
Definition: bool.h:36
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:264
CDelayBuffer.
Definition: delaybuf.hpp:58
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStreamXml –.
Definition: objostrxml.hpp:54
CObjectOStream –.
Definition: objostr.hpp:83
CObjectStreamCopier –.
Definition: objcopy.hpp:71
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Definition: set.hpp:45
char value[7]
Definition: config.c:431
Include a standard set of the NCBI C++ Toolkit most basic headers.
static uch flags
std::ofstream out("events_result.xml")
main entry point for tests
#define NCBI_DEPRECATED_CTOR(decl)
Macro used to mark a constructor as deprecated.
Definition: ncbimisc.hpp:1209
int TEnumValueType
Definition: serialdef.hpp:232
const void * TConstObjectPtr
Definition: serialdef.hpp:59
EStringType
String type.
Definition: serialdef.hpp:185
unsigned int TSerial_Format_Flags
MSerial_Format –.
Definition: serialbase.hpp:571
@ eStringTypeVisible
VisibleString (in ASN.1 sense)
Definition: serialdef.hpp:186
void WriteString(const char *str, size_t length)
static string sm_DefaultDTDFilePrefix
Definition: objostrxml.hpp:438
virtual void EndClassMember(void)
Definition: objostr.cpp:888
MLIOVIR void WriteClassMemberSpecialCase(const CMemberId &memberId, TTypeInfo memberType, TConstObjectPtr memberPtr, ESpecialCaseWrite how)
Definition: objostr.cpp:580
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
void WriteClassMember(const CConstObjectInfoMI &member)
Definition: objostr.cpp:571
ERealValueFormat m_RealFmt
Definition: objostrxml.hpp:442
void SetDTDFilePrefix(const string &prefix)
Set DTD or schema file prefix.
virtual void WriteFileHeader(TTypeInfo type)
Definition: objostr.cpp:680
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objostr.cpp:1029
virtual void WriteUint8(Uint8 data)=0
bool GetEnforcedStdXml(void)
Get scope prefixes handling parameter.
Definition: objostrxml.hpp:126
virtual void CopyString(CObjectIStream &in, EStringType type=eStringTypeVisible)=0
virtual void WriteOther(TConstObjectPtr object, TTypeInfo typeInfo)
Definition: objostr.cpp:786
virtual void WriteInt8(Int8 data)=0
virtual void EndClass(void)
Definition: objostr.cpp:884
void CloseTag(TTypeInfo type)
virtual void WriteSeparator(void)
Definition: objostr.cpp:1181
void OpenStackTag(size_t level)
EEncoding m_Encoding
Definition: objostrxml.hpp:443
void DisableDTDPublicId(void)
Disable DTD public identifier.
map< string, string > m_NsNameToPrefix
Definition: objostrxml.hpp:449
virtual void WriteString(const string &str, EStringType type=eStringTypeVisible)=0
void CloseTag(const string &name)
virtual void WriteBitString(const CBitString &obj)=0
virtual void WriteNullPointer(void)=0
virtual void EndContainerElement(void)
Definition: objostr.cpp:806
static string GetDefaultDTDFilePrefix(void)
Get default (global) DTD file prefix.
EEncoding m_StringEncoding
Definition: objostrxml.hpp:444
map< string, string > m_NsPrefixToName
Definition: objostrxml.hpp:450
bool x_IsStdXml(void)
Definition: objostrxml.hpp:406
MLIOVIR void WriteContainer(const CContainerTypeInfo *containerType, TConstObjectPtr containerPtr)
Definition: objostr.cpp:810
virtual void WriteChar(char data)=0
virtual void CopyAnyContentObject(CObjectIStream &in)=0
virtual void BeginNamedType(TTypeInfo namedTypeInfo)
Definition: objostr.cpp:734
virtual void CopyBitString(CObjectIStream &in)=0
virtual void BeginContainer(const CContainerTypeInfo *containerType)=0
virtual void WriteStringStore(const string &data)=0
bool m_UseDefaultDTDFilePrefix
Definition: objostrxml.hpp:433
virtual void WriteOtherEnd(TTypeInfo typeInfo)
Definition: objostr.cpp:794
virtual void WriteInt4(Int4 data)=0
deque< string > m_NsPrefixes
Definition: objostrxml.hpp:451
string GetDefaultSchemaNamespace(void)
Get default value of namespace name of generated DTD documents.
void CloseTagIfNamed(TTypeInfo type)
void SetDTDPublicId(const string &publicId)
Set DTD public identifier.
ERealValueFormat
formatting of values of type 'real' ('double')
Definition: objostrxml.hpp:248
virtual void WriteUint4(Uint4 data)=0
virtual void WriteCString(const char *str)=0
virtual void WriteObjectReference(TObjectIndex index)=0
void OpenTagIfNamed(TTypeInfo type)
void OpenTag(TTypeInfo type)
virtual void BeginChoiceVariant(const CChoiceTypeInfo *choiceType, const CMemberId &id)=0
virtual string GetPosition(void) const override
Get current stream position as string.
Definition: objostr.cpp:510
virtual void WriteChars(const CharBlock &block, const char *chars, size_t length)=0
void EnableDTDPublicId(void)
Enable DTD public identifier.
MLIOVIR void CopyNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo, CObjectStreamCopier &copier)
Definition: objostr.cpp:761
virtual void EndOfWrite(void)
Definition: objostr.cpp:559
MLIOVIR void WriteClass(const CClassTypeInfo *objectType, TConstObjectPtr objectPtr)
Definition: objostr.cpp:892
virtual void CopyEnum(const CEnumeratedTypeValues &values, CObjectIStream &in)=0
void SetDTDFileName(const string &filename)
Set DTD or schema file name.
virtual void WriteBytes(const ByteBlock &block, const char *bytes, size_t length)=0
void CloseStackTag(size_t level)
virtual void CopyStringStore(CObjectIStream &in)=0
virtual void WriteFloat(float data)
Definition: objostr.cpp:721
virtual void EndChoice(void)
Definition: objostr.cpp:1032
string GetDTDFileName(void) const
Get DTD or schema file name.
string GetDTDFilePrefix(void) const
Get DTD or schema file prefix.
virtual void WriteDouble(double data)=0
void OpenTag(const string &name)
virtual void WriteNull(void)=0
MLIOVIR void WriteNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo, TConstObjectPtr object)
Definition: objostr.cpp:742
virtual void WriteAnyContentObject(const CAnyContentObject &obj)=0
EDataSpecRef m_SpecRef
Definition: objostrxml.hpp:429
ETagAction m_LastTagAction
Definition: objostrxml.hpp:422
virtual void EndNamedType(void)
Definition: objostr.cpp:738
string GetDTDPublicId(void) const
Get DTD public identifier.
virtual void SetFormattingFlags(TSerial_Format_Flags flags)
Set output formatting flags.
Definition: objostr.cpp:1193
virtual void BeginContainerElement(TTypeInfo elementType)
Definition: objostr.cpp:802
virtual void BeginClassMember(const CMemberId &id)=0
virtual void WriteOtherBegin(TTypeInfo typeInfo)=0
void SetDefaultSchemaNamespace(const string &schema_ns)
Set default value of namespace name of generated DTD documents.
virtual void WriteBool(bool data)=0
virtual void EndContainer(void)
Definition: objostr.cpp:798
virtual void EndChoiceVariant(void)
Definition: objostr.cpp:1035
static void SetDefaultDTDFilePrefix(const string &prefix)
Set default (global) DTD file prefix.
string m_DefaultSchemaNamespace
Definition: objostrxml.hpp:447
virtual void WriteEnum(const CEnumeratedTypeValues &values, TEnumValueType value)=0
@ eRealFixedFormat
use 'f' formatting type
Definition: objostrxml.hpp:249
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
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
EEncoding
Definition: ncbistr.hpp:199
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
static const BitmapCharRec *const chars[]
Definition: ncbi_10x20.c:1829
std::istream & in(std::istream &in_, double &x_)
static const char * prefix[]
Definition: pcregrep.c:405
static pcre_uint8 * buffer
Definition: pcretest.c:1051
static const string & GetModuleName(const char *moduleName)
Definition: serial.cpp:87
static const char * str(char *buf, int n)
Definition: stats.c:84
Definition: type.c:6
Modified on Sun Mar 03 03:16:48 2024 by modify_doxy.py rev. 669887