src/XMLDocumentConstructor.h
branchv_0
changeset 6 47b677177365
parent 5 a3794fe5ea4b
child 7 15d9b0ca161a
equal deleted inserted replaced
5:a3794fe5ea4b 6:47b677177365
    59 		std::string result = value;
    59 		std::string result = value;
    60 		std::transform(result.begin(), result.end(), result.begin(), ::tolower);
    60 		std::transform(result.begin(), result.end(), result.begin(), ::tolower);
    61 		return result;
    61 		return result;
    62 	}
    62 	}
    63 
    63 
    64 	std::string fetchBody(std::shared_ptr<vmime::body> body) {
    64 	std::string toHex(const std::string& value) {
       
    65 		static const char* const hexSymbols = "0123456789abcdef";
       
    66 		size_t length = value.length();
       
    67 
       
    68 		std::string result;
       
    69 		result.reserve(3 * length - 1);
       
    70 		for (size_t i = 0; i < length; i++) {
       
    71 			const unsigned char ch = value[i];
       
    72 			result.push_back(hexSymbols[ch >> 4]);
       
    73 			result.push_back(hexSymbols[ch & 15]);
       
    74 			if (i < length - 1) result.push_back(' ');
       
    75 		}
       
    76 		return result;
       
    77 	}
       
    78 
       
    79 	std::string fetchBodyText(std::shared_ptr<vmime::body> body) {
    65 		std::stringstream result;
    80 		std::stringstream result;
    66 		vmime::utility::outputStreamAdapter resultAdapter(result);
    81 		vmime::utility::outputStreamAdapter resultAdapter(result);
    67 
    82 
    68 		const vmime::charset targetEncoding = vmime::charset("utf-8");
    83 		const vmime::charset targetEncoding = vmime::charset("utf-8");
    69 		const vmime::charset sourceEncoding = body->getCharset();
    84 		const vmime::charset sourceEncoding = body->getCharset();
    73 
    88 
    74 		body->getContents()->extract(*resultConverter);
    89 		body->getContents()->extract(*resultConverter);
    75 		resultConverter->flush();
    90 		resultConverter->flush();
    76 
    91 
    77 		return result.str();
    92 		return result.str();
       
    93 	}
       
    94 
       
    95 	std::string fetchBodyBinary(std::shared_ptr<vmime::body> body) {
       
    96 		std::stringstream result;
       
    97 		vmime::utility::outputStreamAdapter binaryResultAdapter(result);
       
    98 		body->getContents()->extract(binaryResultAdapter);
       
    99 		binaryResultAdapter.flush();
       
   100 		return toHex(result.str());
    78 	}
   101 	}
    79 
   102 
    80 	void appendBody(xmlpp::Element* element, std::shared_ptr<vmime::body> body) {
   103 	void appendBody(xmlpp::Element* element, std::shared_ptr<vmime::body> body) {
    81 		element->set_attribute("content-type", format(body->getContentType()));
   104 		element->set_attribute("content-type", format(body->getContentType()));
    82 		// element->set_attribute("content-type-charset", body->getCharset().getName());
   105 		// element->set_attribute("content-type-charset", body->getCharset().getName());
    83 		// element->set_attribute("content-transfer-encoding", body->getEncoding().getName());
   106 		// element->set_attribute("content-transfer-encoding", body->getEncoding().getName());
    84 
   107 
    85 		if (body->getPartCount() == 0) {
   108 		if (body->getPartCount() == 0) {
    86 			element->add_child_cdata(fetchBody(body));
   109 			if (body->getContentType().getType() == "text") element->add_child_cdata(fetchBodyText(body));
       
   110 			else element->add_child_text(fetchBodyBinary(body));
       
   111 			// TODO: if content is valid XML, import it in the DOM tree instead of pasting as a nested text/cdata
       
   112 			// TODO: optional trim of long data
    87 		} else {
   113 		} else {
    88 			for (auto part : body->getPartList()) {
   114 			for (auto part : body->getPartList()) {
    89 				xmlpp::Element* partElement = element->add_child("part");
   115 				xmlpp::Element* partElement = element->add_child("part");
    90 				appendBody(partElement, part->getBody());
   116 				appendBody(partElement, part->getBody());
    91 			}
   117 			}