TYPO3  7.6
MirrorXmlPushParser.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
29 {
33  protected $element;
34 
38  public function __construct()
39  {
40  $this->requiredPhpExtensions = 'xml';
41  }
42 
48  protected function createParser()
49  {
50  $this->objXml = xml_parser_create();
51  xml_set_object($this->objXml, $this);
52  }
53 
61  public function parseXml($file)
62  {
63  $this->createParser();
64  if (!is_resource($this->objXml)) {
65  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342641009);
66  }
67  // keep original character case of XML document
68  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
69  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
70  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
71  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
72  xml_set_character_data_handler($this->objXml, 'characterData');
73  if (!($fp = fopen($file, 'r'))) {
74  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342641010);
75  }
76  while ($data = fread($fp, 4096)) {
77  if (!xml_parse($this->objXml, $data, feof($fp))) {
78  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('XML error %s in line %u of file resource %s.', xml_error_string(xml_get_error_code($this->objXml)), xml_get_current_line_number($this->objXml), $file), 1342641011);
79  }
80  }
81  xml_parser_free($this->objXml);
82  }
83 
92  protected function startElement($parser, $elementName, $attrs)
93  {
94  switch ($elementName) {
95  default:
96  $this->element = $elementName;
97  }
98  }
99 
110  protected function endElement($parser, $elementName)
111  {
112  switch ($elementName) {
113  case 'mirror':
114  $this->notify();
115  $this->resetProperties();
116  break;
117  default:
118  $this->element = null;
119  }
120  }
121 
132  protected function characterData($parser, $data)
133  {
134  if (isset($this->element)) {
135  switch ($this->element) {
136  case 'title':
137  $this->title = $data;
138  break;
139  case 'host':
140  $this->host = $data;
141  break;
142  case 'path':
143  $this->path = $data;
144  break;
145  case 'country':
146  $this->country = $data;
147  break;
148  case 'name':
149  $this->sponsorname = $data;
150  break;
151  case 'link':
152  $this->sponsorlink = $data;
153  break;
154  case 'logo':
155  $this->sponsorlogo = $data;
156  break;
157  default:
158  // Do nothing
159  }
160  }
161  }
162 }