1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Report; 19 20use Fisharebest\Webtrees\Tree; 21 22/** 23 * Class ReportParserBase 24 */ 25class ReportParserBase 26{ 27 /** @var resource The XML parser */ 28 protected $xml_parser; 29 30 /** @var string Text contents of tags */ 31 protected $text = ''; 32 33 /** 34 * Create a parser for a report 35 * 36 * @param string $report The XML filename 37 */ 38 public function __construct(string $report) 39 { 40 $this->xml_parser = xml_parser_create(); 41 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false); 42 xml_set_element_handler($this->xml_parser, [ 43 $this, 44 'startElement', 45 ], [ 46 $this, 47 'endElement', 48 ]); 49 xml_set_character_data_handler($this->xml_parser, [ 50 $this, 51 'characterData', 52 ]); 53 54 $fp = fopen($report, 'r'); 55 while (($data = fread($fp, 4096))) { 56 if (!xml_parse($this->xml_parser, $data, feof($fp))) { 57 throw new \DomainException(sprintf( 58 'XML error: %s at line %d', 59 xml_error_string(xml_get_error_code($this->xml_parser)), 60 xml_get_current_line_number($this->xml_parser) 61 )); 62 } 63 } 64 65 xml_parser_free($this->xml_parser); 66 } 67 68 /** 69 * XML handler for an opening (or self-closing) tag. 70 * 71 * @param resource $parser The resource handler for the xml parser 72 * @param string $name The name of the xml element parsed 73 * @param string[] $attrs An array of key value pairs for the attributes 74 * 75 * @return void 76 */ 77 protected function startElement($parser, $name, $attrs) 78 { 79 $method = $name . 'StartHandler'; 80 if (method_exists($this, $method)) { 81 $this->$method($attrs); 82 } 83 } 84 85 /** 86 * XML handler for a closing tag. 87 * 88 * @param resource $parser the resource handler for the xml parser 89 * @param string $name the name of the xml element parsed 90 * 91 * @return void 92 */ 93 protected function endElement($parser, $name) 94 { 95 $method = $name . 'EndHandler'; 96 if (method_exists($this, $method)) { 97 $this->$method(); 98 } 99 } 100 101 /** 102 * XML handler for character data. 103 * 104 * @param resource $parser The resource handler for the xml parser 105 * @param string $data The name of the xml element parsed 106 * 107 * @return void 108 */ 109 protected function characterData($parser, $data) 110 { 111 $this->text .= $data; 112 } 113} 114