src/XMLTableCommand.h
branchv_0
changeset 2 0d3eb5129582
parent 0 a37196931f63
child 4 a0689654b3c2
--- a/src/XMLTableCommand.h	Wed Jul 24 15:53:42 2019 +0200
+++ b/src/XMLTableCommand.h	Wed Jul 24 21:29:56 2019 +0200
@@ -28,6 +28,7 @@
 
 #include <relpipe/writer/typedefs.h>
 
+#include "Configuration.h"
 
 namespace relpipe {
 namespace in {
@@ -37,29 +38,36 @@
 
 class XMLCommand {
 private:
+	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 
 public:
 
-	void process(std::istream& input, std::ostream& output) {
-
+	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
+		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
 
 		xmlpp::DomParser parser;
 		parser.parse_stream(input);
-		xmlpp::Document* d = parser.get_document();
-		xmlpp::Element* r = d->get_root_node();
-
-		xmlpp::Node::PrefixNsMap m;
-		m["rp"] = "tag:globalcode.info,2018:relpipe";
+		xmlpp::Element* root = parser.get_document()->get_root_node();
 
-		output << "root: " << r->get_name() << std::endl;
-
-		for (xmlpp::Node* n : r->find("//rp:attribute", m)) {
-
-			output << "node:" << n->get_name() << std::endl;
-
+		xmlpp::Node::PrefixNsMap ns;
+		for (int i = 0; i < configuration.namespaceMappings.size(); i++) {
+			std::string prefix = convertor.to_bytes(configuration.namespaceMappings[i]);
+			std::string uri = convertor.to_bytes(configuration.namespaceMappings[++i]);
+			ns[prefix] = uri;
 		}
 
-
+		for (const RelationConfiguration& r : configuration.relationConfigurations) {
+			std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
+			for (AttributeRecipe a : r.attributes) attributesMetadata.push_back(AttributeMetadata{a.name, a.type});
+			relpipe::writer::string_t name = r.nameIsXPath ? convertor.from_bytes(root->eval_to_string(convertor.to_bytes(r.relation), ns)) : r.relation;
+			writer->startRelation(name, attributesMetadata, true);
+			for (xmlpp::Node* n : root->find(convertor.to_bytes(r.xpath), ns)) {
+				for (AttributeRecipe a : r.attributes) {
+					// TODO: convert to bytes only once
+					writer->writeAttribute(convertor.from_bytes(n->eval_to_string(convertor.to_bytes(a.xpath), ns)));
+				}
+			}
+		}
 	}
 };