Source for file HTML.php

Documentation is available at HTML.php

  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/ResultRenderer.php';
  3. require_once RDFAPI_INCLUDE_DIR 'util/RdfUtil.php';
  4.  
  5. /**
  6. *   XML result renderer for SparqlEngine
  7. *
  8. *   @author Tobias Gauß <tobias.gauss@web.de>
  9. *   @author Christian Weiske <cweiske@cweiske.de>
  10. *
  11. *   @package sparql
  12. */
  13. class SparqlEngine_ResultRenderer_HTML implements SparqlEngine_ResultRenderer
  14. {
  15.     /**
  16.     *   If the result HTML should be wrapped in a div
  17.     *   @var boolean 
  18.     */
  19.     protected $bWrap = true;
  20.  
  21.  
  22.  
  23.     /**
  24.     *   Converts the database results into the output format
  25.     *   and returns the result.
  26.     *
  27.     *   @param array $arVartable    Variable table
  28.     *   @param Query $query         SPARQL query object
  29.     *   @param SparqlEngine $engine Sparql Engine to query the database
  30.     *   @return string HTML result
  31.     */
  32.     public function convertFromResult($arVartableQuery $querySparqlEngine $engine)
  33.     {
  34.         $this->query   $query;
  35.         $this->engine  $engine;
  36.         $this->dataset $engine->getDataset();
  37.  
  38.         $strCode     '';
  39.  
  40.         $strResultForm $query->getResultForm();
  41.         switch ($strResultForm{
  42.             case 'select':
  43.             case 'select distinct':
  44.                 $strCode $this->createTableFromRecords($arVartable);
  45.                 break;
  46.  
  47.             case 'construct':
  48.             case 'describe':
  49.                 throw new Exception(
  50.                     'Construct and describe are currently not supported by the'
  51.                     . ' HTML renderer'
  52.                 );
  53.  
  54.             case 'count':
  55.             case 'ask':
  56.                 $nCount count($arVartable);
  57.  
  58.                 if ($strResultForm == 'ask'{
  59.                     $strCode 'There were results.';
  60.                 else {
  61.                     $strCode 'There are ' $nCount ' results.';
  62.                 }
  63.                 break;
  64.  
  65.             default:
  66.                 throw new Exception('Unsupported result form: ' $strResultForm);
  67.         }
  68.  
  69.         return $this->wrapCode($strCode);
  70.     }//public function convertFromResult($arVartable, Query $query, SparqlEngine $engine)
  71.  
  72.  
  73.  
  74.     protected function wrapCode($strCode)
  75.     {
  76.         if (!$this->bWrap{
  77.             return $strCode;
  78.         }
  79.  
  80.         return
  81.             '<div class="SparqlEngine_ResultRenderer_HTML_result">' "\n"
  82.             . $strCode "\n"
  83.             . "</div>\n";
  84.     }//protected function wrapCode($strCode)
  85.  
  86.  
  87.  
  88.     protected function createTableFromRecords($arVartable)
  89.     {
  90.         if (count($arVartable== 0{
  91.             return 'No result rows.';
  92.         }
  93.  
  94.         $arResult array();
  95.         foreach ($arVartable as $row{
  96.             $arResultRow array();
  97.             foreach ($row as $strVarName => $value{
  98.                 $arResultRow[$strVarName$this->createValue($value);
  99.             }
  100.             $arResult[$arResultRow;
  101.         }
  102.  
  103.         //I always wanted to to this :)
  104.         return
  105.             "<table border='1'>\n"
  106.             . " <caption>SPARQL result with " count($arResult" rows</caption>\n"
  107.             . " <thead><th>"
  108.                 . implode('</th><th>'array_keys(reset($arResult)))
  109.             . "</th></thead>\n"
  110.             . " <tbody>\n  <tr>"
  111.             . implode(
  112.                 "</tr>\n  <tr>",
  113.                 array_map(
  114.                     create_function(
  115.                         '$ar',
  116.                         'return implode("", $ar);'
  117.                     ),
  118.                     $arResult
  119.                 )
  120.               )
  121.             . "</tr>\n </tbody>\n"
  122.             . "</table>\n";
  123.     }//protected function createTableFromRecords($arRecordSets)
  124.  
  125.  
  126.  
  127.     /**
  128.     *   Creates an RDF object object
  129.     *   contained in the given $dbRecordSet object.
  130.     *
  131.     *   @see convertFromDbResult() to understand $strVarBase necessity
  132.     *
  133.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  134.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  135.     *
  136.     *   @return string HTML code
  137.     */
  138.     protected function createValue($value)
  139.     {
  140.         if ($value === null{
  141.             $strCode $this->getHtmlNull();
  142.         }
  143.         if ($value instanceof Literal{
  144.             $strCode $this->getHtmlLiteral(
  145.                 $value->getLabel(),
  146.                 $value->getLanguage(),
  147.                 $value->getDatatype()
  148.             );
  149.         else if ($value instanceof Resource{
  150.             $strCode $this->getHtmlResource($value->getURI());
  151.         else {
  152.             $strCode $this->getHtmlBlank();
  153.         }
  154.  
  155.         return '<td style="background-color: '
  156.                . RdfUtil::chooseColor($value'">'
  157.                . $strCode
  158.                . '</td>';
  159.     }//protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  160.  
  161.  
  162.  
  163.     protected function getHtmlNull()
  164.     {
  165.         return '<pre>NULL</pre>';
  166.     }//protected function getHtmlNull()
  167.  
  168.  
  169.  
  170.     protected function getHtmlBlank($value)
  171.     {
  172.         return '<i>Blank node</i>';
  173.     }//protected function getHtmlBlank($value)
  174.  
  175.  
  176.  
  177.     protected function getHtmlResource($value)
  178.     {
  179.         return htmlspecialchars($value);
  180.     }//protected function getHtmlResource($value)
  181.  
  182.  
  183.  
  184.     protected function getHtmlLiteral($value$language$datatype)
  185.     {
  186.         $strCode htmlspecialchars($value);
  187.         if ($language{
  188.             $strCode .= '<br/>&nbsp;&nbsp;<i>xml:lang</i>=' $language;
  189.         }
  190.         if ($datatype{
  191.             $strCode .= '<br/>&nbsp;&nbsp;<i>rdf:type</i>=' $datatype;
  192.         }
  193.         return $strCode;
  194.     }//protected function getHtmlLiteral($value, $language, $datatype)
  195.  
  196. }//class SparqlEngine_ResultRenderer_HTML implements SparqlEngine_ResultRenderer
  197.  
  198. ?>

Documentation generated on Fri, 1 Jun 2007 16:49:18 +0200 by phpDocumentor 1.3.2