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