|
| 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 | + |
| 18 | +#include "XMLReader.h" |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | +#include <ranges> |
| 22 | + |
| 23 | +#include "core/Resource.h" |
| 24 | +#include "utils/TimeUtil.h" |
| 25 | +#include "utils/gsl.h" |
| 26 | + |
| 27 | +namespace org::apache::nifi::minifi::standard { |
| 28 | + |
| 29 | +namespace { |
| 30 | +bool hasChildNodes(const pugi::xml_node& node) { |
| 31 | + return std::ranges::any_of(node, [] (const pugi::xml_node& child) { |
| 32 | + return child.type() == pugi::node_element; |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +void addRecordFieldToObject(core::RecordObject& record_object, const std::string& name, const core::RecordField& field) { |
| 37 | + auto it = record_object.find(name); |
| 38 | + if (it == record_object.end()) { |
| 39 | + record_object.emplace(name, field); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (std::holds_alternative<core::RecordArray>(it->second.value_)) { |
| 44 | + std::get<core::RecordArray>(it->second.value_).emplace_back(field); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + core::RecordArray array; |
| 49 | + array.emplace_back(it->second); |
| 50 | + array.emplace_back(field); |
| 51 | + it->second = core::RecordField(std::move(array)); |
| 52 | +} |
| 53 | +} // namespace |
| 54 | + |
| 55 | +void XMLReader::writeRecordField(core::RecordObject& record_object, const std::string& name, const std::string& value, bool write_pcdata_node) const { |
| 56 | + // If the name is the value set in the Field Name for Content property, we should only add this value to the RecordObject if we are writing a plain character data node. |
| 57 | + if (!write_pcdata_node && name == field_name_for_content_) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (value == "true" || value == "false") { |
| 62 | + addRecordFieldToObject(record_object, name, core::RecordField(value == "true")); |
| 63 | + return; |
| 64 | + } else if (auto date = utils::timeutils::parseDateTimeStr(value)) { |
| 65 | + addRecordFieldToObject(record_object, name, core::RecordField(*date)); |
| 66 | + return; |
| 67 | + } else if (auto date = utils::timeutils::parseRfc3339(value)) { |
| 68 | + addRecordFieldToObject(record_object, name, core::RecordField(*date)); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (std::ranges::all_of(value, ::isdigit)) { |
| 73 | + try { |
| 74 | + uint64_t value_as_uint64 = std::stoull(value); |
| 75 | + addRecordFieldToObject(record_object, name, core::RecordField(value_as_uint64)); |
| 76 | + return; |
| 77 | + } catch (const std::exception&) { |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (value.starts_with('-') && std::ranges::all_of(value | std::views::drop(1), ::isdigit)) { |
| 82 | + try { |
| 83 | + int64_t value_as_int64 = std::stoll(value); |
| 84 | + addRecordFieldToObject(record_object, name, core::RecordField(value_as_int64)); |
| 85 | + return; |
| 86 | + } catch (const std::exception&) { |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + auto value_as_double = std::stod(value); |
| 92 | + addRecordFieldToObject(record_object, name, core::RecordField(value_as_double)); |
| 93 | + return; |
| 94 | + } catch (const std::exception&) { |
| 95 | + } |
| 96 | + |
| 97 | + addRecordFieldToObject(record_object, name, core::RecordField(value)); |
| 98 | +} |
| 99 | + |
| 100 | +void XMLReader::parseNodeElement(core::RecordObject& record_object, const pugi::xml_node& node) const { |
| 101 | + gsl_Expects(node.type() == pugi::node_element); |
| 102 | + if (parse_xml_attributes_ && node.first_attribute()) { |
| 103 | + core::RecordObject child_record_object; |
| 104 | + for (const pugi::xml_attribute& attr : node.attributes()) { |
| 105 | + writeRecordField(child_record_object, attribute_prefix_ + attr.name(), attr.value()); |
| 106 | + } |
| 107 | + parseXmlNode(child_record_object, node); |
| 108 | + addRecordFieldToObject(record_object, node.name(), core::RecordField(std::move(child_record_object))); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (hasChildNodes(node)) { |
| 113 | + core::RecordObject child_record_object; |
| 114 | + parseXmlNode(child_record_object, node); |
| 115 | + addRecordFieldToObject(record_object, node.name(), core::RecordField(std::move(child_record_object))); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + writeRecordField(record_object, node.name(), node.child_value()); |
| 120 | +} |
| 121 | + |
| 122 | +void XMLReader::parseXmlNode(core::RecordObject& record_object, const pugi::xml_node& node) const { |
| 123 | + std::string pc_data_value; |
| 124 | + for (pugi::xml_node child : node.children()) { |
| 125 | + if (child.type() == pugi::node_element) { |
| 126 | + parseNodeElement(record_object, child); |
| 127 | + } else if (child.type() == pugi::node_pcdata) { |
| 128 | + pc_data_value.append(child.value()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (!pc_data_value.empty()) { |
| 133 | + writeRecordField(record_object, field_name_for_content_, pc_data_value, true); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +void XMLReader::addRecordFromXmlNode(const pugi::xml_node& node, core::RecordSet& record_set) const { |
| 138 | + core::RecordObject record_object; |
| 139 | + parseXmlNode(record_object, node); |
| 140 | + core::Record record(std::move(record_object)); |
| 141 | + record_set.emplace_back(std::move(record)); |
| 142 | +} |
| 143 | + |
| 144 | +bool XMLReader::parseRecordsFromXml(core::RecordSet& record_set, const std::string& xml_content) const { |
| 145 | + pugi::xml_document doc; |
| 146 | + if (!doc.load_string(xml_content.c_str())) { |
| 147 | + logger_->log_error("Failed to parse XML content: {}", xml_content); |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + if (expect_records_as_array_) { |
| 152 | + pugi::xml_node root = doc.first_child(); |
| 153 | + for (pugi::xml_node record_node : root.children()) { |
| 154 | + addRecordFromXmlNode(record_node, record_set); |
| 155 | + } |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + pugi::xml_node root = doc.first_child(); |
| 160 | + if (!root.first_child()) { |
| 161 | + logger_->log_info("XML content does not contain any records: {}", xml_content); |
| 162 | + return true; |
| 163 | + } |
| 164 | + addRecordFromXmlNode(root, record_set); |
| 165 | + return true; |
| 166 | +} |
| 167 | + |
| 168 | +void XMLReader::onEnable() { |
| 169 | + auto parseBoolProperty = [this](std::string_view property_name) -> bool { |
| 170 | + if (auto property_value_str = getProperty(property_name); property_value_str && !property_value_str->empty()) { |
| 171 | + if (auto property_value = parsing::parseBool(*property_value_str)) { |
| 172 | + return *property_value; |
| 173 | + } |
| 174 | + throw Exception(PROCESS_SCHEDULE_EXCEPTION, fmt::format("Invalid value for {} property: {}", property_name, *property_value_str)); |
| 175 | + } |
| 176 | + return false; |
| 177 | + }; |
| 178 | + |
| 179 | + field_name_for_content_ = getProperty(FieldNameForContent.name).value_or("value"); |
| 180 | + parse_xml_attributes_ = parseBoolProperty(ParseXMLAttributes.name); |
| 181 | + attribute_prefix_ = getProperty(AttributePrefix.name).value_or(""); |
| 182 | + expect_records_as_array_ = parseBoolProperty(ExpectRecordsAsArray.name); |
| 183 | +} |
| 184 | + |
| 185 | +nonstd::expected<core::RecordSet, std::error_code> XMLReader::read(io::InputStream& input_stream) { |
| 186 | + core::RecordSet record_set{}; |
| 187 | + const auto read_result = [this, &record_set](io::InputStream& input_stream) -> size_t { |
| 188 | + std::string content; |
| 189 | + content.resize(input_stream.size()); |
| 190 | + const auto read_ret = input_stream.read(as_writable_bytes(std::span(content))); |
| 191 | + if (io::isError(read_ret)) { |
| 192 | + logger_->log_error("Failed to read XML data from input stream"); |
| 193 | + return io::STREAM_ERROR; |
| 194 | + } |
| 195 | + if (!parseRecordsFromXml(record_set, content)) { |
| 196 | + return io::STREAM_ERROR; |
| 197 | + } |
| 198 | + return read_ret; |
| 199 | + }(input_stream); |
| 200 | + if (io::isError(read_result)) { |
| 201 | + return nonstd::make_unexpected(std::make_error_code(std::errc::invalid_argument)); |
| 202 | + } |
| 203 | + return record_set; |
| 204 | +} |
| 205 | + |
| 206 | +REGISTER_RESOURCE(XMLReader, ControllerService); |
| 207 | +} // namespace org::apache::nifi::minifi::standard |
0 commit comments