10 defined(
'JPATH_PLATFORM') or die;
27 protected $entryElementName =
'entry';
35 protected $namespaces = array();
52 public function __construct(XMLReader $stream)
54 $this->stream = $stream;
64 public function parse()
75 $el =
new SimpleXMLElement($this->stream->readOuterXml());
78 $ns = $el->getNamespaces(
true);
81 $namespaces = array();
83 foreach ($ns as $prefix => $uri)
92 $namespace = $this->fetchNamespace($prefix);
96 $namespaces[] = $namespace;
101 $this->processElement($feed, $el, $namespaces);
104 $this->moveToClosingElement();
106 while ($this->moveToNextElement());
123 $this->namespaces[$prefix] = $namespace;
136 abstract protected function initialise();
149 protected function processElement(
JFeed $feed, SimpleXMLElement $el, array $namespaces)
152 $method =
'handle' . ucfirst($el->getName());
155 if ($el->getName() == $this->entryElementName)
161 $this->processFeedEntry($entry, $el);
163 foreach ($namespaces as $namespace)
167 $namespace->processElementForFeedEntry($entry, $el);
178 if (is_callable(array($this, $method)))
180 $this->$method($feed, $el);
183 foreach ($namespaces as $namespace)
187 $namespace->processElementForFeed($feed, $el);
202 protected function fetchNamespace($prefix)
204 if (isset($this->namespaces[$prefix]))
206 return $this->namespaces[$prefix];
209 $className = get_class($this) . ucfirst($prefix);
211 if (class_exists($className))
213 $this->namespaces[$prefix] =
new $className;
215 return $this->namespaces[$prefix];
230 protected function moveToNextElement($name = null)
233 while ($this->stream->read())
236 if ($this->stream->nodeType == XMLReader::ELEMENT)
239 if (isset($name) && ($this->stream->name != $name))
259 protected function moveToClosingElement()
262 if ($this->stream->isEmptyElement)
268 $name = $this->stream->name;
269 $depth = $this->stream->depth;
272 while ($this->stream->read())
275 if (($this->stream->name == $name) && ($this->stream->depth == $depth) && ($this->stream->nodeType == XMLReader::END_ELEMENT))
281 throw new RuntimeException(
'Unable to find the closing XML node.');