[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/tests/lib/ -> rss_writer_class.php (source)

   1  <?php
   2  if(!defined("METAL_LIBRARY_XML_RSS_WRITER_CLASS"))
   3  {
   4      define("METAL_LIBRARY_XML_RSS_WRITER_CLASS",1);
   5  
   6  /*
   7   *
   8   * Copyright © (C) Manuel Lemos 2002
   9   *
  10   * @(#) $Id: rss_writer_class.php,v 1.3 2004/06/12 10:44:49 harryf Exp $
  11   *
  12   */
  13  
  14  class rss_writer_class extends xml_writer_class
  15  {
  16      /*
  17       * Protected variables
  18       *
  19       */
  20      var $root="";
  21      var $channel="";
  22      var $image="";
  23      var $textinput="";
  24      var $items=0;
  25      var $itemsequence="";
  26      
  27      /*
  28       * Public variables
  29       *
  30       */
  31      var $specification="1.0";
  32      var $about="";
  33      var $rssnamespaces=array();
  34      var $allownoitems=0;
  35      var $generatedcomment="Generated by: http://www.phpclasses.org/rsswriter";
  36      
  37      
  38      /*
  39       * Protected functions
  40       *
  41       */
  42      Function addrssproperties(&$properties,$parent,&$required,&$optional,$scope)
  43      {
  44          $noattributes=array();
  45          $required_properties=0;
  46          Reset($properties);
  47          $end=(GetType($property=Key($properties))!="string");
  48          for(;!$end;)
  49          {
  50              if(IsSet($required[$property]))
  51              {
  52                  if($required[$property])
  53                  {
  54                      $this->error=("required ".$scope." property \"".$property."\" is already set");
  55                      return 0;
  56                  }
  57                  $required[$property]=1;
  58                  $required_properties++;
  59              }
  60              else
  61              {
  62                  if(IsSet($optional[$property]))
  63                  {
  64                      if($optional[$property])
  65                      {
  66                          $this->error=("optional ".$scope." property \"".$property."\" is already set");
  67                          return 0;
  68                      }
  69                      $optional[$property]=1;
  70                  }
  71                  else
  72                  {
  73                      if(GetType($colon=strpos($property,":",0))=="integer")
  74                      {
  75                          $namespace=substr($property,0,$colon);
  76                          if(!(!strcmp($namespace,"rdf") || IsSet($this->rssnamespaces[$namespace])))
  77                              $this->error=("the name space of property \"".$property."\" was not declared");
  78                      }
  79                      else
  80                          $this->error=("\"".$property."\" is not a supported ".$scope." property");
  81                  }
  82              }
  83              if(!($this->adddatatag($property,$noattributes,$properties[$property],$parent,$path)))
  84                  return 0;
  85              Next($properties);
  86              $end=(GetType($property=Key($properties))!="string");
  87          }
  88          if($required_properties<count($required))
  89          {
  90              Reset($required);
  91              $end=(GetType($property=Key($required))!="string");
  92              for(;!$end;)
  93              {
  94                  if(!($required[$property]))
  95                  {
  96                      $this->error=("it was not specified the required ".$scope." property \"".$property."\"");
  97                      return 0;
  98                  }
  99                  Next($required);
 100                  $end=(GetType($property=Key($required))!="string");
 101              }
 102          }
 103          return 1;
 104      }
 105      
 106      /*
 107       * Public functions
 108       *
 109       */
 110      Function addchannel(&$properties)
 111      {
 112          if(strcmp($this->error,""))
 113              return 0;
 114          if(strcmp($this->channel,""))
 115          {
 116              $this->error="a channel was already added";
 117              return 0;
 118          }
 119          $channel_attributes=array();
 120          switch($this->specification)
 121          {
 122              case "0.9":
 123                  $root="rdf:RDF";
 124                  $attributes=array("xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#","xmlns"=>"http://my.netscape.com/rdf/simple/0.9/");
 125                  $required=array("description"=>0,"link"=>0,"title"=>0);
 126                  $optional=array();
 127                  break;
 128              case "0.91":
 129                  $root="rss";
 130                  $attributes=array("version"=>$this->specification);
 131                  $required=array("description"=>0,"language"=>0,"link"=>0,"title"=>0);
 132                  $optional=array("copyright"=>0,"docs"=>0,"lastBuildDate"=>0,"managingEditor"=>0,"pubDate"=>0,"rating"=>0,"webMaster"=>0);
 133                  break;
 134              case "1.0":
 135                  if(!strcmp($this->about,""))
 136                  {
 137                      $this->error="it was not specified the about URL attribute";
 138                      return 0;
 139                  }
 140                  $root="rdf:RDF";
 141                  $attributes=array("xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#","xmlns"=>"http://purl.org/rss/1.0/");
 142                  Reset($this->rssnamespaces);
 143                  $end=(GetType($namespace=Key($this->rssnamespaces))!="string");
 144                  for(;!$end;)
 145                  {
 146                      if(!strcmp($namespace,"rdf"))
 147                      {
 148                          $this->error="the rdf namespace is being redeclared";
 149                          return 0;
 150                      }
 151                      $attributes[("xmlns:".$namespace)]=$this->rssnamespaces[$namespace];
 152                      Next($this->rssnamespaces);
 153                      $end=(GetType($namespace=Key($this->rssnamespaces))!="string");
 154                  }
 155                  $channel_attributes=array("rdf:about"=>$this->about);
 156                  $required=array("description"=>0,"link"=>0,"title"=>0);
 157                  $optional=array();
 158                  break;
 159              default:
 160                  $this->error="it was not specified a supported RSS specification version";
 161                  return 0;
 162          }
 163          $this->addtag($root,$attributes,"",$path,1);
 164          $this->root=$path;
 165          if(!($this->addtag("channel",$channel_attributes,$this->root,$path,1)))
 166              return 0;
 167          if(!($this->addrssproperties($properties,$path,$required,$optional,"channel")))
 168              return 0;
 169          $this->channel=$path;
 170          return 1;
 171      }
 172      
 173      Function additem(&$properties)
 174      {
 175          if(strcmp($this->error,""))
 176              return 0;
 177          if(!strcmp($this->channel,""))
 178          {
 179              $this->error="the channel was not yet added";
 180              return 0;
 181          }
 182          if(strcmp($this->textinput,""))
 183          {
 184              $this->error="items can not be added to the channel after defining the textinput";
 185              return 0;
 186          }
 187          $attributes=array();
 188          switch($this->specification)
 189          {
 190              case "0.9":
 191                  $parent=$this->root;
 192                  break;
 193              case "0.91":
 194                  $parent=$this->channel;
 195                  break;
 196              case "1.0":
 197                  if(IsSet($properties["link"]))
 198                      $attributes["rdf:about"]=$properties["link"];
 199                  $parent=$this->root;
 200                  break;
 201              default:
 202                  $this->error="it was not specified a supported RSS specification version";
 203                  return 0;
 204          }
 205          if(!($this->addtag("item",$attributes,$parent,$path,1)))
 206              return 0;
 207          $required=array("link"=>0,"title"=>0);
 208          $optional=array("description"=>0);
 209          if(!($this->addrssproperties($properties,$path,$required,$optional,"item")))
 210              return 0;
 211          if(!strcmp($this->specification,"1.0"))
 212          {
 213              if(!strcmp($this->itemsequence,""))
 214              {
 215                  $attributes=array();
 216                  if(!($this->addtag("items",$attributes,$this->channel,$path,1) && $this->addtag("rdf:Seq",$attributes,$path,$path,1)))
 217                      return 0;
 218                  $this->itemsequence=$path;
 219              }
 220              $attributes=array("rdf:resource"=>$properties["link"]);
 221              if(!($this->addtag("rdf:li",$attributes,$this->itemsequence,$path,0)))
 222                  return 0;
 223          }
 224          $this->items++;
 225          return 1;
 226      }
 227      
 228      Function addimage(&$properties)
 229      {
 230          if(strcmp($this->error,""))
 231              return 0;
 232          if(!strcmp($this->channel,""))
 233          {
 234              $this->error="the channel was not yet added";
 235              return 0;
 236          }
 237          if(strcmp($this->image,""))
 238          {
 239              $this->error="the channel image was already associated";
 240              return 0;
 241          }
 242          if($this->items!=0)
 243          {
 244              $this->error="the image can only be defined before adding the channel items";
 245              return 0;
 246          }
 247          $attributes=array();
 248          switch($this->specification)
 249          {
 250              case "0.9":
 251                  $parent=$this->root;
 252                  break;
 253              case "0.91":
 254                  $parent=$this->channel;
 255                  break;
 256              case "1.0":
 257                  if(IsSet($properties["url"]))
 258                      $attributes["rdf:about"]=$properties["url"];
 259                  $parent=$this->root;
 260                  break;
 261              default:
 262                  $this->error="it was not specified a supported RSS specification version";
 263                  return 0;
 264          }
 265          if(!($this->addtag("image",$attributes,$parent,$path,1)))
 266              return 0;
 267          $this->image=$path;
 268          $required=array("link"=>0,"title"=>0,"url"=>0);
 269          $optional=array("description"=>0,"width"=>0,"height"=>0);
 270          if(!($this->addrssproperties($properties,$this->image,$required,$optional,"image")))
 271              return 0;
 272          if(!strcmp($this->specification,"1.0"))
 273          {
 274              $attributes=array("rdf:resource"=>$properties["url"]);
 275              return $this->addtag("image",$attributes,$this->channel,$path,0);
 276          }
 277          return 1;
 278      }
 279      
 280      Function addtextinput(&$properties)
 281      {
 282          if(strcmp($this->error,""))
 283              return 0;
 284          if(!strcmp($this->channel,""))
 285          {
 286              $this->error="the channel was not yet added";
 287              return 0;
 288          }
 289          if(strcmp($this->textinput,""))
 290          {
 291              $this->error="the channel text input was already associated";
 292              return 0;
 293          }
 294          if($this->items==0 && !$this->allownoitems)
 295          {
 296              $this->error="it were not specified any items before defining the channel text input";
 297              return 0;
 298          }
 299          $attributes=array();
 300          switch($this->specification)
 301          {
 302              case "0.9":
 303                  $parent=$this->root;
 304                  break;
 305              case "0.91":
 306                  $parent=$this->channel;
 307                  break;
 308              case "1.0":
 309                  if(IsSet($properties["link"]))
 310                      $attributes["rdf:about"]=$properties["link"];
 311                  $parent=$this->root;
 312                  break;
 313              default:
 314                  $this->error="it was not specified a supported RSS specification version";
 315                  return 0;
 316          }
 317          if(!($this->addtag("textinput",$attributes,$parent,$path,1)))
 318              return 0;
 319          $this->textinput=$path;
 320          $required=array("description"=>0,"link"=>0,"name"=>0,"title"=>0);
 321          $optional=array();
 322          if(!($this->addrssproperties($properties,$this->textinput,$required,$optional,"textinput")))
 323              return 0;
 324          if(!strcmp($this->specification,"1.0"))
 325          {
 326              $attributes=array("rdf:resource"=>$properties["link"]);
 327              return $this->addtag("textinput",$attributes,$this->channel,$path,0);
 328          }
 329          return 1;
 330      }
 331      
 332      Function writerss(&$output)
 333      {
 334          if(strcmp($this->error,""))
 335              return 0;
 336          if(!strcmp($this->channel,""))
 337          {
 338              $this->error="it was not defined the RSS channel";
 339              return 0;
 340          }
 341          if($this->items==0 && !$this->allownoitems)
 342          {
 343              $this->error="it were not defined any RSS channel items";
 344              return 0;
 345          }
 346          switch($this->specification)
 347          {
 348              case "0.9":
 349                  $this->dtdtype="PUBLIC";
 350                  $this->dtddefinition="-//Netscape Communications//DTD RSS 0.9//EN";
 351                  $this->dtdurl="http://my.netscape.com/publish/formats/rss-0.9.dtd";
 352                  break;
 353              case "0.91":
 354                  $this->dtdtype="PUBLIC";
 355                  $this->dtddefinition="-//Netscape Communications//DTD RSS 0.91//EN";
 356                  $this->dtdurl="http://my.netscape.com/publish/formats/rss-0.91.dtd";
 357                  break;
 358              case "1.0":
 359                  $this->dtdtype="";
 360                  break;
 361              default:
 362                  $this->error="it was not specified a supported RSS specification version";
 363                  return 0;
 364          }
 365          return $this->write($output);
 366      }
 367  };
 368  
 369  }
 370  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5