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