src/XMLTableCommand.h
branchv_0
changeset 2 0d3eb5129582
parent 0 a37196931f63
child 4 a0689654b3c2
equal deleted inserted replaced
1:7d6ac51c0d48 2:0d3eb5129582
    26 
    26 
    27 #include <libxml++-2.6/libxml++/libxml++.h>
    27 #include <libxml++-2.6/libxml++/libxml++.h>
    28 
    28 
    29 #include <relpipe/writer/typedefs.h>
    29 #include <relpipe/writer/typedefs.h>
    30 
    30 
       
    31 #include "Configuration.h"
    31 
    32 
    32 namespace relpipe {
    33 namespace relpipe {
    33 namespace in {
    34 namespace in {
    34 namespace xmltable {
    35 namespace xmltable {
    35 
    36 
    36 using namespace relpipe::writer;
    37 using namespace relpipe::writer;
    37 
    38 
    38 class XMLCommand {
    39 class XMLCommand {
    39 private:
    40 private:
       
    41 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    40 
    42 
    41 public:
    43 public:
    42 
    44 
    43 	void process(std::istream& input, std::ostream& output) {
    45 	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
    44 
    46 		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
    45 
    47 
    46 		xmlpp::DomParser parser;
    48 		xmlpp::DomParser parser;
    47 		parser.parse_stream(input);
    49 		parser.parse_stream(input);
    48 		xmlpp::Document* d = parser.get_document();
    50 		xmlpp::Element* root = parser.get_document()->get_root_node();
    49 		xmlpp::Element* r = d->get_root_node();
       
    50 
    51 
    51 		xmlpp::Node::PrefixNsMap m;
    52 		xmlpp::Node::PrefixNsMap ns;
    52 		m["rp"] = "tag:globalcode.info,2018:relpipe";
    53 		for (int i = 0; i < configuration.namespaceMappings.size(); i++) {
    53 
    54 			std::string prefix = convertor.to_bytes(configuration.namespaceMappings[i]);
    54 		output << "root: " << r->get_name() << std::endl;
    55 			std::string uri = convertor.to_bytes(configuration.namespaceMappings[++i]);
    55 
    56 			ns[prefix] = uri;
    56 		for (xmlpp::Node* n : r->find("//rp:attribute", m)) {
       
    57 
       
    58 			output << "node:" << n->get_name() << std::endl;
       
    59 
       
    60 		}
    57 		}
    61 
    58 
    62 
    59 		for (const RelationConfiguration& r : configuration.relationConfigurations) {
       
    60 			std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
       
    61 			for (AttributeRecipe a : r.attributes) attributesMetadata.push_back(AttributeMetadata{a.name, a.type});
       
    62 			relpipe::writer::string_t name = r.nameIsXPath ? convertor.from_bytes(root->eval_to_string(convertor.to_bytes(r.relation), ns)) : r.relation;
       
    63 			writer->startRelation(name, attributesMetadata, true);
       
    64 			for (xmlpp::Node* n : root->find(convertor.to_bytes(r.xpath), ns)) {
       
    65 				for (AttributeRecipe a : r.attributes) {
       
    66 					// TODO: convert to bytes only once
       
    67 					writer->writeAttribute(convertor.from_bytes(n->eval_to_string(convertor.to_bytes(a.xpath), ns)));
       
    68 				}
       
    69 			}
       
    70 		}
    63 	}
    71 	}
    64 };
    72 };
    65 
    73 
    66 }
    74 }
    67 }
    75 }