source: Dev/branches/rest-dojo-ui/Demo/rdfapi/sparql/SparqlEngine/ResultConverter.php @ 312

Last change on this file since 312 was 312, checked in by jkraaijeveld, 13 years ago
File size: 4.0 KB
Line 
1<?php
2require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngine/ResultRenderer.php';
3
4/**
5*   Converts a memory 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 SparqlEngine_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 SparqlEngine_ResultRenderer interface
21    *   - a string like "HTML" or "XML". The appropriate renderer is used then.
22    *   - a full class name, e.g. SparqlEngine_ResultRenderer_XML
23    *
24    *   @param array $arVartable        Variable table
25    *   @param SparqlEngine $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 convertFromResult($arVartable, SparqlEngine $engine, $resultform = false)
32    {
33        if (is_object($resultform)) {
34            if ($resultform instanceof SparqlEngine_ResultRenderer) {
35                return $resultform->convertFromResult(
36                    $arVartable,
37                    $engine->getQuery(),
38                    $engine
39                );
40            } else {
41                throw new Exception(
42                    'Result renderer object needs to implement'
43                    . ' SparqlEngine_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 SparqlEngine_ResultRenderer) {
58                return $rrObj->convertFromResult(
59                    $arVartable,
60                    $engine->getQuery(),
61                    $engine
62                );
63            } else {
64                throw new Exception(
65                    'Result renderer class "' . $strClass . '" needs to implement'
66                    . ' SparqlEngine_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 convertFromResult($arVartable, SparqlEngine $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 = 'SparqlEngine/ResultRenderer/' . $strClass . '.php';
93        @include_once RDFAPI_INCLUDE_DIR . 'sparql/' . $strFile;
94        if (class_exists('SparqlEngine_ResultRenderer_' . $strClass, false)) {
95            return 'SparqlEngine_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 SparqlEngine_ResultConverter
115?>
Note: See TracBrowser for help on using the repository browser.