xref: /webtrees/app/Report/ReportParserBase.php (revision fc747c0fe6ab8c4d4d65bbb60eb220a1c792d7f6)
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     * @return void
74     */
75    protected function startElement($parser, $name, $attrs)
76    {
77        $method = $name . 'StartHandler';
78        if (method_exists($this, $method)) {
79            $this->$method($attrs);
80        }
81    }
82
83    /**
84     * XML handler for a closing tag.
85     *
86     * @param resource $parser the resource handler for the xml parser
87     * @param string   $name   the name of the xml element parsed
88     *
89     * @return void
90     */
91    protected function endElement($parser, $name)
92    {
93        $method = $name . 'EndHandler';
94        if (method_exists($this, $method)) {
95            $this->$method();
96        }
97    }
98
99    /**
100     * XML handler for character data.
101     *
102     * @param resource $parser The resource handler for the xml parser
103     * @param string   $data   The name of the xml element parsed
104     *
105     * @return void
106     */
107    protected function characterData($parser, $data)
108    {
109        $this->text .= $data;
110    }
111}
112