3 namespace Org\Mxchange\CoreFramework\Parser\Xml;
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;
13 use \InvalidArgumentException;
18 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
37 class XmlParser extends BaseParser implements Parseable {
39 * Protected constructor
43 private function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of the class Parser and prepares it for usage
51 * @param $templateInstance A CompileableTemplate instance
52 * @return $parserInstance An instance of this parser
54 public static final function createXmlParser (CompileableTemplate $templateInstance) {
56 $parserInstance = new XmlParser();
58 // Set the template instance
59 $parserInstance->setTemplateInstance($templateInstance);
61 // Return the prepared instance
62 return $parserInstance;
66 * Parses the given XML content
68 * @param $content Valid XML content
70 * @throws XmlParserException If an XML error was found
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)) {
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');
85 // @TODO We need to find a fallback solution here
86 DebugMiddleware::getSelfInstance()->partialStub('Cannot find recode/mbstring extension!');
90 $xmlParser = xml_parser_create();
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);
97 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
100 xml_set_object($xmlParser, $this->getTemplateInstance());
102 // Set handler call-backs
103 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
104 xml_set_character_data_handler($xmlParser, 'characterHandler');
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);
115 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('XML-PARSER: Freeing xmlParser=%s ...', $xmlParser));
116 xml_parser_free($xmlParser);
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('XML-PARSER: EXIT!');