Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

xslt.class.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 a(tom)Blog is a atom powered weblog system. 00004 Copyright (C) 2004 Yoan BLANC 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 */ 00041 class Xslt 00042 { 00044 var $xml; 00046 var $xsl; 00048 var $param; 00049 00051 var $engine; 00053 var $xsltEngines; 00054 00056 var $result; 00057 00066 function Xslt($xml="",$xsl="",$param=NULL,$engine="sablotron") 00067 { 00068 $this->xsltEngines = array("sablotron"=>1,"domxml"=>1,"xsltproc"=>1); 00069 00070 $this->setXml($xml); 00071 $this->setXsl($xsl); 00072 $this->setParam($param); 00073 $this->setEngine($engine); 00074 } 00075 00081 function setEngine($engine) 00082 { 00083 $engine = strtolower($engine); 00084 if(isset($this->xsltEngines[$engine]) && $this->xsltEngines[$engine]==1) 00085 { 00086 $this->engine = $engine; 00087 } 00088 else 00089 { 00090 return false; 00091 } 00092 } 00093 00099 function getEngine() 00100 { 00101 return $this->engine; 00102 } 00103 00109 function transform() 00110 { 00111 switch ($this->engine) 00112 { 00113 case "sablotron": 00114 $engine = new Sablotron($this->xml, $this->xsl, $this->param); 00115 break; 00116 case "domxml": 00117 $engine = new Domxml($this->xml, $this->xsl, $this->param); 00118 break; 00119 case "xsltproc": 00120 $engine = new Xsltproc($this->xml, $this->xsl, $this->param); 00121 break; 00122 } 00123 00124 if(isset($engine)) 00125 { 00126 $engine->transform(); 00127 $this->result = $engine->result(); 00128 } 00129 else 00130 { 00131 $this->result = '<error><msg>xlst.class.php:Xslt::transform()</msg></error>'; 00132 } 00133 } 00134 00140 function setXml($xml="") 00141 { 00142 if(file_exists($xml)) 00143 { 00144 $this->xml = $xml; 00145 return true; 00146 } 00147 else 00148 { 00149 return false; 00150 } 00151 } 00152 00158 function setXsl($xsl="") 00159 { 00160 if(file_exists($xsl)) 00161 { 00162 $this->xsl = $xsl; 00163 return true; 00164 } 00165 else 00166 { 00167 return false; 00168 } 00169 } 00170 00176 function setParam($param=NULL) 00177 { 00178 if(is_array($param)) 00179 { 00180 $this->param = $param; 00181 } 00182 else 00183 { 00184 $this->param = array(); 00185 } 00186 } 00187 00193 function toString() 00194 { 00195 $ret = " 00196 <dl> 00197 <dt>xml</dt> 00198 <dd>".$this->xml."</dd> 00199 <dt>xsl</dt> 00200 <dd>".$this->xsl."</dd>"; 00201 if(count($this->param) > 0) 00202 { 00203 $ret .= " 00204 <dt>param</dt> 00205 <dd><dl>"; 00206 foreach($this->param as $key => $value) 00207 { 00208 $ret .= " 00209 <dt>".$key."</dt> 00210 <dd>".$value."</dd>"; 00211 } 00212 $ret .= "</dl></dd>"; 00213 } 00214 $ret .= " 00215 <dt>engine</dt> 00216 <dd><dl>"; 00217 foreach($this->xsltEngines as $key => $value) 00218 { 00219 $ret .= " 00220 <dt>".$key."</dt> 00221 <dd>"; 00222 if($key == $this->engine) 00223 { 00224 $ret .= "selected"; 00225 } 00226 else if($value == 0) 00227 { 00228 $ret .= "unactive"; 00229 } 00230 else 00231 { 00232 $ret .= "active"; 00233 } 00234 $ret .= "</dd>"; 00235 } 00236 $ret .= "</dl></dd> 00237 </dl> 00238 "; 00239 return $ret; 00240 } 00241 00247 function result() 00248 { 00249 return $this->result; 00250 } 00251 } 00252 00259 class xslt_base 00260 { 00262 var $xml; 00264 var $xsl; 00266 var $param; 00268 var $result; 00269 00277 function xslt_base($xml, $xsl, $param=array()) 00278 { 00279 $this->xml = $xml; 00280 $this->xsl = $xsl; 00281 $this->param = $param; 00282 } 00283 00287 function transform() 00288 { 00289 return "<error><msg>this is an abstract class</msg></error>"; 00290 } 00291 00297 function result() 00298 { 00299 return $this->result; 00300 } 00301 } 00302 00309 class Sablotron extends xslt_base 00310 { 00312 var $xh; 00313 00321 function Sablotron($xml, $xsl, $param=array()) 00322 { 00323 parent::xslt_base($xml, $xsl, $param); 00324 $this->xh = xslt_create(); 00325 } 00326 00330 function transform() 00331 { 00332 if(!($this->result = xslt_process($this->xh, $this->xml, $this->xsl,NULL,array(),$this->param))) 00333 { 00334 $this->result = '<error><msg>'.xslt_error($this->xh).'</msg><num>'.xslt_errno($this->xh).')</num></error>'; 00335 } 00336 xslt_free($this->xh); 00337 } 00338 } 00345 class Domxml extends xslt_base 00346 { 00354 function Domxml($xml, $xsl, $param=array()) 00355 { 00356 parent::xslt_base($xml, $xsl, $param); 00357 } 00358 00362 function transform() 00363 { 00364 $dom_xslt = domxml_xslt_stylesheet_file($this->xsl); 00365 $dom_xml = domxml_open_file($this->xml); 00366 $dom_data = $dom_xslt->process($dom_xml,$this->param); 00367 //$this->result = $dom_data->dump_mem(); 00368 $this->result = $dom_xslt->result_dump_mem($dom_data); 00369 } 00370 } 00371 00378 class Xsltproc extends xslt_base 00379 { 00387 function Xsltproc($xml, $xsl, $param=array()) 00388 { 00389 parent::xslt_base($xml, $xsl, $param); 00390 $this->proc = '/usr/bin/xsltproc'; 00391 } 00392 00396 function transform() 00397 { 00398 exec($this->proc.$this->__paramFormat()." ".$this->xsl." ".$this->xml." 2>&1",$result); 00399 $this->result = implode("\n",$result); 00400 } 00401 00407 function __paramFormat() 00408 { 00409 $string = ""; 00410 if(count($this->param)>0) 00411 { 00412 foreach($this->param as $key => $value) 00413 { 00414 $string .= ' --stringparam '.$key.' "'.$value.'"'; 00415 } 00416 } 00417 return $string; 00418 } 00419 } 00420 ?>

Generated on Fri Jun 25 00:00:22 2004 for a(tom)Blog by doxygen 1.3.7