|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <sstream> |
|
20 #include <iomanip> |
|
21 #include <stdexcept> |
|
22 |
|
23 #include <glibmm-2.4/glibmm/ustring.h> |
|
24 |
|
25 namespace relpipe { |
|
26 namespace in { |
|
27 namespace xmltable { |
|
28 |
|
29 class XMLNameCodec { |
|
30 private: |
|
31 static const char DEFAULT_ESCAPING_CHARACTER = '_'; |
|
32 const char esc; |
|
33 const bool namespaceAware; |
|
34 |
|
35 bool between(gunichar codepoint, gunichar start, gunichar end) { |
|
36 return codepoint >= start && codepoint <= end; |
|
37 } |
|
38 |
|
39 /** |
|
40 * https://www.w3.org/TR/REC-xml/#NT-NameStartChar |
|
41 * |
|
42 * @param codepoint unicode character |
|
43 * @return whether this character is allowed at the beginning of a XML name |
|
44 */ |
|
45 bool isValidNameStartChar(gunichar codepoint) { |
|
46 // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] |
|
47 // | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] |
|
48 // | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
|
49 // | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] |
|
50 return (codepoint == ':' && !namespaceAware) || between(codepoint, 'A', 'Z') || codepoint == '_' || between(codepoint, 'a', 'z') |
|
51 || between(codepoint, 0xC0, 0xD6) || between(codepoint, 0xD8, 0xF6) || between(codepoint, 0xF8, 0x2FF) || between(codepoint, 0x370, 0x37D) || between(codepoint, 0x37F, 0x1FFF) |
|
52 || between(codepoint, 0x200C, 0x200D) || between(codepoint, 0x2070, 0x218F) || between(codepoint, 0x2C00, 0x2FEF) || between(codepoint, 0x3001, 0xD7FF) |
|
53 || between(codepoint, 0xF900, 0xFDCF) || between(codepoint, 0xFDF0, 0xFFFD) || between(codepoint, 0x10000, 0xEFFFF); |
|
54 } |
|
55 |
|
56 /** |
|
57 * https://www.w3.org/TR/REC-xml/#NT-NameChar |
|
58 * |
|
59 * @param codepoint unicode character |
|
60 * @return whether this character is allowed in a XML name |
|
61 */ |
|
62 bool isValidNameChar(gunichar codepoint) { |
|
63 // NameChar ::= NameStartChar | "-" | "." | [0-9] |
|
64 // | #xB7 |
|
65 // | [#x0300-#x036F] | [#x203F-#x2040] |
|
66 return isValidNameStartChar(codepoint) || codepoint == '-' || codepoint == '.' || between(codepoint, '0', '9') |
|
67 || codepoint == 0xB7 |
|
68 || between(codepoint, 0x0300, 0x036F) || between(codepoint, 0x203F, 0x2040); |
|
69 } |
|
70 |
|
71 public: |
|
72 |
|
73 XMLNameCodec() : XMLNameCodec(DEFAULT_ESCAPING_CHARACTER, true) { |
|
74 } |
|
75 |
|
76 /** |
|
77 * @param escapingCharacter must be valid character allowed not only in the middle of the XML name but also as the |
|
78 * first character of the name |
|
79 * @param namespaceAware colon character is reserved as a separator of the prefix and the local name, see |
|
80 * https://www.w3.org/TR/REC-xml-names/#NT-NCName |
|
81 * @throws std::invalid_argument if escapingCharacter is not valid |
|
82 */ |
|
83 XMLNameCodec(const char escapingCharacter, const bool namespaceAware) : esc(escapingCharacter), namespaceAware(namespaceAware) { |
|
84 // TODO: allow also characters like #xB7 and add another escaping if they occur at the beginning of the name? |
|
85 if (!isValidNameStartChar(esc)) { |
|
86 throw std::invalid_argument("The character „" + std::to_string(escapingCharacter) + "“ is not allowed at the beginning of a XML name and thus not usable for escaping"); |
|
87 } |
|
88 } |
|
89 |
|
90 virtual ~XMLNameCodec() { |
|
91 } |
|
92 |
|
93 /** |
|
94 * @param name any string |
|
95 * @return valid name of XML element or attribute |
|
96 */ |
|
97 Glib::ustring encode(Glib::ustring name) { |
|
98 if (name.empty()) { |
|
99 return Glib::ustring(1, esc); |
|
100 } else { |
|
101 std::stringstream result; |
|
102 |
|
103 for (int i = 0; i < name.size(); i++) { |
|
104 gunichar codepoint = name[i]; |
|
105 if (codepoint == esc) { |
|
106 result.put(esc); |
|
107 result.put(esc); |
|
108 continue; |
|
109 } else if ((i == 0 && isValidNameStartChar(codepoint)) || (i > 0 && isValidNameChar(codepoint))) { |
|
110 result << Glib::ustring(1, codepoint); |
|
111 continue; |
|
112 } |
|
113 |
|
114 result.put(esc); |
|
115 result << Glib::ustring::format(std::hex, std::setfill(L'0'), std::setw(2), codepoint); |
|
116 result.put(esc); |
|
117 } |
|
118 |
|
119 return result.str(); |
|
120 } |
|
121 } |
|
122 |
|
123 }; |
|
124 |
|
125 } |
|
126 } |
|
127 } |