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