add --parser-option root-name, enable-comments, enable-whitespace, enable-line-numbers, enable-event-numbers, enable-sub-keys v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 30 Nov 2020 00:12:16 +0100
branchv_0
changeset 32 e72546725c77
parent 31 c6527b45fbc2
child 33 c9a158da6c32
add --parser-option root-name, enable-comments, enable-whitespace, enable-line-numbers, enable-event-numbers, enable-sub-keys
bash-completion.sh
src/XMLDocumentConstructor.h
src/lib/uri.h
--- a/bash-completion.sh	Sun Nov 29 10:49:33 2020 +0100
+++ b/bash-completion.sh	Mon Nov 30 00:12:16 2020 +0100
@@ -75,6 +75,12 @@
 		"unescape-backspace"
 		"tree-style"
 		"tree-with-namespaces"
+		"root-name"
+		"enable-whitespace"
+		"enable-comments"
+		"enable-sub-keys"
+		"enable-line-numbers"
+		"enable-event-numbers"
 	);
 
 	DIALECTS=(
@@ -118,6 +124,12 @@
 	elif [[ "$w2" == "--parser-option" && "$w1" == "quotes"                  && "x$w0" == "x" ]];    then COMPREPLY=("\$'\"\\''")
 	elif [[ "$w2" == "--parser-option" && "$w1" == "tree-style"                               ]];    then COMPREPLY=($(compgen -W "${TREE_STYLES[*]}" -- "$w0"))
 	elif [[ "$w2" == "--parser-option" && "$w1" == "tree-with-namespaces"                     ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--parser-option" && "$w1" == "root-name"               && "x$w0" == "x" ]];    then COMPREPLY=("'ini'")
+	elif [[ "$w2" == "--parser-option" && "$w1" == "enable-whitespace"                        ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--parser-option" && "$w1" == "enable-comments"                          ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--parser-option" && "$w1" == "enable-sub-keys"                          ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--parser-option" && "$w1" == "enable-line-numbers"                      ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w2" == "--parser-option" && "$w1" == "enable-event-numbers"                     ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
 
 	else
 		OPTIONS=(
--- a/src/XMLDocumentConstructor.h	Sun Nov 29 10:49:33 2020 +0100
+++ b/src/XMLDocumentConstructor.h	Mon Nov 30 00:12:16 2020 +0100
@@ -52,6 +52,13 @@
 
 	TreeStyle treeStyle = TreeStyle::Literal;
 
+	std::string rootName = "ini";
+	bool enableLineNumbers = true;
+	bool enableEventNumbers = true;
+	bool enableSubKeys = true;
+	bool enableComments = true;
+	bool enableWhitespace = true;
+
 	/**
 	 * TODO: use a common method
 	 */
@@ -73,7 +80,7 @@
 
 	void startDocument() override {
 		if (currentSection) throw std::out_of_range("Lunatic INI parser send us multiple documents.");
-		currentSection = domParser->get_document()->create_root_node("ini", treeWithNamespaces ? xml::XMLNS : "");
+		currentSection = domParser->get_document()->create_root_node(rootName, treeWithNamespaces ? xml::XMLNS : "");
 	};
 
 	void endDocument() override {
@@ -84,8 +91,8 @@
 		if (treeStyle == TreeStyle::Literal) currentSection->set_attribute("type", "section");
 		currentSection->set_attribute("name", event.name);
 		if (event.comment.size()) currentSection->set_attribute("comment", event.comment);
-		if (event.lineNumber >= 0) currentSection->set_attribute("line-number", std::to_string(event.lineNumber));
-		if (event.eventNumber >= 0) currentSection->set_attribute("event-number", std::to_string(event.eventNumber));
+		if (event.lineNumber >= 0 && enableLineNumbers) currentSection->set_attribute("line-number", std::to_string(event.lineNumber));
+		if (event.eventNumber >= 0 && enableEventNumbers) currentSection->set_attribute("event-number", std::to_string(event.eventNumber));
 	};
 
 	void endSection() override {
@@ -96,34 +103,40 @@
 	void entry(const EntryEvent& event) override {
 		xmlpp::Element* entry = currentSection->add_child(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.fullKey) : "entry");
 		if (treeStyle == TreeStyle::Literal) entry->set_attribute("type", "entry");
-		entry->set_attribute("key", event.key);
-		entry->set_attribute("full-key", event.fullKey);
-		if (event.subKey.size()) entry->set_attribute("sub-key", event.subKey);
+		entry->set_attribute("key", enableSubKeys ? event.key : event.fullKey);
+		if (enableSubKeys)entry->set_attribute("full-key", event.fullKey);
+		if (event.subKey.size() && enableSubKeys) entry->set_attribute("sub-key", event.subKey);
 		if (event.comment.size()) entry->set_attribute("comment", event.comment);
-		if (event.lineNumber >= 0) entry->set_attribute("line-number", std::to_string(event.lineNumber));
-		if (event.eventNumber >= 0) entry->set_attribute("event-number", std::to_string(event.eventNumber));
+		if (event.lineNumber >= 0 && enableLineNumbers) entry->set_attribute("line-number", std::to_string(event.lineNumber));
+		if (event.eventNumber >= 0 && enableEventNumbers) entry->set_attribute("event-number", std::to_string(event.eventNumber));
 		entry->add_child_text(event.value);
 	};
 
 	void comment(const CommentEvent& event) override {
 		xmlpp::Element* comment = currentSection->add_child("comment");
 		if (treeStyle == TreeStyle::Literal) comment->set_attribute("type", "comment");
-		if (event.lineNumber >= 0) comment->set_attribute("line-number", std::to_string(event.lineNumber));
-		if (event.eventNumber >= 0) comment->set_attribute("event-number", std::to_string(event.eventNumber));
+		if (event.lineNumber >= 0 && enableLineNumbers) comment->set_attribute("line-number", std::to_string(event.lineNumber));
+		if (event.eventNumber >= 0 && enableEventNumbers) comment->set_attribute("event-number", std::to_string(event.eventNumber));
 		comment->add_child_text(event.comment);
 	}
 
 	void whitespace(const WhitespaceEvent& event) override {
 		xmlpp::Element* whitespace = currentSection->add_child("whitespace");
 		if (treeStyle == TreeStyle::Literal) whitespace->set_attribute("type", "whitespace");
-		if (event.lineNumber >= 0) whitespace->set_attribute("line-number", std::to_string(event.lineNumber));
-		if (event.eventNumber >= 0) whitespace->set_attribute("event-number", std::to_string(event.eventNumber));
+		if (event.lineNumber >= 0 && enableLineNumbers) whitespace->set_attribute("line-number", std::to_string(event.lineNumber));
+		if (event.eventNumber >= 0 && enableEventNumbers) whitespace->set_attribute("event-number", std::to_string(event.eventNumber));
 		whitespace->add_child_text(event.whitespace);
 	}
 
 	bool setOption(const std::string& uri, const std::string& value) {
 		if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value);
 		else if (uri == xml::TreeStyle) treeStyle = parseTreeStyle(value);
+		else if (uri == xml::RootName) rootName = value;
+		else if (uri == xml::EnableWhitespace) enableWhitespace = parseBoolean(value);
+		else if (uri == xml::EnableComments) enableComments = parseBoolean(value);
+		else if (uri == xml::EnableSubKeys) enableSubKeys = parseBoolean(value);
+		else if (uri == xml::EnableLineNumbers) enableLineNumbers = parseBoolean(value);
+		else if (uri == xml::EnableEventNumbers) enableEventNumbers = parseBoolean(value);
 		else return false;
 		return true;
 	}
--- a/src/lib/uri.h	Sun Nov 29 10:49:33 2020 +0100
+++ b/src/lib/uri.h	Mon Nov 30 00:12:16 2020 +0100
@@ -23,6 +23,7 @@
 
 // TODO: these strings will become globally unique URIs (or IRIs) after moving to alt2xml and relative/unprefixed names should also work
 
+/** general options of the INI parser */
 namespace option {
 static const char* TrimContinuingLines = "trim-continuing-lines";
 static const char* AllowSections = "allow-sections";
@@ -34,19 +35,29 @@
 static const char* Dialect = "dialect";
 }
 
+/** names of dynamically registered unescaping processors; they are also options */
 namespace unescaping {
 static const char* Basic = "unescape-basic";
 static const char* JavaProperties = "unescape-java-properties";
 static const char* Backspace = "unescape-backspace";
 }
 
+/** not options but a values of the dialect option */
 namespace dialect {
 static const char* JavaProperties = "java-properties";
 }
 
+/** options for configuring the stage where events from the INI parser are converted to SAX events or DOM building */
 namespace xml {
 static const char* TreeWithNamespaces = "tree-with-namespaces";
 static const char* TreeStyle = "tree-style";
+static const char* RootName = "root-name";
+static const char* EnableComments = "enable-comments";
+static const char* EnableWhitespace = "enable-whitespace";
+static const char* EnableLineNumbers = "enable-line-numbers";
+static const char* EnableEventNumbers = "enable-event-numbers";
+static const char* EnableSubKeys = "enable-sub-keys";
+// static const char* EnableSections = "enable-sections"; // TODO: flat XML without sections?
 
 static const char* XMLNS = "tag:globalcode.info,2018:alt2xml:TEMPORARY:ini"; // not an option and might change, just preliminary namespace
 }