6f57dca0bc16b4407ec3e0505d3786079d87b63c
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Parser / MirrorXmlPullParser.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 Marcus Krause <marcus#exp2010@t3sec.info>
8 * Steffen Kamper <info@sk-typo3.de>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Parser for TYPO3's mirrors.xml file.
29 *
30 * Depends on PHP ext/xmlreader which should be available
31 * with PHP >= 5.1.0.
32 *
33 * @author Marcus Krause <marcus#exp2010@t3sec.info>
34 * @autho Steffen Kamper <info@sk-typo3.de>
35 * @sincer 2010-02-19
36 * @package Extension Manager
37 * @subpackage Utility/Parser
38 */
39 class MirrorXmlPullParser extends \TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlAbstractParser implements SplSubject {
40
41 /**
42 * Keeps list of attached observers.
43 *
44 * @var SplObserver[]
45 */
46 protected $observers = array();
47
48 /**
49 * Class constructor.
50 *
51 * @access public
52 */
53 public function __construct() {
54 $this->requiredPhpExtensions = 'xmlreader';
55 if ($this->isAvailable()) {
56 $this->objXml = new \XMLReader();
57 }
58 }
59
60 /**
61 * Method parses an extensions.xml file.
62 *
63 * @param string $file file resource, typically a stream
64 * @return void
65 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
66 */
67 public function parseXml($file) {
68 if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
69 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
70 }
71 if ($this->objXml->open($file, 'utf-8') === FALSE) {
72 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', htmlspecialchars($file)), 1342640893);
73 }
74 while ($this->objXml->read()) {
75 if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
76 $this->startElement($this->objXml->name);
77 } else {
78 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
79 $this->endElement($this->objXml->name);
80 } else {
81 continue;
82 }
83 }
84 }
85 $this->objXml->close();
86 }
87
88 /**
89 * Method is invoked when parser accesses start tag of an element.
90 *
91 * @param string $elementName element name at parser's current position
92 * @return void
93 * @see endElement()
94 */
95 protected function startElement($elementName) {
96 switch ($elementName) {
97 case 'title':
98 $this->title = $this->getElementValue($elementName);
99 break;
100 case 'host':
101 $this->host = $this->getElementValue($elementName);
102 break;
103 case 'path':
104 $this->path = $this->getElementValue($elementName);
105 break;
106 case 'country':
107 $this->country = $this->getElementValue($elementName);
108 break;
109 case 'name':
110 $this->sponsorname = $this->getElementValue($elementName);
111 break;
112 case 'link':
113 $this->sponsorlink = $this->getElementValue($elementName);
114 break;
115 case 'logo':
116 $this->sponsorlogo = $this->getElementValue($elementName);
117 break;
118 default:
119
120 }
121 }
122
123 /**
124 * Method is invoked when parser accesses end tag of an element.
125 *
126 * @param string $elementName element name at parser's current position
127 * @return void
128 * @see startElement()
129 */
130 protected function endElement($elementName) {
131 switch ($elementName) {
132 case 'mirror':
133 $this->notify();
134 $this->resetProperties();
135 break;
136 default:
137
138 }
139 }
140
141 /**
142 * Method returns the value of an element at XMLReader's current
143 * position.
144 *
145 * Method will read until it finds the end of the given element.
146 * If element has no value, method returns NULL.
147 *
148 * @param string &$elementName name of element to retrieve it's value from
149 * @return string an element's value if it has a value, otherwise NULL
150 */
151 protected function getElementValue(&$elementName) {
152 $value = NULL;
153 if (!$this->objXml->isEmptyElement) {
154 $value = '';
155 while ($this->objXml->read()) {
156 if ((($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA) || $this->objXml->nodeType == \XMLReader::WHITESPACE) || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
157 $value .= $this->objXml->value;
158 } else {
159 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
160 break;
161 }
162 }
163 }
164 }
165 return $value;
166 }
167
168 /**
169 * Method attaches an observer.
170 *
171 * @param SplObserver $observer an observer to attach
172 * @return void
173 * @see $observers, detach(), notify()
174 */
175 public function attach(\SplObserver $observer) {
176 $this->observers[] = $observer;
177 }
178
179 /**
180 * Method detaches an attached observer
181 *
182 * @param SplObserver $observer an observer to detach
183 * @return void
184 * @see $observers, attach(), notify()
185 */
186 public function detach(\SplObserver $observer) {
187 $key = array_search($observer, $this->observers, TRUE);
188 if (!($key === FALSE)) {
189 unset($this->observers[$key]);
190 }
191 }
192
193 /**
194 * Method notifies attached observers.
195 *
196 * @access public
197 * @return void
198 * @see $observers, attach(), detach()
199 */
200 public function notify() {
201 foreach ($this->observers as $observer) {
202 $observer->update($this);
203 }
204 }
205
206 }
207
208
209 ?>