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