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

Source for file TeraWurflXMLParser_XMLReader.php

Documentation is available at TeraWurflXMLParser_XMLReader.php

  1. <?php
  2. /**
  3.  * Tera_WURFL - PHP MySQL driven WURFL
  4.  * 
  5.  * Tera-WURFL was written by Steve Kamerman, and is based on the
  6.  * Java WURFL Evolution package by Luca Passani and WURFL PHP Tools by Andrea Trassati.
  7.  * This version uses a MySQL database to store the entire WURFL file, multiple patch
  8.  * files, and a persistent caching mechanism to provide extreme performance increases.
  9.  * 
  10.  * @package TeraWurflXMLParser
  11.  * @author Steve Kamerman <stevekamerman AT gmail.com>
  12.  * @version Stable 2.1.3 $Date: 2010/09/18 15:43:21
  13.  * @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
  14.  */
  15. /**
  16.  * Loads the wurfl.xml file using the stream-based XMLReader class
  17.  * @package TeraWurflXMLParser
  18.  */
  19.     
  20.     public function __construct(){
  21.         if(class_exists('XMLReader')){
  22.             $this->parser_type = self::$PARSER_XMLREADER;
  23.         }else{
  24.             throw new Exception("Cannot load XMLReader");
  25.         }
  26.         $this->xml = new XMLReader();
  27.     }
  28.     
  29.     public function open($filename,$file_type){
  30.         $this->file_type = $file_type;
  31.         $this->xml->open($filename);
  32.         //TODO: add error handling
  33.     }
  34.     public function process(Array &$destination){
  35.         $this->devices =$destination;
  36.         while($this->xml->read()){
  37.             switch ($this->xml->nodeType){
  38.                 case XMLReader::ELEMENT:
  39.                     if($this->xml->name == "device"){
  40.                         $this->parseDevice();
  41.                         continue;
  42.                     }
  43.                     if($this->file_type == self::$TYPE_WURFL){
  44.                         if($this->xml->name == "ver" || $this->xml->name == "last_updated"){
  45.                             if($this->xml->name == "ver"$this->wurflVersion = $this->getValue();
  46.                             if($this->xml->name == "last_updated"$this->wurflLastUpdated = $this->getValue();
  47.                         }
  48.                     }
  49.                     break;
  50.                 case XMLReader::END_ELEMENT:
  51.                 default:
  52.                     break;
  53.             
  54.         }
  55.     }
  56.     protected function getValue(){
  57.         $this->xml->read();
  58.         return $this->xml->value;
  59.     }
  60.     protected function parseDevice(){
  61.         $this->devices[$this->xml->getAttribute('id')array();
  62.         $device =$this->devices[$this->xml->getAttribute('id')];
  63.         $device=array(
  64.             'id' => $this->xml->getAttribute('id'),
  65.             'user_agent' => UserAgentUtils::cleanUserAgent($this->xml->getAttribute('user_agent')),
  66.             'fall_back' => $this->xml->getAttribute('fall_back'),
  67.         );
  68.         if($this->xml->getAttribute('actual_device_root')) $device['actual_device_root'($this->xml->getAttribute('actual_device_root')=="true")?1:0;
  69.         $groupdevice '';
  70.         $groupname '';
  71.         $filtering (TeraWurflConfig::$CAPABILITY_FILTER)true:false;
  72.         $includegroup false;
  73.         while($this->xml->read()){
  74.             if($this->xml->nodeType != XMLReader::ELEMENTcontinue;
  75.             // recurse back into this function for the rest of the devices
  76.             switch($this->xml->name){
  77.                 case "device":
  78.                     $this->parseDevice();
  79.                     break;
  80.                 case "group":
  81.                     $groupname $this->xml->getAttribute('id');
  82.                     if($filtering && $this->enabled($this->xml->getAttribute('id'))){
  83.                         $includegroup true;
  84.                     }else{
  85.                         $includegroup false;
  86.                         continue;
  87.                     }
  88.                     $device[$groupnamearray();
  89.                     break;
  90.                 case "capability":
  91.                     if(!$filtering || ($filtering && $includegroup)){
  92.                         // the groupdevice array must already exist
  93.                         $device[$groupname][$this->xml->getAttribute('name')self::cleanValue($this->xml->getAttribute('value'));
  94.                         continue;
  95.                     }
  96.                     if($filtering && !$includegroup && $this->enabled($this->xml->getAttribute('name'))){
  97.                         // the groupdevice array might already exists
  98.                         if(!array_key_exists($groupname,$device)) $device[$groupnamearray();
  99.                         $device[$groupname][$this->xml->getAttribute('name')self::cleanValue($this->xml->getAttribute('value'));
  100.                         continue;
  101.                     }
  102.                     break;
  103.             }
  104.         }
  105.     }
  106. }

Documentation generated on Sun, 19 Sep 2010 00:16:06 +0000 by phpDocumentor 1.4.3