| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Purtle; |
| 4 |
|
| 5 |
/** |
| 6 |
* @license GPL-2.0-or-later |
| 7 |
* @author Daniel Kinzler |
| 8 |
*/ |
| 9 |
class NTriplesRdfWriter extends N3RdfWriterBase { |
| 10 |
|
| 11 |
/** |
| 12 |
* @param string $role |
| 13 |
* @param BNodeLabeler|null $labeler |
| 14 |
* @param N3Quoter|null $quoter |
| 15 |
*/ |
| 16 |
public function __construct( |
| 17 |
$role = parent::DOCUMENT_ROLE, |
| 18 |
?BNodeLabeler $labeler = null, |
| 19 |
?N3Quoter $quoter = null |
| 20 |
) { |
| 21 |
parent::__construct( $role, $labeler, $quoter ); |
| 22 |
|
| 23 |
// NOTE: The RDF 1.1 spec of N-Triples allows full UTF-8, so escaping would not be required. |
| 24 |
// However, as of 2015, many consumers of N-Triples still expect non-ASCII characters |
| 25 |
// to be escaped. |
| 26 |
// NOTE: if this is changed, getMimeType must be changed accordingly. |
| 27 |
$this->quoter->setEscapeUnicode( true ); |
| 28 |
|
| 29 |
$this->transitionTable[self::STATE_OBJECT] = [ |
| 30 |
self::STATE_DOCUMENT => " .\n", |
| 31 |
self::STATE_SUBJECT => " .\n", |
| 32 |
self::STATE_PREDICATE => " .\n", |
| 33 |
self::STATE_OBJECT => " .\n", |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @inheritDoc |
| 39 |
*/ |
| 40 |
protected function expandSubject( &$base, &$local ) { |
| 41 |
$this->expandQName( $base, $local ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @inheritDoc |
| 46 |
*/ |
| 47 |
protected function writeSubject( $base, $local = null ) { |
| 48 |
// noop |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @inheritDoc |
| 53 |
*/ |
| 54 |
protected function expandPredicate( &$base, &$local ) { |
| 55 |
$this->expandShorthand( $base, $local ); // e.g. ( 'a', null ) => ( 'rdf', 'type' ) |
| 56 |
$this->expandQName( $base, $local ); // e.g. ( 'acme', 'foo' ) => ( 'http://acme.test/foo', null ) |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @inheritDoc |
| 61 |
*/ |
| 62 |
protected function writePredicate( $base, $local = null ) { |
| 63 |
// noop |
| 64 |
} |
| 65 |
|
| 66 |
private function writeSubjectAndObject() { |
| 67 |
$this->writeRef( $this->currentSubject[0], $this->currentSubject[1] ); |
| 68 |
$this->write( ' ' ); |
| 69 |
$this->writeRef( $this->currentPredicate[0], $this->currentPredicate[1] ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @inheritDoc |
| 74 |
*/ |
| 75 |
protected function expandResource( &$base, &$local ) { |
| 76 |
$this->expandQName( $base, $local ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @inheritDoc |
| 81 |
*/ |
| 82 |
protected function expandType( &$base, &$local ) { |
| 83 |
$this->expandQName( $base, $local ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @inheritDoc |
| 88 |
*/ |
| 89 |
protected function writeResource( $base, $local = null ) { |
| 90 |
$this->writeSubjectAndObject(); |
| 91 |
$this->write( ' ' ); |
| 92 |
$this->writeRef( $base, $local ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @inheritDoc |
| 97 |
*/ |
| 98 |
protected function writeText( $text, $language = null ) { |
| 99 |
$this->writeSubjectAndObject(); |
| 100 |
$this->write( ' ' ); |
| 101 |
|
| 102 |
parent::writeText( $text, $language ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param string $value |
| 107 |
* @param string|null $typeBase |
| 108 |
* @param string|null $typeLocal |
| 109 |
*/ |
| 110 |
protected function writeValue( $value, $typeBase, $typeLocal = null ) { |
| 111 |
$this->writeSubjectAndObject(); |
| 112 |
$this->write( ' ' ); |
| 113 |
|
| 114 |
parent::writeValue( $value, $typeBase, $typeLocal ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @param string $role |
| 119 |
* @param BNodeLabeler $labeler |
| 120 |
* |
| 121 |
* @return RdfWriterBase |
| 122 |
*/ |
| 123 |
protected function newSubWriter( $role, BNodeLabeler $labeler ) { |
| 124 |
$writer = new self( $role, $labeler, $this->quoter ); |
| 125 |
|
| 126 |
return $writer; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @return string a MIME type |
| 131 |
*/ |
| 132 |
public function getMimeType() { |
| 133 |
// NOTE: Add charset=UTF-8 if and when the constructor configures $this->quoter |
| 134 |
// to write utf-8. |
| 135 |
return 'application/n-triples'; |
| 136 |
} |
| 137 |
|
| 138 |
} |