1ef0d468bSGreg Roach<?php 23976b470SGreg Roach 3ef0d468bSGreg Roach/** 4ef0d468bSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6ef0d468bSGreg Roach * This program is free software: you can redistribute it and/or modify 7ef0d468bSGreg Roach * it under the terms of the GNU General Public License as published by 8ef0d468bSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9ef0d468bSGreg Roach * (at your option) any later version. 10ef0d468bSGreg Roach * This program is distributed in the hope that it will be useful, 11ef0d468bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12ef0d468bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13ef0d468bSGreg Roach * GNU General Public License for more details. 14ef0d468bSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16ef0d468bSGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report; 21ef0d468bSGreg Roach 22208e9f76SGreg Roachuse DomainException; 237774ce41SGreg Roachuse Exception; 248792b649SGreg Roachuse XMLParser; 25299d100dSGreg Roach 26b6f35a76SGreg Roachuse function fclose; 27b6f35a76SGreg Roachuse function feof; 28b6f35a76SGreg Roachuse function fread; 29b19e047dSGreg Roachuse function method_exists; 30b6f35a76SGreg Roachuse function sprintf; 31b6f35a76SGreg Roachuse function xml_error_string; 32b6f35a76SGreg Roachuse function xml_get_current_line_number; 33b6f35a76SGreg Roachuse function xml_get_error_code; 34b6f35a76SGreg Roachuse function xml_parse; 35b6f35a76SGreg Roachuse function xml_parser_create; 36b6f35a76SGreg Roachuse function xml_parser_free; 37b6f35a76SGreg Roachuse function xml_parser_set_option; 38b6f35a76SGreg Roachuse function xml_set_character_data_handler; 39b6f35a76SGreg Roachuse function xml_set_element_handler; 40b6f35a76SGreg Roach 41b6f35a76SGreg Roachuse const XML_OPTION_CASE_FOLDING; 42b19e047dSGreg Roach 43ef0d468bSGreg Roach/** 44ef0d468bSGreg Roach * Class ReportParserBase 45ef0d468bSGreg Roach */ 46c1010edaSGreg Roachclass ReportParserBase 47c1010edaSGreg Roach{ 48b112142eSGreg Roach // The XML parser 49b112142eSGreg Roach protected XMLParser $xml_parser; 50ef0d468bSGreg Roach 51ef0d468bSGreg Roach /** @var string Text contents of tags */ 5277bab461SGreg Roach protected string $text = ''; 53ef0d468bSGreg Roach 54ef0d468bSGreg Roach /** 55ef0d468bSGreg Roach * Create a parser for a report 56ef0d468bSGreg Roach * 57ef0d468bSGreg Roach * @param string $report The XML filename 587774ce41SGreg Roach * 597774ce41SGreg Roach * @throws Exception 60ef0d468bSGreg Roach */ 6176f666f4SGreg Roach public function __construct(string $report) 62c1010edaSGreg Roach { 63ef0d468bSGreg Roach $this->xml_parser = xml_parser_create(); 648a4ee39cSGreg Roach 65*f315390bSGreg Roach xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 0); 668a4ee39cSGreg Roach 678a4ee39cSGreg Roach xml_set_element_handler( 688a4ee39cSGreg Roach $this->xml_parser, 69af14d238SGreg Roach function ($parser, string $name, array $attrs): void { 708a4ee39cSGreg Roach $this->startElement($parser, $name, $attrs); 718a4ee39cSGreg Roach }, 72af14d238SGreg Roach function ($parser, string $name): void { 738a4ee39cSGreg Roach $this->endElement($parser, $name); 748a4ee39cSGreg Roach } 758a4ee39cSGreg Roach ); 768a4ee39cSGreg Roach 778a4ee39cSGreg Roach xml_set_character_data_handler( 788a4ee39cSGreg Roach $this->xml_parser, 799d454b6bSGreg Roach function ($parser, string $data): void { 808a4ee39cSGreg Roach $this->characterData($parser, $data); 818a4ee39cSGreg Roach } 828a4ee39cSGreg Roach ); 83ef0d468bSGreg Roach 846c84de1dSGreg Roach $fp = fopen($report, 'rb'); 857774ce41SGreg Roach 86e364afe4SGreg Roach while ($data = fread($fp, 4096)) { 87ef0d468bSGreg Roach if (!xml_parse($this->xml_parser, $data, feof($fp))) { 88208e9f76SGreg Roach throw new DomainException(sprintf( 89ef0d468bSGreg Roach 'XML error: %s at line %d', 90ef0d468bSGreg Roach xml_error_string(xml_get_error_code($this->xml_parser)), 91ef0d468bSGreg Roach xml_get_current_line_number($this->xml_parser) 92ef0d468bSGreg Roach )); 93ef0d468bSGreg Roach } 94ef0d468bSGreg Roach } 95ef0d468bSGreg Roach 967774ce41SGreg Roach fclose($fp); 977774ce41SGreg Roach 98ef0d468bSGreg Roach xml_parser_free($this->xml_parser); 99ef0d468bSGreg Roach } 100ef0d468bSGreg Roach 101ef0d468bSGreg Roach /** 102ef0d468bSGreg Roach * XML handler for an opening (or self-closing) tag. 103ef0d468bSGreg Roach * 104ef0d468bSGreg Roach * @param resource $parser The resource handler for the xml parser 105ef0d468bSGreg Roach * @param string $name The name of the xml element parsed 10609482a55SGreg Roach * @param array<string> $attrs An array of key value pairs for the attributes 10718d7a90dSGreg Roach * 10818d7a90dSGreg Roach * @return void 109ef0d468bSGreg Roach */ 110af14d238SGreg Roach protected function startElement($parser, string $name, array $attrs): void 111c1010edaSGreg Roach { 112a6f13a4aSGreg Roach $method = $name . 'StartHandler'; 113208e9f76SGreg Roach 114ef0d468bSGreg Roach if (method_exists($this, $method)) { 11579a908a7SGreg Roach $this->$method($attrs); 116ef0d468bSGreg Roach } 117ef0d468bSGreg Roach } 118ef0d468bSGreg Roach 119ef0d468bSGreg Roach /** 120ef0d468bSGreg Roach * XML handler for a closing tag. 121ef0d468bSGreg Roach * 122ef0d468bSGreg Roach * @param resource $parser the resource handler for the xml parser 123ef0d468bSGreg Roach * @param string $name the name of the xml element parsed 12418d7a90dSGreg Roach * 12518d7a90dSGreg Roach * @return void 126ef0d468bSGreg Roach */ 127af14d238SGreg Roach protected function endElement($parser, string $name): void 128c1010edaSGreg Roach { 129a6f13a4aSGreg Roach $method = $name . 'EndHandler'; 130208e9f76SGreg Roach 131ef0d468bSGreg Roach if (method_exists($this, $method)) { 13279a908a7SGreg Roach $this->$method(); 133ef0d468bSGreg Roach } 134ef0d468bSGreg Roach } 135ef0d468bSGreg Roach 136ef0d468bSGreg Roach /** 137ef0d468bSGreg Roach * XML handler for character data. 138ef0d468bSGreg Roach * 139ef0d468bSGreg Roach * @param resource $parser The resource handler for the xml parser 140ef0d468bSGreg Roach * @param string $data The name of the xml element parsed 14118d7a90dSGreg Roach * 14218d7a90dSGreg Roach * @return void 143ef0d468bSGreg Roach */ 14424f2a3afSGreg Roach protected function characterData($parser, string $data): void 145c1010edaSGreg Roach { 146ef0d468bSGreg Roach $this->text .= $data; 147ef0d468bSGreg Roach } 148ef0d468bSGreg Roach} 149