|
| 1 | +/** |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include "XMLRecordSetWriter.h" |
| 18 | + |
| 19 | +#include "core/Resource.h" |
| 20 | +#include "Exception.h" |
| 21 | +#include "utils/TimeUtil.h" |
| 22 | + |
| 23 | +namespace org::apache::nifi::minifi::standard { |
| 24 | + |
| 25 | +void XMLRecordSetWriter::onEnable() { |
| 26 | + if (auto wrap_elements_of_arrays = magic_enum::enum_cast<WrapElementsOfArraysOptions>(getProperty(WrapElementsOfArrays.name).value_or("No Wrapping"))) { |
| 27 | + wrap_elements_of_arrays_ = *wrap_elements_of_arrays; |
| 28 | + } else { |
| 29 | + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Invalid value for Wrap Elements of Arrays property: " + getProperty(WrapElementsOfArrays.name).value_or("")); |
| 30 | + } |
| 31 | + |
| 32 | + array_tag_name_ = getProperty(ArrayTagName.name).value_or(""); |
| 33 | + if (array_tag_name_.empty() && |
| 34 | + (wrap_elements_of_arrays_ == WrapElementsOfArraysOptions::UsePropertyAsWrapper || |
| 35 | + wrap_elements_of_arrays_ == WrapElementsOfArraysOptions::UsePropertyForElements)) { |
| 36 | + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Array Tag Name property must be set when Wrap Elements of Arrays is set to Use Property as Wrapper or Use Property for Elements"); |
| 37 | + } |
| 38 | + |
| 39 | + omit_xml_declaration_ = getProperty(OmitXMLDeclaration.name).value_or("false") == "true"; |
| 40 | + pretty_print_xml_ = getProperty(PrettyPrintXML.name).value_or("false") == "true"; |
| 41 | + |
| 42 | + name_of_record_tag_ = getProperty(NameOfRecordTag.name).value_or(""); |
| 43 | + if (name_of_record_tag_.empty()) { |
| 44 | + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Name of Record Tag property must be set"); |
| 45 | + } |
| 46 | + |
| 47 | + name_of_root_tag_ = getProperty(NameOfRootTag.name).value_or(""); |
| 48 | + if (name_of_root_tag_.empty()) { |
| 49 | + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Name of Root Tag property must be set"); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +std::string XMLRecordSetWriter::formatXmlOutput(pugi::xml_document& xml_doc) const { |
| 54 | + std::ostringstream xml_string_stream; |
| 55 | + uint64_t xml_formatting_flags = 0; |
| 56 | + if (pretty_print_xml_) { |
| 57 | + xml_formatting_flags |= pugi::format_indent; |
| 58 | + } else { |
| 59 | + xml_formatting_flags |= pugi::format_raw; |
| 60 | + } |
| 61 | + if (omit_xml_declaration_) { |
| 62 | + xml_formatting_flags |= pugi::format_no_declaration; |
| 63 | + } |
| 64 | + xml_doc.save(xml_string_stream, " ", gsl::narrow<unsigned int>(xml_formatting_flags)); |
| 65 | + return xml_string_stream.str(); |
| 66 | +} |
| 67 | + |
| 68 | +void XMLRecordSetWriter::convertRecordArrayField(const std::string& field_name, const core::RecordField& field, pugi::xml_node& parent_node) const { |
| 69 | + const auto& record_array = std::get<core::RecordArray>(field.value_); |
| 70 | + pugi::xml_node array_node; |
| 71 | + if (wrap_elements_of_arrays_ == WrapElementsOfArraysOptions::UsePropertyAsWrapper) { |
| 72 | + array_node = parent_node.append_child(array_tag_name_.c_str()); |
| 73 | + } else if (wrap_elements_of_arrays_ == WrapElementsOfArraysOptions::UsePropertyForElements) { |
| 74 | + array_node = parent_node.append_child(field_name.c_str()); |
| 75 | + } |
| 76 | + for (const auto& array_field : record_array) { |
| 77 | + if (wrap_elements_of_arrays_ == WrapElementsOfArraysOptions::UsePropertyAsWrapper) { |
| 78 | + convertRecordField(field_name, array_field, array_node); |
| 79 | + } else if (wrap_elements_of_arrays_ == WrapElementsOfArraysOptions::UsePropertyForElements) { |
| 80 | + convertRecordField(array_tag_name_, array_field, array_node); |
| 81 | + } else { |
| 82 | + convertRecordField(field_name, array_field, parent_node); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void XMLRecordSetWriter::convertRecordField(const std::string& field_name, const core::RecordField& field, pugi::xml_node& parent_node) const { |
| 88 | + if (std::holds_alternative<core::RecordArray>(field.value_)) { |
| 89 | + convertRecordArrayField(field_name, field, parent_node); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + pugi::xml_node field_node = parent_node.append_child(field_name.c_str()); |
| 94 | + if (std::holds_alternative<std::string>(field.value_)) { |
| 95 | + field_node.text().set(std::get<std::string>(field.value_)); |
| 96 | + } else if (std::holds_alternative<int64_t>(field.value_)) { |
| 97 | + field_node.text().set(std::to_string(std::get<int64_t>(field.value_)).c_str()); |
| 98 | + } else if (std::holds_alternative<uint64_t>(field.value_)) { |
| 99 | + field_node.text().set(std::to_string(std::get<uint64_t>(field.value_)).c_str()); |
| 100 | + } else if (std::holds_alternative<double>(field.value_)) { |
| 101 | + field_node.text().set(fmt::format("{:g}", std::get<double>(field.value_)).c_str()); |
| 102 | + } else if (std::holds_alternative<bool>(field.value_)) { |
| 103 | + field_node.text().set(std::get<bool>(field.value_) ? "true" : "false"); |
| 104 | + } else if (std::holds_alternative<std::chrono::system_clock::time_point>(field.value_)) { |
| 105 | + auto time_point = std::get<std::chrono::system_clock::time_point>(field.value_); |
| 106 | + auto time_str = utils::timeutils::getDateTimeStr(std::chrono::time_point_cast<std::chrono::seconds>(time_point)); |
| 107 | + field_node.text().set(time_str.c_str()); |
| 108 | + } else if (std::holds_alternative<core::RecordObject>(field.value_)) { |
| 109 | + const auto& record_object = std::get<core::RecordObject>(field.value_); |
| 110 | + for (const auto& [obj_key, obj_field] : record_object) { |
| 111 | + convertRecordField(obj_key, obj_field, field_node); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +std::string XMLRecordSetWriter::convertRecordSetToXml(const core::RecordSet& record_set) const { |
| 117 | + gsl_Expects(!name_of_record_tag_.empty() && !name_of_root_tag_.empty()); |
| 118 | + pugi::xml_document xml_doc; |
| 119 | + auto root_node = xml_doc.append_child(name_of_root_tag_.c_str()); |
| 120 | + |
| 121 | + for (const auto& record : record_set) { |
| 122 | + auto record_node = root_node.append_child(name_of_record_tag_.c_str()); |
| 123 | + for (const auto& [key, field] : record) { |
| 124 | + convertRecordField(key, field, record_node); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return formatXmlOutput(xml_doc); |
| 129 | +} |
| 130 | + |
| 131 | +void XMLRecordSetWriter::write(const core::RecordSet& record_set, const std::shared_ptr<core::FlowFile>& flow_file, core::ProcessSession& session) { |
| 132 | + if (!flow_file) { |
| 133 | + logger_->log_error("FlowFile is null, cannot write RecordSet to XML"); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + auto xml_content = convertRecordSetToXml(record_set); |
| 138 | + session.write(flow_file, [&xml_content](const std::shared_ptr<io::OutputStream>& stream) -> int64_t { |
| 139 | + stream->write(reinterpret_cast<const uint8_t*>(xml_content.data()), xml_content.size()); |
| 140 | + return gsl::narrow<int64_t>(xml_content.size()); |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +REGISTER_RESOURCE(XMLRecordSetWriter, ControllerService); |
| 145 | +} // namespace org::apache::nifi::minifi::standard |
0 commit comments