Some updates:
[core.git] / framework / main / classes / parser / xml / class_XmlParser.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Parser\Xml;
4
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;
9
10 /**
11  * A Xml Parser class
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15 <<<<<<< HEAD:framework/main/classes/parser/xml/class_XmlParser.php
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 =======
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/parser/xml/class_XmlParser.php
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class XmlParser extends BaseParser implements Parseable {
37         // Exception constants
38         const EXCEPTION_XML_PARSER_ERROR             = 0x1e0;
39         const EXCEPTION_XML_NODE_UNKNOWN             = 0x1e1;
40         const EXCEPTION_XML_NODE_MISMATCH            = 0x1e2;
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Creates an instance of the class Parser and prepares it for usage
54          *
55          * @param       $templateInstance       A CompileableTemplate instance
56          * @return      $parserInstance         An instance of this parser
57          */
58         public static final function createXmlParser (CompileableTemplate $templateInstance) {
59                 // Get a new instance
60                 $parserInstance = new XmlParser();
61
62                 // Set the template instance
63                 $parserInstance->setTemplateInstance($templateInstance);
64
65                 // Return the prepared instance
66                 return $parserInstance;
67         }
68
69         /**
70          * Parses the given XML content
71          *
72          * @param       $content        Valid XML content
73          * @return      void
74          * @throws      XmlParserException      If an XML error was found
75          */
76         public function parseXmlContent ($content) {
77                 // Convert all to UTF8
78                 if (empty($content)) {
79                         // No empty content!
80                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Empty content! Backtrace: <pre>');
81                         debug_print_backtrace();
82                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('</pre>');
83                         exit();
84                 } elseif (function_exists('recode')) {
85                         // Recode found, so use it
86                         $content = recode('html..utf8', $content);
87                 } elseif (function_exists('mb_convert_encoding')) {
88                         // Use mb_convert_encoding()
89                         $content = mb_convert_encoding($content, 'UTF-8', 'auto');
90                 } else {
91                         // @TODO We need to find a fallback solution here
92                         $this->partialStub('Cannot find recode/mbstring extension!');
93                 } // END - if
94
95                 // Get an XML parser
96                 $xmlParser = xml_parser_create();
97
98                 // Force case-folding to on
99                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
100
101                 // Set UTF-8
102                 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
103
104                 // Get instance (we need this :( )
105                 $templateInstance = $this->getTemplateInstance();
106
107                 // Set object
108                 xml_set_object($xmlParser, $templateInstance);
109
110                 // Set handler call-backs
111                 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
112                 xml_set_character_data_handler($xmlParser, 'characterHandler');
113
114                 // Now parse the XML tree
115                 if (!xml_parse($xmlParser, $content)) {
116                         // Error found in XML!
117                         //* DEBUG: */ exit(__METHOD__ . ':<pre>'.htmlentities($content).'</pre>');
118                         throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
119                 } // END - if
120
121                 // Free the parser
122                 xml_parser_free($xmlParser);
123         }
124
125 }