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

Source for file TeraWurflXMLParser_SimpleXML.php

Documentation is available at TeraWurflXMLParser_SimpleXML.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 SimpleXML
  17.  * @package TeraWurflXMLParser
  18.  */
  19.  
  20.     public function __construct(){
  21.         if(function_exists('simplexml_load_file')){
  22.             $this->parser_type = self::$PARSER_SIMPLEXML;
  23.         }else{
  24.             throw new Exception("Cannot load SimpleXML");
  25.         }
  26.     }
  27.     
  28.     public function open($filename,$file_type){
  29.         $this->file_type = $file_type;
  30.         if(function_exists('libxml_use_internal_errors')){
  31.             // Use advanced logging from libXML
  32.             //TODO: Figure out why LibXML doesn't properly report "out of memory" errors
  33.             //      when "libxml_use_internal_errors(true);".  The errors are accounted
  34.             //      for, but their ::message property is null.
  35.             //libxml_use_internal_errors(true);
  36.             $this->xml = simplexml_load_file($filename);
  37.             if (!$this->xml{
  38.                 $errors libxml_get_errors();
  39.                 foreach ($errors as $error{
  40.                     $type '';
  41.                     switch ($error->level{
  42.                         case LIBXML_ERR_WARNING:
  43.                             $type "Warning";
  44.                             break;
  45.                         case LIBXML_ERR_ERROR:
  46.                             $type "Error";
  47.                             break;
  48.                         case LIBXML_ERR_FATAL:
  49.                             $type "Fatal Error";
  50.                             break;
  51.                     }
  52.                     $this->errors["$typetrim($error->message);
  53.                 }
  54.                 libxml_clear_errors();
  55.             }
  56.         }else{
  57.             try{
  58.                 $this->xml = simplexml_load_file($filename);
  59.             }catch(Exception $ex){}
  60.             if(!$this->xml){
  61.                 $this->errors["Error: cannot parse XML file: $filename.";
  62.             }
  63.         }
  64.         if(count($this->errors0){
  65.             throw new Exception("SimpleXML reported the following errors:\n".implode("\n",$this->errors));
  66.         }
  67.     }
  68.     public function process(Array &$destination){
  69.         $this->devices =$destination;
  70.         if($this->file_type == self::$TYPE_WURFL && isset($this->xml->version)){
  71.             $this->wurflVersion = (string) $this->xml->version->ver;
  72.             $this->wurflLastUpdated = (string) $this->xml->version->last_updated;
  73.         }
  74.         $before_errors count($this->errors);
  75.         foreach($this->xml->devices->device as $device){
  76.             $this->loadDeviceXMLToArray($device);
  77.         }
  78.     }
  79.     protected function loadDeviceXMLToArray(&$device){
  80.         $id = (string)$device['id'];
  81.         $this->devices[$idarray('id'=>$id);
  82.         $filtering (TeraWurflConfig::$CAPABILITY_FILTER)true:false;
  83.         $includegroup false;
  84.         if(isset($device['fall_back'])) $this->devices[$id]['fall_back'= (string)$device['fall_back'];
  85.         if(isset($device['user_agent'])) $this->devices[$id]['user_agent'UserAgentUtils::cleanUserAgent((string)$device['user_agent']);
  86.         if(isset($device['actual_device_root'])){
  87.             $this->devices[$id]['actual_device_root'= (string)$device['actual_device_root'];
  88.             $this->devices[$id]['actual_device_root'($this->devices[$id]['actual_device_root'])?1:0;
  89.         }
  90.         foreach($device->group as $group){
  91.             $groupname = (string)$group['id'];
  92.             if($filtering && $this->enabled($groupname)){
  93.                 $includegroup true;
  94.             }else{
  95.                 $includegroup false;
  96.             }
  97.             $groupdata array();
  98.             foreach($group->capability as $cap){
  99.                 $capname = (string)$cap['name'];
  100.                 if(!$filtering || ($filtering && $includegroup|| ($filtering && !$includegroup && $this->enabled($capname))){
  101.                     $groupdata[$capname]=$this->cleanValue((string)$cap['value']);
  102.                 }
  103.             }
  104.             if(count($groupdata0){
  105.                 $this->devices[$id][$groupname$groupdata;
  106.             }
  107.             unset($groupdata);
  108.         }
  109.     }
  110. }

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