streamlet-examples/xpath.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 27 Jan 2020 00:29:56 +0100
branchv_0
changeset 65 6944a03fb883
child 67 0766d298eb1c
permissions -rw-r--r--
streamlet examples: xpath – parse and validate XML document

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "streamlet-common.h"

#include <libxml++-2.6/libxml++/libxml++.h>

class XPathStreamlet : public Streamlet {

	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
		std::vector<AttributeMetadata> oam;
		oam.push_back({getAlias(0, L"xpath"), L"string"});
		return oam;
	}

	std::vector<OutputAttribute> getOutputAttributes() override {
		std::vector<OutputAttribute> oa;

		try {
			xmlpp::DomParser parser;
			parser.parse_file(convertor.to_bytes(currentFile));
			xmlpp::Element* root = parser.get_document()->get_root_node();
			oa.push_back({L"XML OK", false});
		} catch (...) {
			oa.push_back({L"invalid XML", true});
		}

		return oa;
	}
};

STREAMLET_RUN(XPathStreamlet)