3 namespace CoreFramework\Parser\Xml;
5 // Import framework stuff
6 use CoreFramework\Template\CompileableTemplate;
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class XmlParser extends BaseParser implements Parseable {
31 // Exception constants
32 const EXCEPTION_XML_PARSER_ERROR = 0x1e0;
33 const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1;
34 const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
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 ($content) {
71 // Convert all to UTF8
72 if (empty($content)) {
74 self::createDebugInstance(__CLASS__)->debugOutput('Empty content! Backtrace: <pre>');
75 debug_print_backtrace();
76 self::createDebugInstance(__CLASS__)->debugOutput('</pre>');
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 $this->partialStub('Cannot find recode/mbstring extension!');
90 $xmlParser = xml_parser_create();
92 // Force case-folding to on
93 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
96 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
98 // Get instance (we need this :( )
99 $templateInstance = $this->getTemplateInstance();
102 xml_set_object($xmlParser, $templateInstance);
104 // Set handler call-backs
105 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
106 xml_set_character_data_handler($xmlParser, 'characterHandler');
108 // Now parse the XML tree
109 if (!xml_parse($xmlParser, $content)) {
110 // Error found in XML!
111 //* DEBUG: */ exit(__METHOD__ . ':<pre>'.htmlentities($content).'</pre>');
112 throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
116 xml_parser_free($xmlParser);