Renamed Registry -> GenericRegistry to make it clear that this registry does
[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  * @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
18  *
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.
23  *
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.
28  *
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/>.
31  */
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;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of the class Parser and prepares it for usage
50          *
51          * @param       $templateInstance       A CompileableTemplate instance
52          * @return      $parserInstance         An instance of this parser
53          */
54         public static final function createXmlParser (CompileableTemplate $templateInstance) {
55                 // Get a new instance
56                 $parserInstance = new XmlParser();
57
58                 // Set the template instance
59                 $parserInstance->setTemplateInstance($templateInstance);
60
61                 // Return the prepared instance
62                 return $parserInstance;
63         }
64
65         /**
66          * Parses the given XML content
67          *
68          * @param       $content        Valid XML content
69          * @return      void
70          * @throws      XmlParserException      If an XML error was found
71          */
72         public function parseXmlContent ($content) {
73                 // Convert all to UTF8
74                 if (empty($content)) {
75                         // No empty content!
76                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Empty content! Backtrace: <pre>');
77                         debug_print_backtrace();
78                         self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('</pre>');
79                         exit();
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');
86                 } else {
87                         // @TODO We need to find a fallback solution here
88                         $this->partialStub('Cannot find recode/mbstring extension!');
89                 } // END - if
90
91                 // Get an XML parser
92                 $xmlParser = xml_parser_create();
93
94                 // Force case-folding to on
95                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
96
97                 // Set UTF-8
98                 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
99
100                 // Get instance (we need this :( )
101                 $templateInstance = $this->getTemplateInstance();
102
103                 // Set object
104                 xml_set_object($xmlParser, $templateInstance);
105
106                 // Set handler call-backs
107                 xml_set_element_handler($xmlParser, 'startElement', 'finishElement');
108                 xml_set_character_data_handler($xmlParser, 'characterHandler');
109
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);
115                 } // END - if
116
117                 // Free the parser
118                 xml_parser_free($xmlParser);
119         }
120
121 }