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