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 */ 16namespace Fisharebest\Webtrees\Report; 17 18use Fisharebest\Webtrees\Tree; 19 20/** 21 * Class ReportParserBase 22 */ 23class ReportParserBase 24{ 25 /** @var resource The XML parser */ 26 protected $xml_parser; 27 28 /** @var string Text contents of tags */ 29 protected $text = ''; 30 31 /** 32 * Create a parser for a report 33 * 34 * @param string $report The XML filename 35 * @param ReportBase $report_root 36 * @param string[][] $vars 37 * @param Tree $tree 38 */ 39 public function __construct($report, ReportBase $report_root, $vars, Tree $tree) 40 { 41 $this->xml_parser = xml_parser_create(); 42 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false); 43 xml_set_element_handler($this->xml_parser, [ 44 $this, 45 'startElement', 46 ], [ 47 $this, 48 'endElement', 49 ]); 50 xml_set_character_data_handler($this->xml_parser, [ 51 $this, 52 'characterData', 53 ]); 54 55 $fp = fopen($report, 'r'); 56 while (($data = fread($fp, 4096))) { 57 if (!xml_parse($this->xml_parser, $data, feof($fp))) { 58 throw new \DomainException(sprintf( 59 'XML error: %s at line %d', 60 xml_error_string(xml_get_error_code($this->xml_parser)), 61 xml_get_current_line_number($this->xml_parser) 62 )); 63 } 64 } 65 66 xml_parser_free($this->xml_parser); 67 } 68 69 /** 70 * XML handler for an opening (or self-closing) tag. 71 * 72 * @param resource $parser The resource handler for the xml parser 73 * @param string $name The name of the xml element parsed 74 * @param string[] $attrs An array of key value pairs for the attributes 75 */ 76 protected function startElement($parser, $name, $attrs) 77 { 78 $method = $name . 'StartHandler'; 79 if (method_exists($this, $method)) { 80 $this->$method($attrs); 81 } 82 } 83 84 /** 85 * XML handler for a closing tag. 86 * 87 * @param resource $parser the resource handler for the xml parser 88 * @param string $name the name of the xml element parsed 89 */ 90 protected function endElement($parser, $name) 91 { 92 $method = $name . 'EndHandler'; 93 if (method_exists($this, $method)) { 94 $this->$method(); 95 } 96 } 97 98 /** 99 * XML handler for character data. 100 * 101 * @param resource $parser The resource handler for the xml parser 102 * @param string $data The name of the xml element parsed 103 */ 104 protected function characterData($parser, $data) 105 { 106 $this->text .= $data; 107 } 108} 109