TYPO3 API  SVNRelease
class.tx_em_parser_mirrorxmlpullparser.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010 Marcus Krause <marcus#exp2010@t3sec.info>
00006  *         Steffen Kamper <info@sk-typo3.de>
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *
00018  *  This script is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  *  GNU General Public License for more details.
00022  *
00023  *  This copyright notice MUST APPEAR in all copies of the script!
00024  ***************************************************************/
00025 /**
00026  * class.tx_em_parser_mirrorxmlpullparser.php
00027  *
00028  * Module: Extension manager - mirrors.xml pull-parser
00029  *
00030  * $Id: class.tx_em_parser_mirrorxmlpullparser.php 1913 2010-02-21 15:47:37Z mkrause $
00031  *
00032  * @author  Marcus Krause <marcus#exp2010@t3sec.info>
00033  * @author  Steffen Kamper <info@sk-typo3.de>
00034  */
00035 
00036 
00037 /**
00038  * Parser for TYPO3's mirrors.xml file.
00039  *
00040  * Depends on PHP ext/xmlreader which should be available
00041  * with PHP >= 5.1.0.
00042  *
00043  * @author    Marcus Krause <marcus#exp2010@t3sec.info>
00044  * @author    Steffen Kamper <info@sk-typo3.de>
00045  *
00046  * @since      2010-02-19
00047  * @package  TYPO3
00048  * @subpackage  EM
00049  */
00050 class tx_em_Parser_MirrorXmlPullParser extends tx_em_Parser_MirrorXmlAbstractParser implements SplSubject {
00051 
00052 
00053     /**
00054      * Keeps list of attached observers.
00055      *
00056      * @var  array
00057      */
00058     protected $observers = array();
00059 
00060 
00061     /**
00062      * Class constructor.
00063      *
00064      * @access  public
00065      * @return  void
00066      */
00067     function __construct() {
00068         $this->requiredPHPExt = 'xmlreader';
00069 
00070         if ($this->isAvailable()) {
00071             $this->objXML = new XMLReader();
00072         }
00073     }
00074 
00075     /**
00076      * Method parses an extensions.xml file.
00077      *
00078      * @access  public
00079      * @param   string  $file  file resource, typically a stream
00080      * @return  void
00081      * @throws  em_mirrorxml_Exception  in case of XML parser errors
00082      */
00083     public function parseXML($file) {
00084         if (!(is_object($this->objXML) && (get_class($this->objXML) == 'XMLReader'))) {
00085             $this->throwException('Unable to create XML parser.');
00086         }
00087         $this->objXML->open($file, 'utf-8') || $this->throwException(sprintf('Unable to open file ressource %s.', htmlspecialchars($file)));
00088 
00089         while ($this->objXML->read()) {
00090 
00091             if ($this->objXML->nodeType == XMLReader::ELEMENT) {
00092                 $this->startElement($this->objXML->name);
00093             } else {
00094                 if ($this->objXML->nodeType == XMLReader::END_ELEMENT) {
00095                     $this->endElement($this->objXML->name);
00096                 } else {
00097                     continue;
00098                 }
00099             }
00100         }
00101         $this->objXML->close();
00102     }
00103 
00104     /**
00105      * Method is invoked when parser accesses start tag of an element.
00106      *
00107      * @access  protected
00108      * @param   string   $elementName  element name at parser's current position
00109      * @return  void
00110      * @see  endElement()
00111      */
00112     protected function startElement($elementName) {
00113         switch ($elementName) {
00114             case 'title':
00115                 $this->title = $this->getElementValue($elementName);
00116                 break;
00117             case 'host':
00118                 $this->host = $this->getElementValue($elementName);
00119                 break;
00120             case 'path':
00121                 $this->path = $this->getElementValue($elementName);
00122                 break;
00123             case 'country':
00124                 $this->country = $this->getElementValue($elementName);
00125                 break;
00126             case 'name':
00127                 $this->sponsorname = $this->getElementValue($elementName);
00128                 break;
00129             case 'link':
00130                 $this->sponsorlink = $this->getElementValue($elementName);
00131                 break;
00132             case 'logo':
00133                 $this->sponsorlogo = $this->getElementValue($elementName);
00134                 break;
00135         }
00136     }
00137 
00138     /**
00139      * Method is invoked when parser accesses end tag of an element.
00140      *
00141      * @access  protected
00142      * @param   string   $elementName  element name at parser's current position
00143      * @return  void
00144      * @see  startElement()
00145      */
00146     protected function endElement($elementName) {
00147         switch ($elementName) {
00148             case 'mirror':
00149                 $this->notify();
00150                 $this->resetProperties();
00151                 break;
00152         }
00153     }
00154 
00155     /**
00156      * Method returns the value of an element at XMLReader's current
00157      * position.
00158      *
00159      * Method will read until it finds the end of the given element.
00160      * If element has no value, method returns NULL.
00161      *
00162      * @access  protected
00163      * @param   string   $elementName  name of element to retrieve it's value from
00164      * @return  string   an element's value if it has a value, otherwise NULL
00165      */
00166     protected function getElementValue(&$elementName) {
00167         $value = NULL;
00168         if (!$this->objXML->isEmptyElement) {
00169             $value = '';
00170             while ($this->objXML->read()) {
00171                 if ($this->objXML->nodeType == XMLReader::TEXT
00172                         || $this->objXML->nodeType == XMLReader::CDATA
00173                         || $this->objXML->nodeType == XMLReader::WHITESPACE
00174                         || $this->objXML->nodeType == XMLReader::SIGNIFICANT_WHITESPACE) {
00175                     $value .= $this->objXML->value;
00176                 } else {
00177                     if ($this->objXML->nodeType == XMLReader::END_ELEMENT
00178                             && $this->objXML->name === $elementName) {
00179                         break;
00180                     }
00181                 }
00182             }
00183         }
00184         return $value;
00185     }
00186 
00187     /**
00188      * Method attaches an observer.
00189      *
00190      * @access  public
00191      * @param   SplObserver  $observer  an observer to attach
00192      * @return  void
00193      * @see  $observers, detach(), notify()
00194      */
00195     public function attach(SplObserver $observer) {
00196         $this->observers[] = $observer;
00197     }
00198 
00199     /**
00200      * Method detaches an attached observer
00201      *
00202      * @access  public
00203      * @param   SplObserver  $observer  an observer to detach
00204      * @return  void
00205      * @see  $observers, attach(), notify()
00206      */
00207     public function detach(SplObserver $observer) {
00208         $key = array_search($observer, $this->observers, true);
00209         if (!($key === false)) {
00210             unset($this->observers[$key]);
00211         }
00212     }
00213 
00214     /**
00215      * Method notifies attached observers.
00216      *
00217      * @access  public
00218      * @return  void
00219      * @see  $observers, attach(), detach()
00220      */
00221     public function notify() {
00222         foreach ($this->observers as $observer) {
00223             $observer->update($this);
00224         }
00225     }
00226 }
00227 
00228 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/parser/class.tx_em_parser_mirrorxmlpullparser.php'])) {
00229     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/parser/class.tx_em_parser_mirrorxmlpullparser.php']);
00230 }
00231 
00232 ?>