TYPO3 CMS  TYPO3_8-7
MirrorXmlPullParser.php
Go to the documentation of this file.
1 <?php
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 
24 {
30  public function __construct()
31  {
32  $this->requiredPhpExtensions = 'xmlreader';
33  }
34 
38  protected function createParser()
39  {
40  $this->objXml = new \XMLReader();
41  }
42 
49  public function parseXml($file)
50  {
51  $this->createParser();
52  if (!(is_object($this->objXml) && get_class($this->objXml) === 'XMLReader')) {
53  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
54  }
55  if ($this->objXml->open($file, 'utf-8') === false) {
56  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640893);
57  }
58  while ($this->objXml->read()) {
59  if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
60  $this->startElement($this->objXml->name);
61  } else {
62  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
63  $this->endElement($this->objXml->name);
64  } else {
65  continue;
66  }
67  }
68  }
69  $this->objXml->close();
70  }
71 
78  protected function startElement($elementName)
79  {
80  switch ($elementName) {
81  case 'title':
82  $this->title = $this->getElementValue($elementName);
83  break;
84  case 'host':
85  $this->host = $this->getElementValue($elementName);
86  break;
87  case 'path':
88  $this->path = $this->getElementValue($elementName);
89  break;
90  case 'country':
91  $this->country = $this->getElementValue($elementName);
92  break;
93  case 'name':
94  $this->sponsorname = $this->getElementValue($elementName);
95  break;
96  case 'link':
97  $this->sponsorlink = $this->getElementValue($elementName);
98  break;
99  case 'logo':
100  $this->sponsorlogo = $this->getElementValue($elementName);
101  break;
102  default:
103  // Do nothing
104  }
105  }
106 
113  protected function endElement($elementName)
114  {
115  switch ($elementName) {
116  case 'mirror':
117  $this->notify();
118  $this->resetProperties();
119  break;
120  default:
121  // Do nothing
122  }
123  }
124 
135  protected function getElementValue(&$elementName)
136  {
137  $value = null;
138  if (!$this->objXml->isEmptyElement) {
139  $value = '';
140  while ($this->objXml->read()) {
141  if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
142  $value .= $this->objXml->value;
143  } else {
144  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
145  break;
146  }
147  }
148  }
149  }
150  return $value;
151  }
152 }