xref: /webtrees/app/Report/ReportParserBase.php (revision a6f13a4a2d4f52465c4c6a7bad3a4dbfa1ec5089)
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\Date;
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	 */
35	public function __construct($report) {
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	public 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	public 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	public function characterData($parser, $data) {
89		$this->text .= $data;
90	}
91}
92