[!!!][FEATURE] Flash message view helper add deferred rendering
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Parser / MirrorXmlPullParser.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Parser for TYPO3's mirrors.xml file.
19 *
20 * Depends on PHP ext/xmlreader which should be available
21 * with PHP >= 5.1.0.
22 *
23 * @author Marcus Krause <marcus#exp2010@t3sec.info>
24 * @author Steffen Kamper <info@sk-typo3.de>
25 * @sincer 2010-02-19
26 */
27 class MirrorXmlPullParser extends AbstractMirrorXmlParser {
28
29 /**
30 * Class constructor.
31 *
32 * @access public
33 */
34 public function __construct() {
35 $this->requiredPhpExtensions = 'xmlreader';
36 }
37
38 /**
39 * Create required parser
40 *
41 * @return void
42 */
43 protected function createParser() {
44 $this->objXml = new \XMLReader();
45 }
46
47 /**
48 * Method parses an extensions.xml file.
49 *
50 * @param string $file file resource, typically a stream
51 * @return void
52 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
53 */
54 public function parseXml($file) {
55 $this->createParser();
56 if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
57 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
58 }
59 if ($this->objXml->open($file, 'utf-8') === FALSE) {
60 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640893);
61 }
62 while ($this->objXml->read()) {
63 if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
64 $this->startElement($this->objXml->name);
65 } else {
66 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
67 $this->endElement($this->objXml->name);
68 } else {
69 continue;
70 }
71 }
72 }
73 $this->objXml->close();
74 }
75
76 /**
77 * Method is invoked when parser accesses start tag of an element.
78 *
79 * @param string $elementName element name at parser's current position
80 * @return void
81 * @see endElement()
82 */
83 protected function startElement($elementName) {
84 switch ($elementName) {
85 case 'title':
86 $this->title = $this->getElementValue($elementName);
87 break;
88 case 'host':
89 $this->host = $this->getElementValue($elementName);
90 break;
91 case 'path':
92 $this->path = $this->getElementValue($elementName);
93 break;
94 case 'country':
95 $this->country = $this->getElementValue($elementName);
96 break;
97 case 'name':
98 $this->sponsorname = $this->getElementValue($elementName);
99 break;
100 case 'link':
101 $this->sponsorlink = $this->getElementValue($elementName);
102 break;
103 case 'logo':
104 $this->sponsorlogo = $this->getElementValue($elementName);
105 break;
106 default:
107 // Do nothing
108 }
109 }
110
111 /**
112 * Method is invoked when parser accesses end tag of an element.
113 *
114 * @param string $elementName element name at parser's current position
115 * @return void
116 * @see startElement()
117 */
118 protected function endElement($elementName) {
119 switch ($elementName) {
120 case 'mirror':
121 $this->notify();
122 $this->resetProperties();
123 break;
124 default:
125 // Do nothing
126 }
127 }
128
129 /**
130 * Method returns the value of an element at XMLReader's current
131 * position.
132 *
133 * Method will read until it finds the end of the given element.
134 * If element has no value, method returns NULL.
135 *
136 * @param string &$elementName name of element to retrieve it's value from
137 * @return string an element's value if it has a value, otherwise NULL
138 */
139 protected function getElementValue(&$elementName) {
140 $value = NULL;
141 if (!$this->objXml->isEmptyElement) {
142 $value = '';
143 while ($this->objXml->read()) {
144 if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
145 $value .= $this->objXml->value;
146 } else {
147 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
148 break;
149 }
150 }
151 }
152 }
153 return $value;
154 }
155
156 }