Source for file XML.php

Documentation is available at XML.php

  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/ResultRenderer.php';
  3.  
  4. /**
  5. *   Sparql DB XML result renderer as defined by
  6. *   http://www.w3.org/TR/rdf-sparql-XMLres/
  7. *
  8. *   @author Christian Weiske <cweiske@cweiske.de>
  9. *
  10. *   @package sparql
  11. */
  12. class SparqlEngineDb_ResultRenderer_XML implements SparqlEngineDb_ResultRenderer
  13. {
  14.  
  15.     /**
  16.     *   Defines the methods needed to create the types
  17.     *   in $arVarAssignments.
  18.     *   Key is the type (e.g. "s" for subject), and
  19.     *   value the method's name.
  20.     *
  21.     *   @see $arVarAssignments
  22.     *
  23.     *   @var array 
  24.     */
  25.     protected $arCreationMethods = array(
  26.         's' => 'createSubjectFromDbRecordSetPart',
  27.         'p' => 'createPredicateFromDbRecordSetPart',
  28.         'o' => 'createObjectFromDbRecordSetPart'
  29.     );
  30.  
  31.  
  32.  
  33.     /**
  34.     *   Converts the database results into nice HTML.
  35.     *
  36.     *   @param array $arRecordSets  Array of (possibly several) SQL query results.
  37.     *   @param Query $query     SPARQL query object
  38.     *   @param SparqlEngineDb $engine   Sparql Engine to query the database
  39.     *   @return mixed   HTML code
  40.     */
  41.     public function convertFromDbResults($arRecordSetsQuery $querySparqlEngineDb $engine)
  42.     {
  43.         $this->query $query;
  44.         $this->sg    $engine->getSqlGenerator();
  45.         $strCode     '';
  46.  
  47.         $strResultForm $query->getResultForm();
  48.         switch ($strResultForm{
  49.             case 'select':
  50.             case 'select distinct':
  51.                 $strCode $this->createFromRecords($arRecordSets$strResultForm);
  52.                 break;
  53.  
  54.             case 'construct':
  55.             case 'describe':
  56.                 throw new Exception(
  57.                     'Construct and describe are not supported by the'
  58.                     . ' XML renderer'
  59.                 );
  60.  
  61.             case 'count':
  62.             case 'ask':
  63.                 if (count($arRecordSets1{
  64.                     throw new Exception(
  65.                         'More than one result set for a '
  66.                         . $strResultForm ' query!'
  67.                     );
  68.                 }
  69.  
  70.                 $nCount 0;
  71.                 $dbRecordSet reset($arRecordSets);
  72.                 foreach ($dbRecordSet as $row{
  73.                     $nCount intval($row['count']);
  74.                     break;
  75.                 }
  76.  
  77.                 if ($strResultForm == 'ask'{
  78.                     $strCode $this->getHead()
  79.                         . '  <boolean>'
  80.                         . self::getSpokenBoolean($nCount 0)
  81.                         . '</boolean>';
  82.                 else {
  83.                     $strCode $this->getHead()
  84.                         . '  <int>'
  85.                         . $nCount
  86.                         . '</int>';
  87.                 }
  88.                 break;
  89.  
  90.             default:
  91.                 throw new Exception('Unsupported result form: ' $strResultForm);
  92.         }
  93.  
  94.         return $this->wrapCode($strCode);
  95.     }//public function convertFromDbResults($arRecordSets, Query $query, SparqlEngineDb $engine)
  96.  
  97.  
  98.  
  99.     protected function wrapCode($strCode)
  100.     {
  101.         return <<<EOT
  102. <?xml version="1.0"?>
  103. <sparql xmlns="http://www.w3.org/2005/sparql-results#">
  104.  
  105. EOT            . $strCode "\n"
  106.             . "</sparql>\n";
  107.     }//protected function wrapCode($strCode)
  108.  
  109.  
  110.  
  111.     protected function getHead($strXml '')
  112.     {
  113.         return "  <head>\n"
  114.             . $strXml
  115.             . "  </head>\n";
  116.     }//protected function getHead($strXml = '')
  117.  
  118.  
  119.  
  120.     protected function createFromRecords($arRecordSets$strResultForm)
  121.     {
  122.         $arResultVars $this->query->getResultVars();
  123.  
  124.         if (in_array('*'$arResultVars)) {
  125.             $arResultVars   array_keys($this->sg->arVarAssignments);
  126.         }
  127.  
  128.         $strHeadXml $this->getHead(
  129.             "    <variable name=\""
  130.             . implode(
  131.                 "\"/>\n    <variable name=\"",
  132.                 $arResultVars
  133.             )
  134.             . "\"/>\n"
  135.         );
  136.  
  137.         $arResult array();
  138.         foreach ($arRecordSets as $dbRecordSet{
  139.             //work around bug in adodb:
  140.             // ADORecordSet_empty does not implement php5 iterators
  141.             if ($dbRecordSet->RowCount(<= 0{
  142.                 return array();
  143.             }
  144.  
  145.             foreach ($dbRecordSet as $row{
  146.                 $arResultRow array();
  147.                 foreach ($arResultVars as $strVarName{
  148.                     if (!isset($this->sg->arVarAssignments[$strVarName])) {
  149.                         //variable is in select, but not in result (test: q-select-2)
  150.                         $arResultRow[$strVarName'';
  151.                     else {
  152.                         $arVarSettings  $this->sg->arVarAssignments[$strVarName];
  153.                         $strMethod      $this->arCreationMethods[$arVarSettings[1]];
  154.                         $arResultRow[$strVarName$this->$strMethod($dbRecordSet$arVarSettings[0]$strVarName);
  155.                     }
  156.                 }
  157.                 $arResult[$arResultRow;
  158.             }
  159.         }
  160.  
  161.  
  162.         $arSM $this->query->getSolutionModifier();
  163.  
  164.         return
  165.             $strHeadXml
  166.             . '  <results ordered="'
  167.                 . self::getSpokenBoolean($arSM['order by'!== null)
  168.                 . '" distinct="'
  169.                 . self::getSpokenBoolean($strResultForm == 'select distinct')
  170.                 . '">' "\n"
  171.             . $this->getResultXml($arResult)
  172.             . "  </results>\n";
  173.     }//protected function createFromRecords($arRecordSets)
  174.  
  175.  
  176.  
  177.     protected function getResultXml($arResult)
  178.     {
  179.         $strCode '';
  180.         foreach ($arResult as $arSet{
  181.             $strCode .= "    <result>\n";
  182.             foreach ($arSet as $strVarName => $strValue{
  183.                 if ($strValue !== null{
  184.                     $strCode .= '      <binding name="' $strVarName '">'
  185.                         . $strValue
  186.                         . "</binding>\n";
  187.                 }
  188.             }
  189.             $strCode .= "    </result>\n";
  190.         }
  191.         return $strCode;
  192.     }//protected function getResultXml($arResult)
  193.  
  194.  
  195.  
  196.     protected static function getSpokenBoolean($b)
  197.     {
  198.         return $b 'true' 'false';
  199.     }//protected static function getSpokenBoolean($b)
  200.  
  201.  
  202.  
  203.     /**
  204.     *   Creates an RDF subject object
  205.     *   contained in the given $dbRecordSet object.
  206.     *
  207.     *   @see convertFromDbResult() to understand $strVarBase necessity
  208.     *
  209.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  210.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  211.     *
  212.     *   @return string HTML code
  213.     */
  214.     protected function createSubjectFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase$strVarName)
  215.     {
  216.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']] === null{
  217.             return $this->getXmlNull();
  218.         }
  219.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_is']] == 'r'
  220.             //null should be predicate which is always a resource
  221.          || $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_is']] === null
  222.         {
  223.             return $this->getXmlResource($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  224.         else {
  225.             return $this->getXmlBlank($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  226.         }
  227.     }//protected function createSubjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  228.  
  229.  
  230.  
  231.     /**
  232.     *   Creates an RDF predicate object
  233.     *   contained in the given $dbRecordSet object.
  234.     *
  235.     *   @see convertFromDbResult() to understand $strVarBase necessity
  236.     *
  237.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  238.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  239.     *
  240.     *   @return string HTML code
  241.     */
  242.     protected function createPredicateFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase$strVarName)
  243.     {
  244.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']] === null{
  245.             return $this->getXmlNull();
  246.         }
  247.  
  248.         return $this->getXmlResource($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  249.     }//protected function createPredicateFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  250.  
  251.  
  252.  
  253.     /**
  254.     *   Creates an RDF object object
  255.     *   contained in the given $dbRecordSet object.
  256.     *
  257.     *   @see convertFromDbResult() to understand $strVarBase necessity
  258.     *
  259.     *   @param ADORecordSet $dbRecordSet    Record set returned from ADOConnection::Execute()
  260.     *   @param string       $strVarBase     Prefix of the columns the recordset fields have.
  261.     *
  262.     *   @return string HTML code
  263.     */
  264.     protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet$strVarBase$strVarName)
  265.     {
  266.         if ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']] === null{
  267.             return $this->getXmlNull();
  268.         }
  269.         switch ($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_is']]{
  270.             case 'r':
  271.                 return $this->getXmlResource($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  272.                 break;
  273.             case 'b':
  274.                 return $this->getXmlBlank($dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']]);
  275.                 break;
  276.             default:
  277.                 return $this->getXmlLiteral(
  278.                     $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_value']],
  279.                     $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_lang']],
  280.                     $dbRecordSet->fields[$strVarBase '.' $this->sg->arVarAssignments[$strVarName]['sql_type']]
  281.                 );
  282.         }
  283.     }//protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVarName)
  284.  
  285.  
  286.  
  287.     protected function getXmlNull()
  288.     {
  289.         return null;
  290.     }//protected function getHtmlNull()
  291.  
  292.  
  293.  
  294.     protected function getXmlBlank($value)
  295.     {
  296.         return '<bnode>' $value '</bnode>';
  297.     }//protected function getHtmlBlank($value)
  298.  
  299.  
  300.  
  301.     protected function getXmlResource($value)
  302.     {
  303.         return '<uri>' htmlspecialchars($value'</uri>';
  304.     }//protected function getHtmlResource($value)
  305.  
  306.  
  307.  
  308.     protected function getXmlLiteral($value$language$datatype)
  309.     {
  310.         $strCode '<literal';
  311.         if ($language{
  312.             $strCode ' xml:lang="' $language '"';
  313.         }
  314.         if ($datatype{
  315.             $strCode ' datatype="' $datatype '"';
  316.         }
  317.         $strCode .= '>' htmlspecialchars($value'</literal>';
  318.         return $strCode;
  319.     }//protected function getHtmlLiteral($value, $language, $datatype)
  320.  
  321. }//class SparqlEngineDb_ResultRenderer_XML implements SparqlEngineDb_ResultRenderer
  322.  
  323. ?>

Documentation generated on Fri, 1 Jun 2007 16:52:45 +0200 by phpDocumentor 1.3.2