<?
    
# Transformation of Weather XML into RDF/XML.

    # (c) 2003 Morten Frederiksen
    # License: http://www.gnu.org/licenses/gpl

    
$path=$_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
    
$path=preg_replace('|\?.+|','',$path);
    
$xsl=join('',@file('weather.xsl'));
    
$metadata=join('',@file('weather.rdf'));
    
$xhtml='';
    
$weather='';

    
# Create XSLT parser.
    
$xslt=xslt_create();
    
xslt_set_error_handler($xslt,'xsltError');
    
$xslterror='';

    
# Look for XML.
    
$xml=@$_GET['xml'];
    if (
preg_match('|^http://|',$xml))
    {
        
# Try to retrieve weather XML.
        
$weather=@file($xml);
        if (
$weather)
        {
            
# Try to convert to RDF/XML.
            
if ($rdf=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                    array(
'/_xsl'=>$xsl,'/_xml'=>@join('',$weather)),
                    array(
'url'=>$xml)))
            {
                
header('Content-Type: application/rdf+xml; charset=utf-8');
                print(
$rdf);
                exit;
            };
            
$xhtml='<h1>Error</h1><p>Unable to transform Weather XML (<a href="'.htmlspecialchars($xml).'">'.htmlspecialchars($xml).'</a>).</p><p>'.$xslterror.'</p>';
        }
        else
            
# Flag non-retrievable RDF Schema.
            
$xhtml='<h1>Error</h1><p>Unable to find or read Weather XML ('.htmlspecialchars($xml).').</p>';
    }
    elseif (
$xml!='')
        
$xhtml='<h1>Error</h1><p>Invalid request (<em>xml='.htmlspecialchars($xml).'</em>), only absolute HTTP URLs allowed.</p>';

    if (
$xhtml=='')
    {
        
# Create service form.
        
if (!($xhtml=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                array(
'/_xsl'=>join('',@file('services-xhtml.xsl',1)),'/_xml'=>$metadata),
                array(
'path'=>$path))))
            
$xhtml='<p>Unable to parse metadata.</p><p>'.$xslterror.'</p>';
    };

    
# Transform metadata into header and footer.
    
if (!($metahead=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
            array(
'/_xsl'=>join('',@file('metahead.xsl',1)),'/_xml'=>$metadata))))
        
$metahead='<title>Parse error: '.$xslterror.'</title>';
    elseif (!(
$metatop=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
            array(
'/_xsl'=>join('',@file('metatop.xsl',1)),'/_xml'=>$metadata),
            array(
'path'=>$path,'uri'=>$rdfs))))
        
$metatop='<p>Unable to parse metadata.</p><p>'.$xslterror.'</p>';
    elseif (!(
$metafoot=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
            array(
'/_xsl'=>join('',@file('metafoot.xsl',1)),'/_xml'=>$metadata))))
        
$metafoot='<p>Unable to parse metadata.</p><p>'.$xslterror.'</p>';

    
# Display page.
    
print('<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
'
.$metahead.'
</head>
<body>
'
.$metatop.'
  <div style="padding: 1em">
'
.$xhtml.'
  </div>
'
.$metafoot.'
</body>
<!--
'
.str_replace('--','- -',$schema).'
-->
</html>'
);

    function
xsltError($x,$e,$l,$f)
    {
        global
$xslterror;
        
$xslterror='Sablotron XSLT transformation error';
        
reset($f);
        while(list(
$key,$val)=each($f))
        {
            if (
$key=='line')
                
$xslterror.=' on line '.$val;
            if (
$key=='msg')
                
$xslterror.=': <strong>'.$val.'</strong>';
        };
        
$xslterror.='.';
    };

?>