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