2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser
;
4 /***************************************************************
7 * (c) 2010 Marcus Krause <marcus#exp2010@t3sec.info>
8 * Steffen Kamper <info@sk-typo3.de>
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Parser for TYPO3's mirrors.xml file.
30 * Depends on PHP ext/xmlreader which should be available
33 * @author Marcus Krause <marcus#exp2010@t3sec.info>
34 * @autho Steffen Kamper <info@sk-typo3.de>
36 * @package Extension Manager
37 * @subpackage Utility/Parser
39 class MirrorXmlPullParser
extends \TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlAbstractParser
implements \SplSubject
{
42 * Keeps list of attached observers.
46 protected $observers = array();
53 public function __construct() {
54 $this->requiredPhpExtensions
= 'xmlreader';
55 if ($this->isAvailable()) {
56 $this->objXml
= new \
XMLReader();
61 * Method parses an extensions.xml file.
63 * @param string $file file resource, typically a stream
65 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
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);
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);
74 while ($this->objXml
->read()) {
75 if ($this->objXml
->nodeType
== \XMLReader
::ELEMENT
) {
76 $this->startElement($this->objXml
->name
);
78 if ($this->objXml
->nodeType
== \XMLReader
::END_ELEMENT
) {
79 $this->endElement($this->objXml
->name
);
85 $this->objXml
->close();
89 * Method is invoked when parser accesses start tag of an element.
91 * @param string $elementName element name at parser's current position
95 protected function startElement($elementName) {
96 switch ($elementName) {
98 $this->title
= $this->getElementValue($elementName);
101 $this->host
= $this->getElementValue($elementName);
104 $this->path
= $this->getElementValue($elementName);
107 $this->country
= $this->getElementValue($elementName);
110 $this->sponsorname
= $this->getElementValue($elementName);
113 $this->sponsorlink
= $this->getElementValue($elementName);
116 $this->sponsorlogo
= $this->getElementValue($elementName);
124 * Method is invoked when parser accesses end tag of an element.
126 * @param string $elementName element name at parser's current position
128 * @see startElement()
130 protected function endElement($elementName) {
131 switch ($elementName) {
134 $this->resetProperties();
142 * Method returns the value of an element at XMLReader's current
145 * Method will read until it finds the end of the given element.
146 * If element has no value, method returns NULL.
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
151 protected function getElementValue(&$elementName) {
153 if (!$this->objXml
->isEmptyElement
) {
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
;
159 if ($this->objXml
->nodeType
== \XMLReader
::END_ELEMENT
&& $this->objXml
->name
=== $elementName) {
169 * Method attaches an observer.
171 * @param SplObserver $observer an observer to attach
173 * @see $observers, detach(), notify()
175 public function attach(\SplObserver
$observer) {
176 $this->observers
[] = $observer;
180 * Method detaches an attached observer
182 * @param SplObserver $observer an observer to detach
184 * @see $observers, attach(), notify()
186 public function detach(\SplObserver
$observer) {
187 $key = array_search($observer, $this->observers
, TRUE);
188 if (!($key === FALSE)) {
189 unset($this->observers
[$key]);
194 * Method notifies attached observers.
198 * @see $observers, attach(), detach()
200 public function notify() {
201 foreach ($this->observers
as $observer) {
202 $observer->update($this);