71 virtual ~HierarchicalINIContentHandler() { |
78 virtual ~HierarchicalINIContentHandler() { |
72 } |
79 } |
73 |
80 |
74 void startDocument() override { |
81 void startDocument() override { |
75 if (currentSection) throw std::out_of_range("Lunatic INI parser send us multiple documents."); |
82 if (currentSection) throw std::out_of_range("Lunatic INI parser send us multiple documents."); |
76 currentSection = domParser->get_document()->create_root_node("ini", treeWithNamespaces ? xml::XMLNS : ""); |
83 currentSection = domParser->get_document()->create_root_node(rootName, treeWithNamespaces ? xml::XMLNS : ""); |
77 }; |
84 }; |
78 |
85 |
79 void endDocument() override { |
86 void endDocument() override { |
80 }; |
87 }; |
81 |
88 |
82 void startSection(const SectionStartEvent& event) override { |
89 void startSection(const SectionStartEvent& event) override { |
83 currentSection = currentSection->add_child(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.name) : "section"); |
90 currentSection = currentSection->add_child(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.name) : "section"); |
84 if (treeStyle == TreeStyle::Literal) currentSection->set_attribute("type", "section"); |
91 if (treeStyle == TreeStyle::Literal) currentSection->set_attribute("type", "section"); |
85 currentSection->set_attribute("name", event.name); |
92 currentSection->set_attribute("name", event.name); |
86 if (event.comment.size()) currentSection->set_attribute("comment", event.comment); |
93 if (event.comment.size()) currentSection->set_attribute("comment", event.comment); |
87 if (event.lineNumber >= 0) currentSection->set_attribute("line-number", std::to_string(event.lineNumber)); |
94 if (event.lineNumber >= 0 && enableLineNumbers) currentSection->set_attribute("line-number", std::to_string(event.lineNumber)); |
88 if (event.eventNumber >= 0) currentSection->set_attribute("event-number", std::to_string(event.eventNumber)); |
95 if (event.eventNumber >= 0 && enableEventNumbers) currentSection->set_attribute("event-number", std::to_string(event.eventNumber)); |
89 }; |
96 }; |
90 |
97 |
91 void endSection() override { |
98 void endSection() override { |
92 currentSection = currentSection->get_parent(); |
99 currentSection = currentSection->get_parent(); |
93 if (currentSection == nullptr) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before."); |
100 if (currentSection == nullptr) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before."); |
94 }; |
101 }; |
95 |
102 |
96 void entry(const EntryEvent& event) override { |
103 void entry(const EntryEvent& event) override { |
97 xmlpp::Element* entry = currentSection->add_child(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.fullKey) : "entry"); |
104 xmlpp::Element* entry = currentSection->add_child(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.fullKey) : "entry"); |
98 if (treeStyle == TreeStyle::Literal) entry->set_attribute("type", "entry"); |
105 if (treeStyle == TreeStyle::Literal) entry->set_attribute("type", "entry"); |
99 entry->set_attribute("key", event.key); |
106 entry->set_attribute("key", enableSubKeys ? event.key : event.fullKey); |
100 entry->set_attribute("full-key", event.fullKey); |
107 if (enableSubKeys)entry->set_attribute("full-key", event.fullKey); |
101 if (event.subKey.size()) entry->set_attribute("sub-key", event.subKey); |
108 if (event.subKey.size() && enableSubKeys) entry->set_attribute("sub-key", event.subKey); |
102 if (event.comment.size()) entry->set_attribute("comment", event.comment); |
109 if (event.comment.size()) entry->set_attribute("comment", event.comment); |
103 if (event.lineNumber >= 0) entry->set_attribute("line-number", std::to_string(event.lineNumber)); |
110 if (event.lineNumber >= 0 && enableLineNumbers) entry->set_attribute("line-number", std::to_string(event.lineNumber)); |
104 if (event.eventNumber >= 0) entry->set_attribute("event-number", std::to_string(event.eventNumber)); |
111 if (event.eventNumber >= 0 && enableEventNumbers) entry->set_attribute("event-number", std::to_string(event.eventNumber)); |
105 entry->add_child_text(event.value); |
112 entry->add_child_text(event.value); |
106 }; |
113 }; |
107 |
114 |
108 void comment(const CommentEvent& event) override { |
115 void comment(const CommentEvent& event) override { |
109 xmlpp::Element* comment = currentSection->add_child("comment"); |
116 xmlpp::Element* comment = currentSection->add_child("comment"); |
110 if (treeStyle == TreeStyle::Literal) comment->set_attribute("type", "comment"); |
117 if (treeStyle == TreeStyle::Literal) comment->set_attribute("type", "comment"); |
111 if (event.lineNumber >= 0) comment->set_attribute("line-number", std::to_string(event.lineNumber)); |
118 if (event.lineNumber >= 0 && enableLineNumbers) comment->set_attribute("line-number", std::to_string(event.lineNumber)); |
112 if (event.eventNumber >= 0) comment->set_attribute("event-number", std::to_string(event.eventNumber)); |
119 if (event.eventNumber >= 0 && enableEventNumbers) comment->set_attribute("event-number", std::to_string(event.eventNumber)); |
113 comment->add_child_text(event.comment); |
120 comment->add_child_text(event.comment); |
114 } |
121 } |
115 |
122 |
116 void whitespace(const WhitespaceEvent& event) override { |
123 void whitespace(const WhitespaceEvent& event) override { |
117 xmlpp::Element* whitespace = currentSection->add_child("whitespace"); |
124 xmlpp::Element* whitespace = currentSection->add_child("whitespace"); |
118 if (treeStyle == TreeStyle::Literal) whitespace->set_attribute("type", "whitespace"); |
125 if (treeStyle == TreeStyle::Literal) whitespace->set_attribute("type", "whitespace"); |
119 if (event.lineNumber >= 0) whitespace->set_attribute("line-number", std::to_string(event.lineNumber)); |
126 if (event.lineNumber >= 0 && enableLineNumbers) whitespace->set_attribute("line-number", std::to_string(event.lineNumber)); |
120 if (event.eventNumber >= 0) whitespace->set_attribute("event-number", std::to_string(event.eventNumber)); |
127 if (event.eventNumber >= 0 && enableEventNumbers) whitespace->set_attribute("event-number", std::to_string(event.eventNumber)); |
121 whitespace->add_child_text(event.whitespace); |
128 whitespace->add_child_text(event.whitespace); |
122 } |
129 } |
123 |
130 |
124 bool setOption(const std::string& uri, const std::string& value) { |
131 bool setOption(const std::string& uri, const std::string& value) { |
125 if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value); |
132 if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value); |
126 else if (uri == xml::TreeStyle) treeStyle = parseTreeStyle(value); |
133 else if (uri == xml::TreeStyle) treeStyle = parseTreeStyle(value); |
|
134 else if (uri == xml::RootName) rootName = value; |
|
135 else if (uri == xml::EnableWhitespace) enableWhitespace = parseBoolean(value); |
|
136 else if (uri == xml::EnableComments) enableComments = parseBoolean(value); |
|
137 else if (uri == xml::EnableSubKeys) enableSubKeys = parseBoolean(value); |
|
138 else if (uri == xml::EnableLineNumbers) enableLineNumbers = parseBoolean(value); |
|
139 else if (uri == xml::EnableEventNumbers) enableEventNumbers = parseBoolean(value); |
127 else return false; |
140 else return false; |
128 return true; |
141 return true; |
129 } |
142 } |
130 |
143 |
131 }; |
144 }; |