src/XMLDocumentConstructor.h
branchv_0
changeset 21 053054f9f702
parent 19 aabdab3e05a0
child 24 ee72fccc5267
equal deleted inserted replaced
20:e67584a06be6 21:053054f9f702
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    16  */
    17 #pragma once
    17 #pragma once
    18 
    18 
    19 namespace relpipe {
       
    20 namespace in {
       
    21 namespace xmltable {
       
    22 
       
    23 #include <codecvt>
    19 #include <codecvt>
    24 #include <vector>
    20 #include <vector>
    25 
    21 
    26 #include <libxml++-2.6/libxml++/libxml++.h>
    22 #include <libxml++-2.6/libxml++/libxml++.h>
    27 #include <yaml.h>
    23 #include <yaml.h>
    28 
    24 
       
    25 #include "XMLNameCodec.h"
       
    26 
       
    27 namespace relpipe {
       
    28 namespace in {
       
    29 namespace xmltable {
       
    30 
    29 class XMLDocumentConstructor {
    31 class XMLDocumentConstructor {
    30 private:
    32 private:
    31 	std::istream* input = nullptr;
    33 	std::istream* input = nullptr;
    32 	xmlpp::DomParser* parser = nullptr;
    34 	xmlpp::DomParser* parser = nullptr;
    33 	yaml_parser_t yamlParser;
    35 	yaml_parser_t yamlParser;
       
    36 	XMLNameCodec nameCodec;
    34 
    37 
    35 	enum class Mode {
    38 	enum class Mode {
    36 		ROOT,
    39 		ROOT,
    37 		SEQUENCE,
    40 		SEQUENCE,
    38 		MAPPING,
    41 		MAPPING,
    46 		std::istream* input = ((XMLDocumentConstructor*) instance)->input;
    49 		std::istream* input = ((XMLDocumentConstructor*) instance)->input;
    47 		input->read((char*) buffer, size);
    50 		input->read((char*) buffer, size);
    48 		*length = input->gcount();
    51 		*length = input->gcount();
    49 		return (input->good() || input->eof()) ? 1 : 0;
    52 		return (input->good() || input->eof()) ? 1 : 0;
    50 	}
    53 	}
    51 
    54 	
    52 	/**
    55 	/**
    53 	 * Both YAML and XML strings are in UTF-8.
    56 	 * Both YAML and XML strings are in UTF-8.
    54 	 */
    57 	 */
    55 	const char* y2x(yaml_char_t* value) {
    58 	const char* y2x(yaml_char_t* value) {
    56 		return value ? (const char*) value : "";
    59 		return value ? (const char*) value : "";
    57 	}
    60 	}
    58 
    61 
    59 	const std::string y2xname(yaml_char_t* value) {
    62 	const Glib::ustring y2xname(yaml_char_t* value) {
    60 		// FIXME: escaping, assure valid XML names
    63 		return nameCodec.encode(y2x(value));
    61 		//return std::string("name_") + y2x(value);
       
    62 		return y2x(value);
       
    63 	}
    64 	}
    64 
    65 
    65 	xmlpp::Element* parentOrSelf(xmlpp::Element* current) {
    66 	xmlpp::Element* parentOrSelf(xmlpp::Element* current) {
    66 		return current->get_parent() == nullptr ? current : current->get_parent();
    67 		return current->get_parent() == nullptr ? current : current->get_parent();
    67 	}
    68 	}