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