TYPO3 CMS  TYPO3_7-6
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 
25 {
31  public function __construct()
32  {
33  $this->requiredPhpExtensions = 'xmlreader';
34  }
35 
41  protected function createParser()
42  {
43  $this->objXml = new \XMLReader();
44  }
45 
53  public function parseXml($file)
54  {
55  $this->createParser();
56  if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
57  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
58  }
59  if ($this->objXml->open($file, 'utf-8') === false) {
60  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640893);
61  }
62  while ($this->objXml->read()) {
63  if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
64  $this->startElement($this->objXml->name);
65  } else {
66  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
67  $this->endElement($this->objXml->name);
68  } else {
69  continue;
70  }
71  }
72  }
73  $this->objXml->close();
74  }
75 
83  protected function startElement($elementName)
84  {
85  switch ($elementName) {
86  case 'title':
87  $this->title = $this->getElementValue($elementName);
88  break;
89  case 'host':
90  $this->host = $this->getElementValue($elementName);
91  break;
92  case 'path':
93  $this->path = $this->getElementValue($elementName);
94  break;
95  case 'country':
96  $this->country = $this->getElementValue($elementName);
97  break;
98  case 'name':
99  $this->sponsorname = $this->getElementValue($elementName);
100  break;
101  case 'link':
102  $this->sponsorlink = $this->getElementValue($elementName);
103  break;
104  case 'logo':
105  $this->sponsorlogo = $this->getElementValue($elementName);
106  break;
107  default:
108  // Do nothing
109  }
110  }
111 
119  protected function endElement($elementName)
120  {
121  switch ($elementName) {
122  case 'mirror':
123  $this->notify();
124  $this->resetProperties();
125  break;
126  default:
127  // Do nothing
128  }
129  }
130 
141  protected function getElementValue(&$elementName)
142  {
143  $value = null;
144  if (!$this->objXml->isEmptyElement) {
145  $value = '';
146  while ($this->objXml->read()) {
147  if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
148  $value .= $this->objXml->value;
149  } else {
150  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
151  break;
152  }
153  }
154  }
155  }
156  return $value;
157  }
158 }