renamed lib-local.php -> lib-lfdb.php because it really loads the "legendary"
[core.git] / inc / main / classes / parser / xml / class_XmlParser.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Parser\Xml;
4
5 // Import framework stuff
6 use CoreFramework\Template\CompileableTemplate;
7
8 /**
9  * A Xml Parser class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
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;
35
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates an instance of the class Parser and prepares it for usage
48          *
49          * @param       $templateInstance       A CompileableTemplate instance
50          * @return      $parserInstance         An instance of this parser
51          */
52         public static final function createXmlParser (CompileableTemplate $templateInstance) {
53                 // Get a new instance
54                 $parserInstance = new XmlParser();
55
56                 // Set the template instance
57                 $parserInstance->setTemplateInstance($templateInstance);
58
59                 // Return the prepared instance
60                 return $parserInstance;
61         }
62
63         /**
64          * Parses the given XML content
65          *
66          * @param       $content        Valid XML content
67          * @return      void
68          * @throws      XmlParserException      If an XML error was found
69          */
70         public function parseXmlContent ($content) {
71                 // Convert all to UTF8
72                 if (empty($content)) {
73                         // No empty content!
74                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Empty content! Backtrace: <pre>');
75                         debug_print_backtrace();
76                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('</pre>');
77                         exit();
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                         $this->partialStub('Cannot find recode/mbstring extension!');
87                 } // END - if
88
89                 // Get an XML parser
90                 $xmlParser = xml_parser_create();
91
92                 // Force case-folding to on
93                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
94
95                 // Set UTF-8
96                 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
97
98                 // Get instance (we need this :( )
99                 $templateInstance = $this->getTemplateInstance();
100
101                 // Set object
102                 xml_set_object($xmlParser, $templateInstance);
103
104                 // Set handler call-backs
105                 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
106                 xml_set_character_data_handler($xmlParser, 'characterHandler');
107
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);
113                 } // END - if
114
115                 // Free the parser
116                 xml_parser_free($xmlParser);
117         }
118
119 }