Author: as
Date: Thu Nov  1 11:10:57 2007
New Revision: 6653

Log:
- Added better parsing of 'item' RSS1 feed elements.
- Added support for 'image' RSS1 feed element (only parsing for now).

Added:
    trunk/Feed/tests/rss1/regression/parse/optional/
    trunk/Feed/tests/rss1/regression/parse/optional/image/
    trunk/Feed/tests/rss1/regression/parse/optional/image/image.in
    trunk/Feed/tests/rss1/regression/parse/optional/image/image.out
    trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.in
    trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.out
    trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.in
    trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.out
Modified:
    trunk/Feed/src/processors/rss1.php
    trunk/Feed/src/tools/feed_tools.php

Modified: trunk/Feed/src/processors/rss1.php
==============================================================================
--- trunk/Feed/src/processors/rss1.php [iso-8859-1] (original)
+++ trunk/Feed/src/processors/rss1.php [iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -36,48 +36,45 @@
         'title'        => array( '#' => 'string' ),
         'link'         => array( '#' => 'string' ),
         'description'  => array( '#' => 'string' ),
-        'items'        => array( '#' => 'node',
+        'items'        => array( '#' => 'none',
                                  'ATTRIBUTES' => array( 'resource' => 'string' 
) ),
 
-        'image'        => array( '#' => 'string' ),
-        'textinput'    => array( '#' => 'string',
+        'image'        => array( '#'          => 'none',
+                                 'ATTRIBUTES' => array( 'about'   => 'string' 
),
+                              
+                                 'NODES'      => array(
+                                                   'title'        => array( 
'#' => 'string' ),
+                                                   'url'          => array( 
'#' => 'string' ),
+                                                   'link'         => array( 
'#' => 'string' ),
+
+                                                   'REQUIRED'     => array( 
'title', 'url', 'link' ),
+                                                   ), ),
+
+        // outside channel
+        'item'         => array( '#'          => 'none',
+                                 'ATTRIBUTES' => array( 'about'   => 'string',
+                                                        'resource'=> 'string', 
),
+
+                                 'NODES'      => array(
+                                                   'title'        => array( 
'#' => 'string' ),
+                                                   'link'         => array( 
'#' => 'string' ),
+
+                                                   'description'  => array( 
'#' => 'string' ),
+
+                                                   'REQUIRED'     => array( 
'title', 'link' ),
+                                                   'OPTIONAL'     => array( 
'description' ),
+                                                   ),
+                                 'MULTI'      => 'items' ),
+
+        'textInput'    => array( '#'          => 'string',
                                  'ATTRIBUTES' => array( 'resource' => 'string' 
) ),
 
-
-        'image'     => array( '#'          => 'node',
-                              'ATTRIBUTES' => array( 'about'   => 'string' ),
-                              
-                              'NODES'      => array(
-                                                'title'        => 'string',
-                                                'url'          => 'string',
-                                                'link'         => 'string',
-
-                                                'REQUIRED'     => array( 
'title', 'url', 'link' ),
-                                                ), ),
-
-        // outside channel
-        'item'      => array( '#'          => 'node',
-                              'ATTRIBUTES' => array( 'about'   => 'string',
-                                                     'resource'=> 'string', ),
-
-                              'NODES'      => array(
-                                                'title'        => array( '#' 
=> 'string' ),
-                                                'link'         => array( '#' 
=> 'string' ),
-
-                                                'description'  => array( '#' 
=> 'string' ),
-
-                                                'REQUIRED'     => array( 
'title', 'link' ),
-                                                'OPTIONAL'     => array( 
'description' ),
-                                                ),
-                              'MULTI'      => 'items' ),
-
-        'textInput' => array( '#'          => 'string',
-                              'ATTRIBUTES' => array( 'resource' => 'string' ) 
),
-
-        'REQUIRED'  => array( 'title', 'link', 'description', 'item' ),
-        'OPTIONAL'  => array( 'image', 'textinput' ),
-
-        'MULTI'     => array( 'items'      => 'item' ),
+        'REQUIRED'     => array( 'title', 'link', 'description', 'item' ),
+        'OPTIONAL'     => array( 'image', 'textinput' ),
+
+        'MULTI'        => array( 'items'      => 'item' ),
+        
+        'ELEMENTS_MAP' => array( 'textInput'  => 'textinput' ),
 
         );
 
@@ -350,24 +347,16 @@
                         {
                             $resource = ezcFeedTools::getAttribute( $el, 
'resource' );
 
-                            if ( $resource !== null )
-                            {
-                                foreach ( $items as $item )
-                                {
-                                    $about = ezcFeedTools::getAttribute( 
$item, 'about' );
-                                    if ( $about === $resource )
-                                    {
-                                        $element = $feed->add( 'item' );
-                                        $this->parseItem( $feed, $element, 
$item );
-                                        break;
-                                    }
-                                }
-                            }
-
+                            $item = ezcFeedTools::getNodeByAttribute( 
$xml->documentElement, 'item', 'about', $resource );
+                            $element = $feed->add( 'item' );
+                            $this->parseItem( $feed, $element, $item );
                         }
                         break;
 
                     case 'image':
+                        $resource = ezcFeedTools::getAttribute( $channelChild, 
'resource' );
+
+                        $image = ezcFeedTools::getNodeByAttribute( 
$xml->documentElement, 'image', 'about', $resource );
                         $this->parseImage( $feed, $image );
                         break;
 
@@ -434,28 +423,31 @@
      * @param DOMElement $xml The XML element object to parse
      * @ignore
      */
-    protected function parseImage( ezcFeed $feed, DOMElement $xml )
-    {
-        $feed->image = new ezcFeedElement( $this->schema->get( 'image' ) );
-        foreach ( $xml->childNodes as $itemChild )
-        {
-            if ( $itemChild->nodeType == XML_ELEMENT_NODE )
-            {
-                $tagName = $itemChild->tagName;
-                switch ( $tagName )
-                {
-                    case 'title':
-                    case 'link':
-                    case 'url':
-                        $feed->image->$tagName = $itemChild->textContent;
-                        break;
-                }
-            }
-        }
-
-        foreach ( ezcFeedTools::getAttributes( $xml ) as $key => $value )
-        {
-            $feed->image->$key = $value;
+    protected function parseImage( ezcFeed $feed, DOMElement $xml = null )
+    {
+        $image = $feed->add( 'image' );
+        if ( $xml !== null )
+        {
+            foreach ( $xml->childNodes as $itemChild )
+            {
+                if ( $itemChild->nodeType == XML_ELEMENT_NODE )
+                {
+                    $tagName = $itemChild->tagName;
+                    switch ( $tagName )
+                    {
+                        case 'title':
+                        case 'link':
+                        case 'url':
+                            $image->$tagName = $itemChild->textContent;
+                            break;
+                    }
+                }
+            }
+
+            foreach ( ezcFeedTools::getAttributes( $xml ) as $key => $value )
+            {
+                $image->$key = $value;
+            }
         }
     }
 }

Modified: trunk/Feed/src/tools/feed_tools.php
==============================================================================
--- trunk/Feed/src/tools/feed_tools.php [iso-8859-1] (original)
+++ trunk/Feed/src/tools/feed_tools.php [iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -63,6 +63,36 @@
     }
 
     /**
+     * Returns a DOMNode child of $parent with name $nodeName and which has an
+     * attribute $attribute with the value $value. Returns null if no such node
+     * is found.
+     *
+     * @param DOMNode $parent The XML parent node
+     * @param string $nodeName The node name to find
+     * @param string $attribute The attribute of the node
+     * @param mixed $value The value of the attribute
+     * @return DOMNode
+     */
+    public static function getNodeByAttribute( DOMNode $parent, $nodeName, 
$attribute, $value )
+    {
+        $result = null;
+        $nodes = $parent->getElementsByTagName( $nodeName );
+
+        foreach ( $nodes as $node )
+        {
+            $nodeAttribute = ezcFeedTools::getAttribute( $node, $attribute );
+            if ( $nodeAttribute !== null
+                 && $nodeAttribute === $value )
+            {
+                $result = $node;
+                break;
+            }
+        }
+
+        return $result;
+    }
+
+    /**
      * Returns the provided $date in timestamp format.
      *
      * @param mixed $date A date

Added: trunk/Feed/tests/rss1/regression/parse/optional/image/image.in
==============================================================================
--- trunk/Feed/tests/rss1/regression/parse/optional/image/image.in (added)
+++ trunk/Feed/tests/rss1/regression/parse/optional/image/image.in [iso-8859-1] 
Thu Nov  1 11:10:57 2007
@@ -1,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 
xmlns="http://purl.org/rss/1.0/";>
+  <channel>
+    <title>Feed title</title>
+       <link>Feed link</link>
+    <description>Feed description</description>
+    <image rdf:resource="Image link"/>
+  </channel>
+</rdf:RDF>

Added: trunk/Feed/tests/rss1/regression/parse/optional/image/image.out
==============================================================================
--- trunk/Feed/tests/rss1/regression/parse/optional/image/image.out (added)
+++ trunk/Feed/tests/rss1/regression/parse/optional/image/image.out 
[iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -1,0 +1,11 @@
+<?php
+$feed = new ezcFeed( 'rss1' );
+
+$feed->title = 'Feed title';
+$feed->link = 'Feed link';
+$feed->description = 'Feed description';
+
+$image = $feed->add( 'image' );
+
+return $feed;
+?>

Added: trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.in
==============================================================================
--- trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.in 
(added)
+++ trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.in 
[iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -1,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 
xmlns="http://purl.org/rss/1.0/";>
+  <channel>
+    <title>Feed title</title>
+       <link>Feed link</link>
+    <description>Feed description</description>
+    <image rdf:resource="Image link"/>
+  </channel>
+
+  <image rdf:about="Image link">
+    <title>Image title</title>
+    <link>Image link</link>
+    <url>Image url</url>
+  </image>
+</rdf:RDF>

Added: trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.out
==============================================================================
--- trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.out 
(added)
+++ trunk/Feed/tests/rss1/regression/parse/optional/image/image_complete.out 
[iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -1,0 +1,15 @@
+<?php
+$feed = new ezcFeed( 'rss1' );
+
+$feed->title = 'Feed title';
+$feed->link = 'Feed link';
+$feed->description = 'Feed description';
+
+$image = $feed->add( 'image' );
+$image->about = 'Image link';
+$image->title = 'Image title';
+$image->link = 'Image link';
+$image->url = 'Image url';
+
+return $feed;
+?>

Added: trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.in
==============================================================================
--- trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.in (added)
+++ trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.in 
[iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -1,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 
xmlns="http://purl.org/rss/1.0/";>
+  <channel>
+    <title>Feed title</title>
+       <link>Feed link</link>
+    <description>Feed description</description>
+    <image/>
+  </channel>
+</rdf:RDF>

Added: trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.out
==============================================================================
--- trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.out 
(added)
+++ trunk/Feed/tests/rss1/regression/parse/optional/image/image_empty.out 
[iso-8859-1] Thu Nov  1 11:10:57 2007
@@ -1,0 +1,11 @@
+<?php
+$feed = new ezcFeed( 'rss1' );
+
+$feed->title = 'Feed title';
+$feed->link = 'Feed link';
+$feed->description = 'Feed description';
+
+$image = $feed->add( 'image' );
+
+return $feed;
+?>


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to