cvs: pear /XML_Feed_Parser Parser.php /XML_Feed_Parser/tests errors.php

From: Date: Sun, 06 Nov 2005 22:36:59 +0000
Subject: cvs: pear /XML_Feed_Parser Parser.php /XML_Feed_Parser/tests errors.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-36119@lists.php.net to get a copy of this message
jystewart Sun Nov 6 17:36:59 2005 EDT Modified files: /pear/XML_Feed_Parser Parser.php /pear/XML_Feed_Parser/tests errors.php Log: Added tests for RSS2 identified by namespaces Added fallback to RSS2 where RSS version not specified http://cvs.php.net/diff.php/pear/XML_Feed_Parser/Parser.php?r1=1.6&r2=1.7&ty=u Index: pear/XML_Feed_Parser/Parser.php diff -u pear/XML_Feed_Parser/Parser.php:1.6 pear/XML_Feed_Parser/Parser.php:1.7 --- pear/XML_Feed_Parser/Parser.php:1.6 Sat Oct 22 13:57:06 2005 +++ pear/XML_Feed_Parser/Parser.php Sun Nov 6 17:36:56 2005 @@ -17,7 +17,7 @@ * @author James Stewart <james@jystewart.net> * @copyright 2005 James Stewart <james@jystewart.net> * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL - * @version CVS: $Id: Parser.php,v 1.6 2005/10/22 17:57:06 jystewart Exp $ + * @version CVS: $Id: Parser.php,v 1.7 2005/11/06 22:36:56 jystewart Exp $ * @link http://dev.jystewart.net/XML_Feed_Parser/ */ @@ -63,6 +63,15 @@ protected $idMappings = array(); /** + * A storage space for Namespace URIs. + * @var array + */ + private $feedNamespaces = array( + 'rss2' => array( + 'http://backend.userland.com/rss', + 'http://backend.userland.com/rss2', + 'http://blogs.law.harvard.edu/tech/rss')); + /** * Our constructor takes care of detecting feed types and instantiating * appropriate classes. For now we're going to treat Atom 0.3 as Atom 1.0 * but raise a warning. I do not intend to introduce full support for @@ -74,7 +83,9 @@ function __construct($feed, $strict = false) { $this->model = new DOMDocument; - $this->model->loadXML($feed); + if (! $this->model->loadXML($feed)) { + throw new XML_Feed_Parser_Exception("Invalid input: this is not valid XML"); + } /* detect feed type */ $doc_element = $this->model->documentElement; @@ -89,12 +100,13 @@ require_once 'Parser/AtomElement.php'; $class = 'XML_Feed_Parser_Atom'; trigger_error( - 'Atom 0.3 deprecated, using 1.0 parser which won\'t provide - all options', E_USER_WARNING); + 'Atom 0.3 deprecated, using 1.0 parser which won\'t provide ' . + 'all options', E_USER_WARNING); break; - case ($doc_element->hasChildNodes() and $doc_element->childNodes->length > 1 + case ($doc_element->namespaceURI == 'http://purl.org/rss/1.0/' or + ($doc_element->hasChildNodes() and $doc_element->childNodes->length > 1 and $doc_element->childNodes->item(1)->namespaceURI == - 'http://purl.org/rss/1.0/'): + 'http://purl.org/rss/1.0/')): require_once 'Parser/RSS1.php'; require_once 'Parser/RSS1Element.php'; $class = 'XML_Feed_Parser_RSS1'; @@ -106,13 +118,12 @@ require_once 'Parser/RSS09Element.php'; $class = 'XML_Feed_Parser_RSS09'; break; - case ($doc_element->tagName == 'rss' and - $doc_element->hasAttribute('version') and - $doc_element->getAttribute('version') == 2): - require_once 'Parser/RSS2.php'; - require_once 'Parser/RSS2Element.php'; - $class = 'XML_Feed_Parser_RSS2'; - break; + case ($doc_element->namespaceURI == + 'http://my.netscape.com/rdf/simple/0.9/'): + require_once 'Parser/RSS09.php'; + require_once 'Parser/RSS09Element.php'; + $class = 'XML_Feed_Parser_RSS09'; + break; case ($doc_element->tagName == 'rss' and $doc_element->hasAttribute('version') and $doc_element->getAttribute('version') == 0.91): @@ -133,6 +144,17 @@ require_once 'Parser/RSS2Element.php'; $class = 'XML_Feed_Parser_RSS2'; break; + case (in_array($doc_element->namespaceURI, $this->feedNamespaces['rss2']) + or $doc_element->tagName == 'rss'): + if (! $doc_element->hasAttribute('version') or + $doc_element->getAttribute('version') != 2) { + trigger_error('RSS version not specified. Parsing as RSS2.0', + E_USER_WARNING); + } + require_once 'Parser/RSS2.php'; + require_once 'Parser/RSS2Element.php'; + $class = 'XML_Feed_Parser_RSS2'; + break; default: throw new XML_Feed_Parser_Exception('Feed type unknown'); break; http://cvs.php.net/diff.php/pear/XML_Feed_Parser/tests/errors.php?r1=1.2&r2=1.3&ty=u Index: pear/XML_Feed_Parser/tests/errors.php diff -u pear/XML_Feed_Parser/tests/errors.php:1.2 pear/XML_Feed_Parser/tests/errors.php:1.3 --- pear/XML_Feed_Parser/tests/errors.php:1.2 Sat Oct 22 19:13:23 2005 +++ pear/XML_Feed_Parser/tests/errors.php Sun Nov 6 17:36:59 2005 @@ -1,6 +1,6 @@ <?php -require_once 'XML/Feed/Parser.php'; +require_once '../Parser.php'; require_once 'PHPUnit.php'; /** @@ -43,6 +43,26 @@ $this->assertTrue($e instanceof XML_Feed_Parser_Exception); } } + + function test_emptyInput() + { + $file = null; + try { + $feed = new XML_Feed_Parser($file); + } catch (Exception $e) { + $this->assertTrue($e instanceof XML_Feed_Parser_Exception); + } + } + + function test_nonXMLInput() + { + $file = "My string"; + try { + $feed = new XML_Feed_Parser($file); + } catch (Exception $e) { + $this->assertTrue($e instanceof XML_Feed_Parser_Exception); + } + } } $suite = new PHPUnit_TestSuite;

« previous php.pear.cvs (#36119) next »