src/XMLCommand.h
branchv_0
changeset 6 be83e0f457a8
parent 5 e5cf88ce91ac
child 7 85741b08db48
equal deleted inserted replaced
5:e5cf88ce91ac 6:be83e0f457a8
    30 #include <xercesc/util/XMLString.hpp>
    30 #include <xercesc/util/XMLString.hpp>
    31 
    31 
    32 #include <relpipe/writer/typedefs.h>
    32 #include <relpipe/writer/typedefs.h>
    33 
    33 
    34 #include "StreamInputSource.h"
    34 #include "StreamInputSource.h"
       
    35 #include "XercesStringConvertor.h"
    35 
    36 
    36 namespace relpipe {
    37 namespace relpipe {
    37 namespace in {
    38 namespace in {
    38 namespace xml {
    39 namespace xml {
    39 
    40 
    44 private:
    45 private:
    45 
    46 
    46 	class RelpipeSaxHandler : public xercesc::DefaultHandler {
    47 	class RelpipeSaxHandler : public xercesc::DefaultHandler {
    47 	private:
    48 	private:
    48 		unique_ptr<RelationalWriter> writer;
    49 		unique_ptr<RelationalWriter> writer;
    49 
    50 		XercesStringConvertor xConvertor;
    50 		std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    51 
       
    52 		string_t toString(const XMLCh * const chars) {
       
    53 			// XMLCh = char16_t
       
    54 			// „All XML data is handled within Xerces-C++ as strings of XMLCh characters. Regardless of the size of the type chosen, the data stored in variables of type XMLCh will always be utf-16 encoded values.“
       
    55 			// see https://xerces.apache.org/xerces-c/program-others-3.html
       
    56 			// other solution (depends on boost): https://flylib.com/books/en/2.131.1/working_with_xerces_strings.html
       
    57 
       
    58 			// TODO: review this text conversion and test on various platforms
       
    59 			char* x = XMLString::transcode(chars);
       
    60 			string s = string(x);
       
    61 			XMLString::release(&x);
       
    62 			return convertor.from_bytes(s);
       
    63 		}
       
    64 
    51 
    65 	public:
    52 	public:
    66 
    53 
    67 		RelpipeSaxHandler(std::ostream& output) : DefaultHandler(), writer(Factory::create(output)) {
    54 		RelpipeSaxHandler(std::ostream& output) : DefaultHandler(), writer(Factory::create(output)) {
    68 		}
    55 		}
    79 			}, true);
    66 			}, true);
    80 		}
    67 		}
    81 
    68 
    82 		void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const Attributes& attrs) override {
    69 		void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const Attributes& attrs) override {
    83 			writer->writeAttribute(L"startElement");
    70 			writer->writeAttribute(L"startElement");
    84 			writer->writeAttribute(toString(uri));
    71 			writer->writeAttribute(xConvertor.toString(uri));
    85 			writer->writeAttribute(toString(localname));
    72 			writer->writeAttribute(xConvertor.toString(localname));
    86 			writer->writeAttribute(toString(qname));
    73 			writer->writeAttribute(xConvertor.toString(qname));
    87 			writer->writeAttribute(L"");
    74 			writer->writeAttribute(L"");
    88 
    75 
    89 			for (int i = 0; i < attrs.getLength(); i++) {
    76 			for (int i = 0; i < attrs.getLength(); i++) {
    90 				writer->writeAttribute(L"attribute");
    77 				writer->writeAttribute(L"attribute");
    91 				writer->writeAttribute(toString(attrs.getURI(i)));
    78 				writer->writeAttribute(xConvertor.toString(attrs.getURI(i)));
    92 				writer->writeAttribute(toString(attrs.getLocalName(i)));
    79 				writer->writeAttribute(xConvertor.toString(attrs.getLocalName(i)));
    93 				writer->writeAttribute(toString(attrs.getQName(i)));
    80 				writer->writeAttribute(xConvertor.toString(attrs.getQName(i)));
    94 				writer->writeAttribute(toString(attrs.getValue(i)));
    81 				writer->writeAttribute(xConvertor.toString(attrs.getValue(i)));
    95 			}
    82 			}
    96 		}
    83 		}
    97 
    84 
    98 		void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname) override {
    85 		void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname) override {
    99 			writer->writeAttribute(L"endElement");
    86 			writer->writeAttribute(L"endElement");
   100 			writer->writeAttribute(toString(uri));
    87 			writer->writeAttribute(xConvertor.toString(uri));
   101 			writer->writeAttribute(toString(localname));
    88 			writer->writeAttribute(xConvertor.toString(localname));
   102 			writer->writeAttribute(toString(qname));
    89 			writer->writeAttribute(xConvertor.toString(qname));
   103 			writer->writeAttribute(L"");
    90 			writer->writeAttribute(L"");
   104 		}
    91 		}
   105 
    92 
   106 		void characters(const XMLCh * const chars, const XMLSize_t length) override {
    93 		void characters(const XMLCh * const chars, const XMLSize_t length) override {
   107 			writer->writeAttribute(L"characters");
    94 			writer->writeAttribute(L"characters");
   108 			writer->writeAttribute(L"");
    95 			writer->writeAttribute(L"");
   109 			writer->writeAttribute(L"");
    96 			writer->writeAttribute(L"");
   110 			writer->writeAttribute(L"");
    97 			writer->writeAttribute(L"");
   111 			writer->writeAttribute(toString(chars));
    98 			writer->writeAttribute(xConvertor.toString(chars));
   112 		}
    99 		}
   113 
   100 
   114 		void comment(const XMLCh * const chars, const XMLSize_t length) override {
   101 		void comment(const XMLCh * const chars, const XMLSize_t length) override {
   115 			writer->writeAttribute(L"comment");
   102 			writer->writeAttribute(L"comment");
   116 			writer->writeAttribute(L"");
   103 			writer->writeAttribute(L"");
   117 			writer->writeAttribute(L"");
   104 			writer->writeAttribute(L"");
   118 			writer->writeAttribute(L"");
   105 			writer->writeAttribute(L"");
   119 			writer->writeAttribute(toString(chars));
   106 			writer->writeAttribute(xConvertor.toString(chars));
   120 		}
   107 		}
   121 
   108 
   122 		void startCDATA() override {
   109 		void startCDATA() override {
   123 			writer->writeAttribute(L"startCDATA");
   110 			writer->writeAttribute(L"startCDATA");
   124 			writer->writeAttribute(L"");
   111 			writer->writeAttribute(L"");