TYPO3 CMS  TYPO3_6-2
MirrorXmlPullParser.php
Go to the documentation of this file.
1 <?php
3 
27 
33  public function __construct() {
34  $this->requiredPhpExtensions = 'xmlreader';
35  }
36 
42  protected function createParser() {
43  $this->objXml = new \XMLReader();
44  }
45 
53  public function parseXml($file) {
54  $this->createParser();
55  if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
56  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
57  }
58  if ($this->objXml->open($file, 'utf-8') === FALSE) {
59  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', htmlspecialchars($file)), 1342640893);
60  }
61  while ($this->objXml->read()) {
62  if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
63  $this->startElement($this->objXml->name);
64  } else {
65  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
66  $this->endElement($this->objXml->name);
67  } else {
68  continue;
69  }
70  }
71  }
72  $this->objXml->close();
73  }
74 
82  protected function startElement($elementName) {
83  switch ($elementName) {
84  case 'title':
85  $this->title = $this->getElementValue($elementName);
86  break;
87  case 'host':
88  $this->host = $this->getElementValue($elementName);
89  break;
90  case 'path':
91  $this->path = $this->getElementValue($elementName);
92  break;
93  case 'country':
94  $this->country = $this->getElementValue($elementName);
95  break;
96  case 'name':
97  $this->sponsorname = $this->getElementValue($elementName);
98  break;
99  case 'link':
100  $this->sponsorlink = $this->getElementValue($elementName);
101  break;
102  case 'logo':
103  $this->sponsorlogo = $this->getElementValue($elementName);
104  break;
105  default:
106  // Do nothing
107  }
108  }
109 
117  protected function endElement($elementName) {
118  switch ($elementName) {
119  case 'mirror':
120  $this->notify();
121  $this->resetProperties();
122  break;
123  default:
124  // Do nothing
125  }
126  }
127 
138  protected function getElementValue(&$elementName) {
139  $value = NULL;
140  if (!$this->objXml->isEmptyElement) {
141  $value = '';
142  while ($this->objXml->read()) {
143  if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
144  $value .= $this->objXml->value;
145  } else {
146  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
147  break;
148  }
149  }
150  }
151  }
152  return $value;
153  }
154 }