]> git.mxchange.org Git - core.git/blob - framework/main/classes/parser/xml/class_XmlParser.php
28ba4a8b607ae17a5d5b6a95156d9ded8a7b6723
[core.git] / framework / main / classes / parser / xml / class_XmlParser.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Parser\Xml;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Parser\BaseParser;
7 use Org\Mxchange\CoreFramework\Parser\Parseable;
8 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
9
10 // Import SPL stuff
11 use \InvalidArgumentException;
12
13 /**
14  * A Xml Parser class
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class XmlParser extends BaseParser implements Parseable {
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates an instance of the class Parser and prepares it for usage
48          *
49          * @param       $templateInstance       A CompileableTemplate instance
50          * @return      $parserInstance         An instance of this parser
51          */
52         public static final function createXmlParser (CompileableTemplate $templateInstance) {
53                 // Get a new instance
54                 $parserInstance = new XmlParser();
55
56                 // Set the template instance
57                 $parserInstance->setTemplateInstance($templateInstance);
58
59                 // Return the prepared instance
60                 return $parserInstance;
61         }
62
63         /**
64          * Parses the given XML content
65          *
66          * @param       $content        Valid XML content
67          * @return      void
68          * @throws      XmlParserException      If an XML error was found
69          */
70         public function parseXmlContent (string $content) {
71                 // Convert all to UTF8
72                 if (empty($content)) {
73                         // No empty content
74                         throw new InvalidArgumentException('content is empty');
75                 } elseif (function_exists('recode')) {
76                         // Recode found, so use it
77                         $content = recode('html..utf8', $content);
78                 } elseif (function_exists('mb_convert_encoding')) {
79                         // Use mb_convert_encoding()
80                         $content = mb_convert_encoding($content, 'UTF-8', 'auto');
81                 } else {
82                         // @TODO We need to find a fallback solution here
83                         $this->partialStub('Cannot find recode/mbstring extension!');
84                 }
85
86                 // Get an XML parser
87                 $xmlParser = xml_parser_create();
88
89                 // Force case-folding to on
90                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
91
92                 // Set UTF-8
93                 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
94
95                 // Set object
96                 xml_set_object($xmlParser, $this->getTemplateInstance());
97
98                 // Set handler call-backs
99                 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
100                 xml_set_character_data_handler($xmlParser, 'characterHandler');
101
102                 // Now parse the XML tree
103                 if (!xml_parse($xmlParser, $content)) {
104                         // Error found in XML
105                         //* DEBUG: */ exit(__METHOD__ . ':<pre>'.htmlentities($content).'</pre>');
106                         throw new XmlParserException(array($this, $xmlParser), Parseable::EXCEPTION_XML_PARSER_ERROR);
107                 }
108
109                 // Free the parser
110                 xml_parser_free($xmlParser);
111         }
112
113 }