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;
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class XmlParser extends BaseParser implements Parseable {
33 // Exception constants
34 const EXCEPTION_XML_PARSER_ERROR = 0x1e0;
35 const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1;
36 const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
39 * Protected constructor
43 protected 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 ($content) {
73 // Convert all to UTF8
74 if (empty($content)) {
76 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Empty content! Backtrace: <pre>');
77 debug_print_backtrace();
78 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('</pre>');
80 } elseif (function_exists('recode')) {
81 // Recode found, so use it
82 $content = recode('html..utf8', $content);
83 } elseif (function_exists('mb_convert_encoding')) {
84 // Use mb_convert_encoding()
85 $content = mb_convert_encoding($content, 'UTF-8', 'auto');
87 // @TODO We need to find a fallback solution here
88 $this->partialStub('Cannot find recode/mbstring extension!');
92 $xmlParser = xml_parser_create();
94 // Force case-folding to on
95 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
98 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
100 // Get instance (we need this :( )
101 $templateInstance = $this->getTemplateInstance();
104 xml_set_object($xmlParser, $templateInstance);
106 // Set handler call-backs
107 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
108 xml_set_character_data_handler($xmlParser, 'characterHandler');
110 // Now parse the XML tree
111 if (!xml_parse($xmlParser, $content)) {
112 // Error found in XML!
113 //* DEBUG: */ exit(__METHOD__ . ':<pre>'.htmlentities($content).'</pre>');
114 throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
118 xml_parser_free($xmlParser);