ce4c4b197b22e63dfdacc57de19ec9d9febb91b6
[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-2013 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 * @author Steffen Kamper <info@sk-typo3.de>
35 * @sincer 2010-02-19
36 */
37 class MirrorXmlPullParser extends AbstractMirrorXmlParser {
38
39 /**
40 * Class constructor.
41 *
42 * @access public
43 */
44 public function __construct() {
45 $this->requiredPhpExtensions = 'xmlreader';
46 }
47
48 /**
49 * Create required parser
50 *
51 * @return void
52 */
53 protected function createParser() {
54 $this->objXml = new \XMLReader();
55 }
56
57 /**
58 * Method parses an extensions.xml file.
59 *
60 * @param string $file file resource, typically a stream
61 * @return void
62 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
63 */
64 public function parseXml($file) {
65 $this->createParser();
66 if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
67 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
68 }
69 if ($this->objXml->open($file, 'utf-8') === FALSE) {
70 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', htmlspecialchars($file)), 1342640893);
71 }
72 while ($this->objXml->read()) {
73 if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
74 $this->startElement($this->objXml->name);
75 } else {
76 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
77 $this->endElement($this->objXml->name);
78 } else {
79 continue;
80 }
81 }
82 }
83 $this->objXml->close();
84 }
85
86 /**
87 * Method is invoked when parser accesses start tag of an element.
88 *
89 * @param string $elementName element name at parser's current position
90 * @return void
91 * @see endElement()
92 */
93 protected function startElement($elementName) {
94 switch ($elementName) {
95 case 'title':
96 $this->title = $this->getElementValue($elementName);
97 break;
98 case 'host':
99 $this->host = $this->getElementValue($elementName);
100 break;
101 case 'path':
102 $this->path = $this->getElementValue($elementName);
103 break;
104 case 'country':
105 $this->country = $this->getElementValue($elementName);
106 break;
107 case 'name':
108 $this->sponsorname = $this->getElementValue($elementName);
109 break;
110 case 'link':
111 $this->sponsorlink = $this->getElementValue($elementName);
112 break;
113 case 'logo':
114 $this->sponsorlogo = $this->getElementValue($elementName);
115 break;
116 default:
117 // Do nothing
118 }
119 }
120
121 /**
122 * Method is invoked when parser accesses end tag of an element.
123 *
124 * @param string $elementName element name at parser's current position
125 * @return void
126 * @see startElement()
127 */
128 protected function endElement($elementName) {
129 switch ($elementName) {
130 case 'mirror':
131 $this->notify();
132 $this->resetProperties();
133 break;
134 default:
135 // Do nothing
136 }
137 }
138
139 /**
140 * Method returns the value of an element at XMLReader's current
141 * position.
142 *
143 * Method will read until it finds the end of the given element.
144 * If element has no value, method returns NULL.
145 *
146 * @param string &$elementName name of element to retrieve it's value from
147 * @return string an element's value if it has a value, otherwise NULL
148 */
149 protected function getElementValue(&$elementName) {
150 $value = NULL;
151 if (!$this->objXml->isEmptyElement) {
152 $value = '';
153 while ($this->objXml->read()) {
154 if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
155 $value .= $this->objXml->value;
156 } else {
157 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
158 break;
159 }
160 }
161 }
162 }
163 return $value;
164 }
165 }