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