Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
WriterUtils.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_XML_WriterUtils_h_
5#define _Stroika_Foundation_DataExchange_XML_WriterUtils_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10
16
17/*
18 * TODO:
19 * @todo Short term- convert to using only Stroika string class (but make easy to still leverage from HF -
20 * think this out carefully befor change)
21 *
22 * @todo Harmonize the WriteQuoted/Format4XML routines - probably just switching to the later.
23 * Maybe add optional params for some types (control enums).
24 * Support dates, and VariantValue, and optional, etc...
25 */
26
27namespace Stroika::Foundation::DataExchange::XML {
28
29 using Characters::String;
30
31 /**
32 * \brief Whether s is an NCName (https://www.w3.org/TR/xml-names/#NT-NCName) - an XML name with no ':', as a local name and
33 * a namespace prefix must each be
34 */
35 bool IsNCName (const String& s);
36
37 /**
38 * \brief s as an attribute's value, in ASCII: each character spelled so a parser reads it back as that character
39 *
40 * So a character not ASCII is a reference to its code point (one, outside the BMP too), and so is a TAB, LF or CR, which a
41 * parser would otherwise read as a space (attribute-value normalization). Throws DataExchange::BadFormatException on a
42 * character XML 1.0 cannot hold at all, not even as a reference (U+0000 to U+001F but TAB, LF and CR, U+FFFE, U+FFFF).
43 *
44 * \note The string overload passes s's bytes through, quoting only & < > " and ' - an encoding it cannot know.
45 */
46 string QuoteForXMLAttribute (const string& s);
47 string QuoteForXMLAttribute (const wstring& s); ///< \brief s as an attribute's value, in ASCII, each character as a parser reads it back
48 string QuoteForXMLAttribute (const String& s); ///< \brief s as an attribute's value, in ASCII, each character as a parser reads it back
49 string QuoteForXMLAttribute (const optional<String>& s); ///< \brief s as an attribute's value - nothing, where s is missing
50
51 /**
52 */
53 String QuoteForXMLAttributeW (const String& s);
54
55 /**
56 * \brief s as an element's text, in ASCII (so assuming nothing of the encoding the XML is written in): each character
57 * spelled so a parser reads it back as that character
58 *
59 * So a character not ASCII is a reference to its code point (one, outside the BMP too), and so is a CR, which a parser
60 * would otherwise read as LF (line-end normalization). Throws DataExchange::BadFormatException on a character XML 1.0
61 * cannot hold at all, not even as a reference (U+0000 to U+001F but TAB, LF and CR, U+FFFE, U+FFFF).
62 *
63 * \note The string overload passes s's bytes through, quoting only & < > and dashes - an encoding it cannot know.
64 */
65 string QuoteForXML (const string& s);
66 string QuoteForXML (const wstring& s); ///< \brief s as an element's text, in ASCII, each character as a parser reads it back
67 string QuoteForXML (const String& s); ///< \brief s as an element's text, in ASCII, each character as a parser reads it back
68 string QuoteForXML (const optional<String>& s); ///< \brief s as an element's text - nothing, where s is missing
69
70 /**
71 */
72 String QuoteForXMLW (const String& s);
73
74 class Indenter {
75 public:
76 Indenter (const String& indentText = "\t"sv);
77
78 public:
79 nonvirtual void Indent (unsigned int indentLevel, ostream& out) const;
80 nonvirtual void Indent (unsigned int indentLevel, wostream& out) const;
81
82 private:
83 string fTabS_;
84 wstring fTabW_;
85 };
86
87 /**
88 * Format values for XML output.
89 * PROBABLY should map QuoteForXML to this overloaded name?
90 *
91 * Note - this assumes we're outputting using UTF8.
92 *
93 * SHOULD probably include Memory::Optiona<> variations on all these as well - so we can fairly freely just say "Foramt4XML"
94 */
95 string Format4XML (bool v);
96 //string Format4XML (Time::Date v); NYI
97 //string Format4XML (Time::DateTime v);
98
99}
100
101/*
102 ********************************************************************************
103 ***************************** Implementation Details ***************************
104 ********************************************************************************
105 */
106#include "WriterUtils.inl"
107
108#endif /*_Stroika_Foundation_DataExchange_XML_WriterUtils_h_*/