src/XMLDocumentConstructor.h
branchv_0
changeset 43 6b194b178946
parent 42 c97b6152016a
equal deleted inserted replaced
42:c97b6152016a 43:6b194b178946
    56 	 * number of remainin items in the fixed-size container (map or array) at current tree-level
    56 	 * number of remainin items in the fixed-size container (map or array) at current tree-level
    57 	 */
    57 	 */
    58 	std::vector<ssize_t> remainingItems;
    58 	std::vector<ssize_t> remainingItems;
    59 #define INDEFINITE -1
    59 #define INDEFINITE -1
    60 
    60 
       
    61 #define ATTR_VALUE_TYPE "value-type"
       
    62 #define ATTR_KEY_TYPE "key-type"
       
    63 #define ATTR_TAG "tag"
       
    64 #define ATTR_BYTES_READ "bytes-read"
       
    65 
    61 	std::stringstream currentIndefiniteString;
    66 	std::stringstream currentIndefiniteString;
    62 
    67 
    63 	uint64_t currentTag;
    68 	uint64_t currentTag;
    64 	bool currentTagPresent = false;
    69 	bool currentTagPresent = false;
    65 
    70 
    85 		}
    90 		}
    86 	}
    91 	}
    87 
    92 
    88 	void writeCurrentTag(xmlpp::Element* element) {
    93 	void writeCurrentTag(xmlpp::Element* element) {
    89 		if (currentTagPresent) {
    94 		if (currentTagPresent) {
    90 			element->set_attribute("tag", std::to_string(currentTag));
    95 			element->set_attribute(ATTR_TAG, std::to_string(currentTag));
    91 			currentTagPresent = false;
    96 			currentTagPresent = false;
    92 		}
    97 		}
    93 	}
    98 	}
    94 
    99 
    95 	void appendScalarValue(Glib::ustring value, Glib::ustring cborType) {
   100 	void appendScalarValue(Glib::ustring value, Glib::ustring cborType) {
    96 		if (mode.back() == Mode::ARRAY) {
   101 		if (mode.back() == Mode::ARRAY) {
    97 			xmlpp::Element* element = current->add_child(nameCodec.encode(itemName));
   102 			xmlpp::Element* element = current->add_child(nameCodec.encode(itemName));
    98 			element->add_child_text(value);
   103 			element->add_child_text(value);
    99 			element->set_attribute("value-type", cborType);
   104 			element->set_attribute(ATTR_VALUE_TYPE, cborType);
   100 			writeCurrentTag(element);
   105 			writeCurrentTag(element);
   101 		} else if (mode.back() == Mode::MAP_KEY) {
   106 		} else if (mode.back() == Mode::MAP_KEY) {
   102 			current = current->add_child(nameCodec.encode(value));
   107 			current = current->add_child(nameCodec.encode(value));
   103 			current->set_attribute("key-type", cborType);
   108 			current->set_attribute(ATTR_KEY_TYPE, cborType);
   104 			writeCurrentTag(current);
   109 			writeCurrentTag(current);
   105 			mode.push_back(Mode::MAP_VALUE);
   110 			mode.push_back(Mode::MAP_VALUE);
   106 		} else if (mode.back() == Mode::MAP_VALUE) {
   111 		} else if (mode.back() == Mode::MAP_VALUE) {
   107 			current->add_child_text(value);
   112 			current->add_child_text(value);
   108 			current->set_attribute("value-type", cborType);
   113 			current->set_attribute(ATTR_VALUE_TYPE, cborType);
   109 			writeCurrentTag(current);
   114 			writeCurrentTag(current);
   110 			current = parentOrSelf(current);
   115 			current = parentOrSelf(current);
   111 			mode.pop_back();
   116 			mode.pop_back();
   112 		} else if (mode.back() == Mode::ROOT) {
   117 		} else if (mode.back() == Mode::ROOT) {
   113 			current->add_child_text(value);
   118 			current->add_child_text(value);
   114 			current->set_attribute("value-type", cborType);
   119 			current->set_attribute(ATTR_VALUE_TYPE, cborType);
   115 			writeCurrentTag(current);
   120 			writeCurrentTag(current);
   116 		} else if (mode.back() == Mode::BYTE_STRING || mode.back() == Mode::CHAR_STRING) {
   121 		} else if (mode.back() == Mode::BYTE_STRING || mode.back() == Mode::CHAR_STRING) {
   117 			currentIndefiniteString << value;
   122 			currentIndefiniteString << value;
   118 		} else {
   123 		} else {
   119 			// TODO: throw exception?
   124 			// TODO: throw exception?
   122 		checkRemainingItems();
   127 		checkRemainingItems();
   123 	}
   128 	}
   124 
   129 
   125 	void arrayStart(ssize_t size) {
   130 	void arrayStart(ssize_t size) {
   126 		if (mode.back() == Mode::ROOT) {
   131 		if (mode.back() == Mode::ROOT) {
   127 			current->set_attribute("value-type", "array");
   132 			current->set_attribute(ATTR_VALUE_TYPE, "array");
   128 			itemName = "item";
   133 			itemName = "item";
   129 		} else if (mode.back() == Mode::ARRAY) {
   134 		} else if (mode.back() == Mode::ARRAY) {
   130 			current = current->add_child(nameCodec.encode(itemName));
   135 			current = current->add_child(nameCodec.encode(itemName));
   131 			itemName = "item";
   136 			itemName = "item";
   132 		} else if (mode.back() == Mode::MAP_VALUE) {
   137 		} else if (mode.back() == Mode::MAP_VALUE) {
   159 		} else {
   164 		} else {
   160 			// TODO: map might be a key of another map → wrap/nest
   165 			// TODO: map might be a key of another map → wrap/nest
   161 			// …probably not
   166 			// …probably not
   162 		}
   167 		}
   163 
   168 
   164 		current->set_attribute("value-type", "map");
   169 		current->set_attribute(ATTR_VALUE_TYPE, "map");
   165 
   170 
   166 		mode.push_back(Mode::MAP_KEY);
   171 		mode.push_back(Mode::MAP_KEY);
   167 		remainingItems.push_back(size);
   172 		remainingItems.push_back(size);
   168 		writeCurrentTag(current);
   173 		writeCurrentTag(current);
   169 
   174 
   317 			if (result.status != cbor_decoder_status::CBOR_DECODER_FINISHED) throw relpipe::writer::RelpipeWriterException(L"CBOR parsing failed: status = " + std::to_wstring(result.status));
   322 			if (result.status != cbor_decoder_status::CBOR_DECODER_FINISHED) throw relpipe::writer::RelpipeWriterException(L"CBOR parsing failed: status = " + std::to_wstring(result.status));
   318 		}
   323 		}
   319 
   324 
   320 		checkRemainingItems();
   325 		checkRemainingItems();
   321 
   326 
   322 		parser->get_document()->get_root_node()->set_attribute("bytes-read", std::to_string(bytesRead));
   327 		parser->get_document()->get_root_node()->set_attribute(ATTR_BYTES_READ, std::to_string(bytesRead));
   323 	}
   328 	}
   324 };
   329 };
   325 
   330 
   326 }
   331 }
   327 }
   332 }