FeedParser.php
6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Feed;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Feed\Parser\NamespaceParserInterface;
/**
* Feed Parser class.
*
* @since 3.1.4
*/
abstract class FeedParser
{
/**
* The feed element name for the entry elements.
*
* @var string
* @since 3.1.4
*/
protected $entryElementName = 'entry';
/**
* Array of NamespaceParserInterface objects
*
* @var array
* @since 3.1.4
*/
protected $namespaces = array();
/**
* The XMLReader stream object for the feed.
*
* @var \XMLReader
* @since 3.1.4
*/
protected $stream;
/**
* Constructor.
*
* @param \XMLReader $stream The XMLReader stream object for the feed.
*
* @since 3.1.4
*/
public function __construct(\XMLReader $stream)
{
$this->stream = $stream;
}
/**
* Method to parse the feed into a JFeed object.
*
* @return Feed
*
* @since 3.1.4
*/
public function parse()
{
$feed = new Feed;
// Detect the feed version.
$this->initialise();
// Let's get this party started...
do
{
// Expand the element for processing.
$el = new \SimpleXMLElement($this->stream->readOuterXml());
// Get the list of namespaces used within this element.
$ns = $el->getNamespaces(true);
// Get an array of available namespace objects for the element.
$namespaces = array();
foreach ($ns as $prefix => $uri)
{
// Ignore the empty namespace prefix.
if (empty($prefix))
{
continue;
}
// Get the necessary namespace objects for the element.
$namespace = $this->fetchNamespace($prefix);
if ($namespace)
{
$namespaces[] = $namespace;
}
}
// Process the element.
$this->processElement($feed, $el, $namespaces);
// Skip over this element's children since it has been processed.
$this->moveToClosingElement();
}
while ($this->moveToNextElement());
return $feed;
}
/**
* Method to register a namespace handler object.
*
* @param string $prefix The XML namespace prefix for which to register the namespace object.
* @param NamespaceParserInterface $namespace The namespace object to register.
*
* @return JFeed
*
* @since 3.1.4
*/
public function registerNamespace($prefix, NamespaceParserInterface $namespace)
{
$this->namespaces[$prefix] = $namespace;
return $this;
}
/**
* Method to initialise the feed for parsing. If child parsers need to detect versions or other
* such things this is where you'll want to implement that logic.
*
* @return void
*
* @since 3.1.4
*/
abstract protected function initialise();
/**
* Method to parse a specific feed element.
*
* @param Feed $feed The Feed object being built from the parsed feed.
* @param \SimpleXMLElement $el The current XML element object to handle.
* @param array $namespaces The array of relevant namespace objects to process for the element.
*
* @return void
*
* @since 3.1.4
*/
protected function processElement(Feed $feed, \SimpleXMLElement $el, array $namespaces)
{
// Build the internal method name.
$method = 'handle' . ucfirst($el->getName());
// If we are dealing with an item then it is feed entry time.
if ($el->getName() == $this->entryElementName)
{
// Create a new feed entry for the item.
$entry = new FeedEntry;
// First call the internal method.
$this->processFeedEntry($entry, $el);
foreach ($namespaces as $namespace)
{
if ($namespace instanceof NamespaceParserInterface)
{
$namespace->processElementForFeedEntry($entry, $el);
}
}
// Add the new entry to the feed.
$feed->addEntry($entry);
return;
}
// Otherwise we treat it like any other element.
// First call the internal method.
if (is_callable(array($this, $method)))
{
$this->$method($feed, $el);
}
foreach ($namespaces as $namespace)
{
if ($namespace instanceof NamespaceParserInterface)
{
$namespace->processElementForFeed($feed, $el);
}
}
}
/**
* Method to get a namespace object for a given namespace prefix.
*
* @param string $prefix The XML prefix for which to fetch the namespace object.
*
* @return mixed NamespaceParserInterface or false if none exists.
*
* @since 3.1.4
*/
protected function fetchNamespace($prefix)
{
if (isset($this->namespaces[$prefix]))
{
return $this->namespaces[$prefix];
}
$className = get_class($this) . ucfirst($prefix);
if (class_exists($className))
{
$this->namespaces[$prefix] = new $className;
return $this->namespaces[$prefix];
}
return false;
}
/**
* Method to move the stream parser to the next XML element node.
*
* @param string $name The name of the element for which to move the stream forward until is found.
*
* @return boolean True if the stream parser is on an XML element node.
*
* @since 3.1.4
*/
protected function moveToNextElement($name = null)
{
// Only keep looking until the end of the stream.
while ($this->stream->read())
{
// As soon as we get to the next ELEMENT node we are done.
if ($this->stream->nodeType == \XMLReader::ELEMENT)
{
// If we are looking for a specific name make sure we have it.
if (isset($name) && ($this->stream->name != $name))
{
continue;
}
return true;
}
}
return false;
}
/**
* Method to move the stream parser to the closing XML node of the current element.
*
* @return void
*
* @since 3.1.4
* @throws \RuntimeException If the closing tag cannot be found.
*/
protected function moveToClosingElement()
{
// If we are on a self-closing tag then there is nothing to do.
if ($this->stream->isEmptyElement)
{
return;
}
// Get the name and depth for the current node so that we can match the closing node.
$name = $this->stream->name;
$depth = $this->stream->depth;
// Only keep looking until the end of the stream.
while ($this->stream->read())
{
// If we have an END_ELEMENT node with the same name and depth as the node we started with we have a bingo. :-)
if (($this->stream->name == $name) && ($this->stream->depth == $depth) && ($this->stream->nodeType == \XMLReader::END_ELEMENT))
{
return;
}
}
throw new \RuntimeException('Unable to find the closing XML node.');
}
}