|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class NamespaceExposingTurtleParser 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
|
|
|
if ($this->column > 0) { |
|
62
|
|
|
$this->column -= mb_strlen($chars, "UTF-8"); |
|
63
|
|
|
$this->bytePos -= strlen($chars); |
|
64
|
|
|
if ($this->bytePos < 0) { |
|
65
|
|
|
$this->bytePos = 0; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
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.