source: Dev/branches/rest-dojo-ui/server/rdfapi/sparql/SparqlEngineDb/ResultConverter.php @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 4.0 KB
Line 
1<?php
2require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb/ResultRenderer.php';
3
4/**
5*   Converts a database result into a proper
6*   rdf statement triple array
7*
8*   @author Christian Weiske <cweiske@cweiske.de>
9*   @license http://www.gnu.org/licenses/lgpl.html LGPL
10*
11*   @package sparql
12*/
13class SparqlEngineDb_ResultConverter
14{
15    /**
16    *   Determines the correct renderer and calls it.
17    *
18    *   The $resultform may be:
19    *   - false: The default renderer is taken then
20    *   - an object that implements SparqlEngineDb_ResultRenderer interface
21    *   - a string like "HTML" or "XML". The appropriate renderer is used then.
22    *   - a full class name, e.g. SparqlEngineDb_ResultRenderer_XML
23    *
24    *   @param array    $arRecordSets   Array of anything ADOConnection::Execute() can return
25    *   @param SparqlEngineDb $engine   Sparql database engine.
26    *   @param mixed    $resultform     Which format the results shall be in (false or "xml")
27    *
28    *   @return mixed   Most likely an array or a boolean value,
29    *                   or anything else as determined by $resultform
30    */
31    public static function convertFromDbResults($arRecordSets, SparqlEngineDb $engine, $resultform = false)
32    {
33        if (is_object($resultform)) {
34            if ($resultform instanceof SparqlEngineDb_ResultRenderer) {
35                return $resultform->convertFromDbResults(
36                    $arRecordSets,
37                    $engine->getQuery(),
38                    $engine
39                );
40            } else {
41                throw new Exception(
42                    'Result renderer object needs to implement'
43                    . ' SparqlEngineDb_ResultRenderer interface'
44                );
45            }
46        }
47
48        if ($resultform === false) {
49            $resultform = 'Default';
50        } else if ($resultform == 'xml') {
51            //kept for BC reasons
52            $resultform = 'XML';
53        }
54
55        if ($strClass = self::loadClass($resultform)) {
56            $rrObj = new $strClass();
57            if ($rrObj instanceof SparqlEngineDb_ResultRenderer) {
58                return $rrObj->convertFromDbResults(
59                    $arRecordSets,
60                    $engine->getQuery(),
61                    $engine
62                );
63            } else {
64                throw new Exception(
65                    'Result renderer class "' . $strClass . '" needs to implement'
66                    . ' SparqlEngineDb_ResultRenderer interface'
67                );
68            }
69        } else {
70            throw new Exception(
71                'Result renderer class "' . $resultform . '" could not be loaded.'
72            );
73        }
74    }//public static function convertFromDbResults($arRecordSets, SparqlEngineDb $engine, $resultform = false)
75
76
77
78    /**
79    *   Tries to load a given class if it doesn't exist,
80    *   and returns true if the class can be used.
81    *
82    *   @param string $strClass Classname
83    *   @return mixed Class name if the class is loaded and can be used, false if not.
84    */
85    protected static function loadClass($strClass)
86    {
87        if (class_exists($strClass, false)) {
88            return $strClass;
89        }
90
91        //RAP style, shortcut notation
92        $strFile = 'SparqlEngineDb/ResultRenderer/' . $strClass . '.php';
93        @include_once RDFAPI_INCLUDE_DIR . 'sparql/' . $strFile;
94        if (class_exists('SparqlEngineDb_ResultRenderer_' . $strClass, false)) {
95            return 'SparqlEngineDb_ResultRenderer_' . $strClass;
96        }
97
98        //RAP style
99        $strFile = str_replace('_', '/', $strClass) . '.php';
100        @include_once RDFAPI_INCLUDE_DIR . 'sparql/' . $strFile;
101        if (class_exists($strClass, false)) {
102            return $strClass;
103        }
104
105        //PEAR style
106        @include_once $strFile;
107        if (class_exists($strClass, false)) {
108            return $strClass;
109        }
110
111        return false;
112    }//protected static function loadClass($strClass)
113
114}//class SparqlEngineDb_ResultConverter
115?>
Note: See TracBrowser for help on using the repository browser.