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