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