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