3 namespace Org\Mxchange\CoreFramework\Parser\Xml;
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;
11 use \InvalidArgumentException;
16 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
35 class XmlParser extends BaseParser implements Parseable {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of the class Parser and prepares it for usage
49 * @param $templateInstance A CompileableTemplate instance
50 * @return $parserInstance An instance of this parser
52 public static final function createXmlParser (CompileableTemplate $templateInstance) {
54 $parserInstance = new XmlParser();
56 // Set the template instance
57 $parserInstance->setTemplateInstance($templateInstance);
59 // Return the prepared instance
60 return $parserInstance;
64 * Parses the given XML content
66 * @param $content Valid XML content
68 * @throws XmlParserException If an XML error was found
70 public function parseXmlContent (string $content) {
71 // Convert all to UTF8
72 if (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');
82 // @TODO We need to find a fallback solution here
83 $this->partialStub('Cannot find recode/mbstring extension!');
87 $xmlParser = xml_parser_create();
89 // Force case-folding to on
90 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
93 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
96 xml_set_object($xmlParser, $this->getTemplateInstance());
98 // Set handler call-backs
99 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
100 xml_set_character_data_handler($xmlParser, 'characterHandler');
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);
110 xml_parser_free($xmlParser);