|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class SkosmosTurtleParser extends EasyRdf_Parser_Turtle |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
private $bytePos = 0; |
|
6
|
|
|
private $dataLength = null; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Returns the namespace prefixes as an array of prefix => URI |
|
10
|
|
|
* @return array $namespaces |
|
11
|
|
|
*/ |
|
12
|
|
|
public function getNamespaces() |
|
13
|
|
|
{ |
|
14
|
|
|
return $this->namespaces; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A lot faster since we're now only reading the next 4 bytes |
|
19
|
|
|
* instead of the whole string. |
|
20
|
|
|
* Gets the next character to be returned by read(). |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function peek() |
|
23
|
|
|
{ |
|
24
|
|
|
if(!$this->dataLength) { $this->dataLength = strlen($this->data); } |
|
25
|
|
|
if ($this->dataLength > $this->bytePos) { |
|
26
|
|
|
$slice = substr($this->data, $this->bytePos, 4); |
|
27
|
|
|
return mb_substr($slice, 0, 1, "UTF-8"); |
|
28
|
|
|
} else { |
|
29
|
|
|
return -1; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Does not manipulate the data variable. Keeps track of the |
|
35
|
|
|
* byte position instead. |
|
36
|
|
|
* Read a single character from the input buffer. |
|
37
|
|
|
* Returns -1 when the end of the file is reached. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function read() |
|
40
|
|
|
{ |
|
41
|
|
|
$char = $this->peek(); |
|
42
|
|
|
if ($char == -1) { |
|
43
|
|
|
return -1; |
|
44
|
|
|
} |
|
45
|
|
|
$this->bytePos += strlen($char); |
|
46
|
|
|
// Keep tracks of which line we are on (0A = Line Feed) |
|
47
|
|
|
if ($char == "\x0A") { |
|
48
|
|
|
$this->line += 1; |
|
49
|
|
|
$this->column = 1; |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->column += 1; |
|
52
|
|
|
} |
|
53
|
|
|
return $char; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Steps back, restoring the previous character or statement read() to the input buffer. |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function unread($chars) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->column -= mb_strlen($chars, "UTF-8"); |
|
62
|
|
|
$this->bytePos -= strlen($chars); |
|
63
|
|
|
if ($this->bytePos < 0) { |
|
64
|
|
|
$this->bytePos = 0; |
|
65
|
|
|
} |
|
66
|
|
|
if ($this->column < 1) { |
|
67
|
|
|
$this->column = 1; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Reverse skips through whitespace in 4 byte increments. |
|
74
|
|
|
* (Keeps the byte pointer accurate when unreading.) |
|
75
|
|
|
* @ignore |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function unskipWS() |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->bytePos - 4 > 0) { |
|
80
|
|
|
$slice = substr($this->data, $this->bytePos - 4, 4); |
|
81
|
|
|
while($slice != '') { |
|
82
|
|
|
if (!self::isWhitespace(mb_substr($slice, -1, 1, "UTF-8"))) { |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
$slice = substr($slice, 0, -1); |
|
86
|
|
|
$this->bytePos -= 1; |
|
87
|
|
|
} |
|
88
|
|
|
// This 4 byte slice was full of whitespace. |
|
89
|
|
|
// We need to check that there isn't more in the next slice. |
|
90
|
|
|
$this->unSkipWS(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Parse triples with unskipWS (doesn't loose the pointer position in blank nodes). |
|
96
|
|
|
* @ignore |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function parseTriples() |
|
99
|
|
|
{ |
|
100
|
|
|
$c = $this->peek(); |
|
101
|
|
|
|
|
102
|
|
|
// If the first character is an open bracket we need to decide which of |
|
103
|
|
|
// the two parsing methods for blank nodes to use |
|
104
|
|
|
if ($c == '[') { |
|
105
|
|
|
$c = $this->read(); |
|
|
|
|
|
|
106
|
|
|
$this->skipWSC(); |
|
107
|
|
|
$c = $this->peek(); |
|
108
|
|
|
if ($c == ']') { |
|
109
|
|
|
$c = $this->read(); |
|
|
|
|
|
|
110
|
|
|
$this->subject = $this->createBNode(); |
|
111
|
|
|
$this->skipWSC(); |
|
112
|
|
|
$this->parsePredicateObjectList(); |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->unskipWS(); |
|
115
|
|
|
$this->unread('['); |
|
116
|
|
|
$this->subject = $this->parseImplicitBlank(); |
|
117
|
|
|
} |
|
118
|
|
|
$this->skipWSC(); |
|
119
|
|
|
$c = $this->peek(); |
|
120
|
|
|
|
|
121
|
|
|
// if this is not the end of the statement, recurse into the list of |
|
122
|
|
|
// predicate and objects, using the subject parsed above as the subject |
|
123
|
|
|
// of the statement. |
|
124
|
|
|
if ($c != '.') { |
|
125
|
|
|
$this->parsePredicateObjectList(); |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
|
|
$this->parseSubject(); |
|
129
|
|
|
$this->skipWSC(); |
|
130
|
|
|
$this->parsePredicateObjectList(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->subject = null; |
|
134
|
|
|
$this->predicate = null; |
|
135
|
|
|
$this->object = null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.