XML_Feed_Parser
[ class tree: XML_Feed_Parser ] [ index: XML_Feed_Parser ] [ all elements ]

Source for file Factory.php

Documentation is available at Factory.php

  1. <?php
  2. require_once 'Log.php';
  3. require_once 'Log/null.php';
  4. require_once 'XML/Feed/Parser/Sanitizer.php';
  5.  
  6. class XML_Feed_Parser_Factory {
  7.     /**
  8.      * A storage space for Namespace URIs.
  9.      * @var array 
  10.      */
  11.     private $feedNamespaces = array(
  12.         'rss2' => array(
  13.             'http://backend.userland.com/rss',
  14.             'http://backend.userland.com/rss2',
  15.             'http://blogs.law.harvard.edu/tech/rss'));
  16.  
  17.     public function __construct(Log $log = null{
  18.         if ($log === null{
  19.             $log = new Log_null(''''array()null);
  20.         }
  21.         $this->log $log;
  22.     }
  23.  
  24.    /**
  25.      * Detects feed types and instantiate appropriate objects.
  26.      *
  27.      * Our constructor takes care of detecting feed types and instantiating
  28.      * appropriate classes. For now we're going to treat Atom 0.3 as Atom 1.0
  29.      * but raise a warning. I do not intend to introduce full support for
  30.      * Atom 0.3 as it has been deprecated, but others are welcome to.
  31.      *
  32.      * @param    string    $feed    XML serialization of the feed
  33.      * @param    bool    $strict    Whether or not to validate the feed
  34.      * @param    bool    $suppressWarnings Trigger errors for deprecated feed types?
  35.      * @param    bool    $tidy    Whether or not to try and use the tidy library on input
  36.      */
  37.     function build(DOMDocument $model$feed$strict = false$suppressWarnings = false$tidy = false
  38.     {
  39.         $options = 0;
  40.         if ($suppressWarnings{
  41.             $options |= LIBXML_NOWARNING;
  42.             $options |= LIBXML_NOERROR;
  43.         }
  44.  
  45.         if (empty($feed)) {
  46.             throw new XML_Feed_Parser_Exception('Invalid input: file is empty');
  47.         }
  48.  
  49.         if ($model->loadXML($feed$options)) {
  50.             if (extension_loaded('tidy'&& $tidy{
  51.                 $tidy = new tidy;
  52.                 $tidy->parseString($feed
  53.                     array('input-xml' => true'output-xml' => true));
  54.                 $tidy->cleanRepair();
  55.                 if ($model->loadXML((string) $tidy)) {
  56.                     throw new XML_Feed_Parser_Exception('Invalid input: this is not ' .
  57.                         'valid XML');
  58.                 }
  59.             else {
  60.                 throw new XML_Feed_Parser_Exception('Invalid input: this is not valid XML');
  61.             }
  62.         }
  63.  
  64.  
  65.         /* detect feed type */
  66.         $doc_element $model->documentElement;
  67.  
  68.  
  69.         $class $this->determineClass($doc_element$suppressWarnings);
  70.  
  71.         /* Instantiate feed object */
  72.         $feed = new $class($model$strict);
  73.         $feed->setSanitizer(new XML_Feed_Parser_Unsafe_Sanitizer());
  74.  
  75.         return $feed;
  76.     }
  77.  
  78.     public function determineClass($doc_element$suppressWarnings = false
  79.     {
  80.         switch (true
  81.             case ($doc_element->namespaceURI == 'http://www.w3.org/2005/Atom'):
  82.                 require_once 'XML/Feed/Parser/Atom.php';
  83.                 require_once 'XML/Feed/Parser/AtomElement.php';
  84.                 $class 'XML_Feed_Parser_Atom';
  85.                 break;
  86.             case ($doc_element->namespaceURI == 'http://purl.org/atom/ns#'):
  87.                 require_once 'XML/Feed/Parser/Atom.php';
  88.                 require_once 'XML/Feed/Parser/AtomElement.php';
  89.                 $class 'XML_Feed_Parser_Atom';
  90.  
  91.                 $this->log->warning('Atom 0.3 deprecated, using 1.0 parser which won\'t provide ' .
  92.                     'all options');
  93.                 break;
  94.             case ($doc_element->namespaceURI == 'http://purl.org/rss/1.0/' || 
  95.                 ($doc_element->hasChildNodes(&& $doc_element->childNodes->length > 1 
  96.                 && $doc_element->childNodes->item(1)->namespaceURI == 
  97.                 'http://purl.org/rss/1.0/')):
  98.                 require_once 'XML/Feed/Parser/RSS1.php';
  99.                 require_once 'XML/Feed/Parser/RSS1Element.php';
  100.                 $class 'XML_Feed_Parser_RSS1';
  101.                 break;
  102.             case ($doc_element->namespaceURI == 'http://purl.org/rss/1.1/' || 
  103.                 ($doc_element->hasChildNodes(&& $doc_element->childNodes->length > 1 
  104.                 && $doc_element->childNodes->item(1)->namespaceURI == 
  105.                 'http://purl.org/rss/1.1/')):
  106.                 require_once 'XML/Feed/Parser/RSS11.php';
  107.                 require_once 'XML/Feed/Parser/RSS11Element.php';
  108.                 $class 'XML_Feed_Parser_RSS11';
  109.                 break;
  110.             case (($doc_element->hasChildNodes(&& $doc_element->childNodes->length > 1
  111.                 && $doc_element->childNodes->item(1)->namespaceURI == 
  112.                 'http://my.netscape.com/rdf/simple/0.9/'|| 
  113.                 $doc_element->namespaceURI == 'http://my.netscape.com/rdf/simple/0.9/'):
  114.                 require_once 'XML/Feed/Parser/RSS09.php';
  115.                 require_once 'XML/Feed/Parser/RSS09Element.php';
  116.                 $class 'XML_Feed_Parser_RSS09';
  117.                 break;
  118.             case ($doc_element->tagName == 'rss' and
  119.                 $doc_element->hasAttribute('version'&& 
  120.                 $doc_element->getAttribute('version'== 0.91):
  121.                 $this->log->warning('RSS 0.91 has been superceded by RSS2.0. Using RSS2.0 parser.');
  122.                 require_once 'XML/Feed/Parser/RSS2.php';
  123.                 require_once 'XML/Feed/Parser/RSS2Element.php';
  124.                 $class 'XML_Feed_Parser_RSS2';
  125.                 break;
  126.             case ($doc_element->tagName == 'rss' and
  127.                 $doc_element->hasAttribute('version'&& 
  128.                 $doc_element->getAttribute('version'== 0.92):
  129.                 $this->log->warning('RSS 0.92 has been superceded by RSS2.0. Using RSS2.0 parser.');
  130.                 require_once 'XML/Feed/Parser/RSS2.php';
  131.                 require_once 'XML/Feed/Parser/RSS2Element.php';
  132.                 $class 'XML_Feed_Parser_RSS2';
  133.                 break;
  134.             case (in_array($doc_element->namespaceURI$this->feedNamespaces['rss2'])
  135.                 || $doc_element->tagName == 'rss'):
  136.                 if ($doc_element->hasAttribute('version'|| 
  137.                     $doc_element->getAttribute('version'!= 2{
  138.                     $this->log->warning('RSS version not specified. Parsing as RSS2.0');
  139.                 }
  140.                 require_once 'XML/Feed/Parser/RSS2.php';
  141.                 require_once 'XML/Feed/Parser/RSS2Element.php';
  142.                 $class 'XML_Feed_Parser_RSS2';
  143.                 break;
  144.             default:
  145.                 throw new XML_Feed_Parser_Exception('Feed type unknown');
  146.                 break;
  147.         }
  148.  
  149.  
  150.         return $class;
  151.     }
  152. }

Documentation generated on Mon, 11 Mar 2019 15:47:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.