<?php
/************************************************************
# XalanTransform Object for PHP
# Interfaces PHP with the Xalan XSLT Processor for Unix
# Copyright (C) 2004 Craig R. Hoover
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/gpl.txt
#
/************************************************************
# This class allows PHP to utilize the Xalan processor
# through Java for execution of XSLT transformations
# you must know the executable path of Java as well as the
# Xalan Java class in order to execute. This can be set in
# the javaexecpath property.
#
# Results are passed to: XalanTransform->output
#
# Parameters to set:
# xml -> XML data
# xsl -> XSL data
# xmlfile -> XML file path
# xslfile -> XSL file path
# outputtype -> HTML or XML
# outputfile -> File to output results to.
#
# Optional:
# javaexecpath -> Default execution path for Java and Xalan
#
# Returned data:
# error -> Errors returned during processing
# output -> Processed data. If outputted to file, file path
# xml -> Returns XML used for processing
# xsl -> Returns XSL used for processing
#
# Methods:
# process() -> Process data
# reset() -> Clears object buffer for new object
************************************************************/
class XalanTransform{
var $xmlfile;
var $xslfile;
var $outputtype;
var $output;
var $outputfile;
var $xml;
var $xsl;
var $error;
var $javaexecpath;
var $command;
function XalanTransform(){
// Set standard output type
$this->outputtype="TEXT";
// Set error message
$this->error="";
// Set path to execute Xalan command line
$this->javaexecpath=" /opt/IBMJava2-141/jre/bin/java org.apache.xalan.xslt.Process";
}
function process(){
$xmlpassed=false;
$xslpassed=false;
// Check for xmlfile if passed
if($this->xmlfile==""){
if ($this->xml==""){
$this->error="ERROR: XML source was left empty.";
return 0;
}else{
// If XML data was passed, create temp file
$fname="xalan".rand().".xml";
$fp=fopen("/tmp/$fname","w");
fwrite($fp,$this->xml);
fclose($fp);
$this->xmlfile="/tmp/$fname";
$xmlpassed=true;
}
}else{
// Check that xml path actually exists
$fp=@realpath($this->xmlfile);
if(!$fp){
$this->error="ERROR: XML file specified was not found.";
return 0;
}
// Read XML file into xml property
$fp=fopen($this->xmlfile,"r");
$this->xml=fread($fp,filesize($this->xmlfile));
fclose($fp);
}
// Check for xslfile if passed
if($this->xslfile==""){
if ($this->xsl==""){
$this->error="ERROR: XSL source was left empty.";
return 0;
}else{
// If XSL data was passed, create temp file
$fname="xalan".rand().".xsl";
$fp=fopen("/tmp/$fname","w");
fwrite($fp,$this->xsl);
fclose($fp);
$this->xslfile="/tmp/$fname";
$xslpassed=true;
}
}else{
// Check that xsl path actually exists
$fp=@realpath($this->xslfile);
if(!$fp){
$this->error="ERROR: XSL file specified was not found.";
return 0;
}
// Read XSL file into xsl property
$fp=fopen($this->xslfile,"r");
$this->xsl=fread($fp,filesize($this->xslfile));
fclose($fp);
}
// If output file was specified, add to system call
$outfile=$this->outputfile!="" ? " -OUT ".$this->outputfile:"";
// Create temp random error file to avoid object conflicts
$errfile="/tmp/xalanerrfile".rand();
// Concatenate system call
$shellcmd=$this->javaexecpath." -IN ".$this->xmlfile." -XSL ".$this->xslfile.$outfile." -".$this->outputtype." 2> $errfile";
$this->command=$shellcmd;
// Execute Xalan interpreter
exec($shellcmd,$result,$status);
$str="";
// If XML data was passed then delete temp file
if($xmlpassed){
unlink($this->xmlfile);
}
// If XSL data was passed then delete temp file
if($xslpassed){
unlink($this->xslfile);
}
// Results come back in an array
// Test to see if array has items
// If so, read into $str variable
if (count($result)>0){
foreach($result as $line){
$str.=$line;
}
}else{
if(filesize($errfile)>1){
$fp=fopen($errfile,"r");
$str=fread($fp, filesize($errfile));
fclose($fp);
unlink($errfile);
$this->error="ERROR: Processing Failed! ".$str;
return 0;
}else{
$str=realpath($this->outputfile);
}
}
if(file_exists($errfile)){
unlink($errfile);
}
$this->output=$str;
}
function reset(){
$this->xmfile=null;
unset($this->xmlfile);
$this->xslfile=null;
unset($this->xslfile);
$this->xml=null;
unset($this->xml);
$this->xsl=null;
unset($this->xsl);
$this->output=null;
unset($this->output);
$this->error=null;
unset($this->error);
$this->outputtype=null;
unset($this->outputtype);
$this->outputfile=null;
unset($this->outputfile);
$this->processtime=null;
unset($this->processtime);
}
}
/*############### EXAMPLES #################
// USE FIlES AND WRITE TO STRING
$xslt=new XalanTransform;
$xslt->xmlfile="somexmlfile.xml";
$xslt->xslfile="somexslfile.xsl";
$xslt->outputtype="HTML";
$xslt->process();
if(strlen($xslt->error)==0){
print "Successfull transformation!";
print $xslt->output;
}else{
print "Error transforming document:\n";
print $xslt->error;
}
// USE TEXT STRINGS AND SAVE TO FILE
$xml=file("somexmlfile.xml");
$xsl=<<<EOF
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<xsl:apply-templates/>
</head>
<body>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
EOF;
$xslt=new XalanTransform;
$xslt->xml=$xml;
$xslt->xsl=$xsl;
$xslt->outputtype="HTML";
$xslt->outputfile="./results/myfile.html";
$xslt->process();
if(strlen($xslt->error)==0){
print "Successfull transformation!";
print $xslt->output;
}else{
print "Error transforming document:\n";
print $xslt->error;
}
##########################################*/
?>