source: Dev/branches/rest-dojo-ui/server/rdfapi/syntax/TriXParser.php @ 289

Last change on this file since 289 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: 3.0 KB
Line 
1<?php
2// ----------------------------------------------------------------------------------
3// Class: TriXParser
4// ----------------------------------------------------------------------------------
5
6/**
7* Temporary implementation of a TriX-Parser (Usable only with PHP > V5)
8* Currently, it doesn't support any namespaces and has problems with typed literals.
9* So this parser only works with TRIX documents where the default namespace is the TRIX namespace.
10*
11* @version  $Id$
12* @author Daniel Westphal (http://www.d-westphal.de)
13*
14* @package      dataset
15* @access       public
16**/
17class TriXParser 
18{
19        /**
20        * Reference to the graphSet
21        *
22        * @var          GraphSet
23        * @access       private
24        */
25        var $graphSet;
26       
27        /**
28        * Constructor
29        * Needs a reference to a graphSet
30        *
31        * @param    GraphSet
32        * @access       public
33        */             
34        function TriXParser(&$graphSet)
35        {
36                $this->graphSet=&$graphSet;     
37        }
38
39        /**
40        * Parse an XML string
41        *
42        * @param   string
43        * @access       public
44        */
45        function parseString($string)
46        {
47                $this->_populateGraphSet(simplexml_load_string($string));
48        }
49       
50        /**
51        * Parse from a file
52        *
53        * @param   string
54        * @access       public
55        */
56        function parseFile($pathToFile)
57        {
58                $this->_populateGraphSet(simplexml_load_file($pathToFile));
59        }
60       
61        /**
62        * Populates the graphSet with namedGraphs and triples.
63        *
64        * @param   object simpleXMLModel  $xmlModel
65        * @access       private
66        */
67        function _populateGraphSet(&$xmlModel)
68        {
69                $defaultGraphOccurred=false;
70               
71                foreach ($xmlModel->graph as $graph)
72                {
73                        if (isset($graph->uri))
74                        {
75                                $graphName=(string)$graph->uri;
76                                $namedGraph=& $this->graphSet->getNamedGraph($graphName);
77                                if ($namedGraph ==null)
78                                        $namedGraph=& $this->graphSet->createGraph($graphName);
79                        } else
80                        {
81                                if ($defaultGraphOccurred)
82                                        trigger_error('Only one unnamed Graph per file allowed', E_USER_ERROR);
83                               
84                                $namedGraph=& $this->graphSet->getDefaultGraph();
85                                $defaultGraphOccurred=true;
86                        }       
87                                               
88                        foreach ($graph->triple as $triple)
89                        {
90                                $tripleCount=0;
91                                $tripleArray=array();
92                                foreach ($triple->children() as $tag => $value)
93                                {
94                                        $tripleArray[$tripleCount++]=$this->_element2Resource((string)$tag,$value);
95                                };
96                                $namedGraph->add(new Statement($tripleArray[0],$tripleArray[1],$tripleArray[2]));       
97                        };
98                };
99        }
100       
101        /**
102        * return a mathing resource tyoe
103        *
104        * @param   string 
105        * @param   object simpleXMLNode $value
106        * @access       private
107        */
108        function _element2Resource($tag,$value)
109        {
110                switch ($tag)
111                {
112                                case 'uri':
113                                        return new Resource((string)$value);
114                                break;
115               
116                                case 'id':
117                                        return new BlankNode((string)$value);
118                                break;
119                               
120                                case 'typedLiteral':
121                                        $literal=new Literal((string)$value);
122                                        $literal->setDatatype((string)$value['datatype']);
123                                        return $literal;
124                                break;
125                                       
126                                case 'plainLiteral':
127                                        $literal=new Literal((string)$value);
128                                        if(isset($value['xml:lang']))
129                                                $literal->setLanguage((string)$value['xml:lang']);
130                                        return $literal;
131                                break;
132                }
133        }
134}
135?>
Note: See TracBrowser for help on using the repository browser.