diff --git a/src/N3RdfWriterBase.php b/src/N3RdfWriterBase.php index dfbe861..7d64aff 100644 --- a/src/N3RdfWriterBase.php +++ b/src/N3RdfWriterBase.php @@ -1,70 +1,71 @@ quoter = $quoter ?: new N3Quoter(); } protected function writeRef( $base, $local = null ) { if ( $local === null ) { if ( $base === 'a' ) { $this->write( 'a' ); } else { $this->writeIRI( $base ); } } else { $this->write( "$base:$local" ); } } protected function writeIRI( $iri, $trustIRI = false ) { if ( !$trustIRI ) { $iri = $this->quoter->escapeIRI( $iri ); } $this->write( "<$iri>" ); } protected function writeQName( $base, $local ) { $this->write( "$base:$local" ); } protected function writeText( $text, $language = null ) { $value = $this->quoter->escapeLiteral( $text ); $this->write( '"' . $value . '"' ); - if ( $language !== null ) { + // preg_match is somewhat (12%) slower than strspn but more readable + if ( $language !== null && preg_match( '/^[0-9A-Za-z-]+$/', $language ) ) { $this->write( '@' . $language ); } } protected function writeValue( $value, $typeBase = null, $typeLocal = null ) { $value = $this->quoter->escapeLiteral( $value ); $this->write( '"' . $value. '"' ); if ( $typeBase !== null ) { $this->write( '^^' ); $this->writeRef( $typeBase, $typeLocal ); } } } diff --git a/src/XmlRdfWriter.php b/src/XmlRdfWriter.php index dd6d244..7f417e9 100644 --- a/src/XmlRdfWriter.php +++ b/src/XmlRdfWriter.php @@ -1,221 +1,225 @@ transitionTable[self::STATE_START][self::STATE_DOCUMENT] = function() { $this->beginDocument(); }; $this->transitionTable[self::STATE_DOCUMENT][self::STATE_FINISH] = function() { $this->finishDocument(); }; $this->transitionTable[self::STATE_OBJECT][self::STATE_DOCUMENT] = function() { $this->finishSubject(); }; $this->transitionTable[self::STATE_OBJECT][self::STATE_SUBJECT] = function() { $this->finishSubject(); }; } private function escape( $text ) { return htmlspecialchars( $text, ENT_QUOTES ); } protected function expandSubject( &$base, &$local ) { $this->expandQName( $base, $local ); } protected function expandPredicate( &$base, &$local ) { $this->expandShorthand( $base, $local ); } protected function expandResource( &$base, &$local ) { $this->expandQName( $base, $local ); } protected function expandType( &$base, &$local ) { $this->expandQName( $base, $local ); } private function tag( $ns, $name, $attributes = array(), $content = null ) { $sep = $ns === '' ? '' : ':'; $this->write( '<' . $ns . $sep . $name ); foreach ( $attributes as $attr => $value ) { if ( is_int( $attr ) ) { // positional array entries are passed verbatim, may be callbacks. $this->write( $value ); continue; } $this->write( " $attr=\"" . $this->escape( $value ) . '"' ); } if ( $content === null ) { $this->write( '>' ); } elseif ( $content === '' ) { $this->write( '/>' ); } else { $this->write( '>' . $content ); $this->close( $ns, $name ); } } private function close( $ns, $name ) { $sep = $ns === '' ? '' : ':'; $this->write( '' ); } /** * Generates an attribute list, containing the attribute given by $name, or rdf:nodeID * if $target is a blank node id (starting with "_:"). If $target is a qname, an attempt * is made to resolve it into a full IRI based on the namespaces registered by calling * prefix(). * * @param string $name the attribute name (without the 'rdf:' prefix) * @param string|null $base * @param string|null $local * * @throws InvalidArgumentException * @return string[] */ private function getTargetAttributes( $name, $base, $local ) { if ( $base === null && $local === null ) { return array(); } // handle blank if ( $base === '_' ) { $name = 'nodeID'; $value = $local; } elseif ( $local !== null ) { throw new InvalidArgumentException( "Expected IRI, got QName: $base:$local" ); } else { $value = $base; } return array( "rdf:$name" => $value ); } /** * Emit a document header. */ private function beginDocument() { $this->write( "\n" ); // define a callback for generating namespace attributes $namespaceAttrCallback = function() { $attr = ''; $namespaces = $this->getPrefixes(); foreach ( $namespaces as $ns => $uri ) { $escapedUri = htmlspecialchars( $uri, ENT_QUOTES ); $nss = $ns === '' ? '' : ":$ns"; $attr .= " xmlns$nss=\"$escapedUri\""; } return $attr; }; $this->tag( 'rdf', 'RDF', array( $namespaceAttrCallback ) ); $this->write( "\n" ); } protected function writeSubject( $base, $local = null ) { $attr = $this->getTargetAttributes( 'about', $base, $local ); $this->write( "\t" ); $this->tag( 'rdf', 'Description', $attr ); $this->write( "\n" ); } /** * Emit the root element */ private function finishSubject() { $this->write( "\t" ); $this->close( 'rdf', 'Description' ); $this->write( "\n" ); } /** * Write document footer */ private function finishDocument() { // close document element $this->close( 'rdf', 'RDF' ); $this->write( "\n" ); } protected function writePredicate( $base, $local = null ) { // noop } protected function writeResource( $base, $local = null ) { $attr = $this->getTargetAttributes( 'resource', $base, $local ); $this->write( "\t\t" ); $this->tag( $this->currentPredicate[0], $this->currentPredicate[1], $attr, '' ); $this->write( "\n" ); } protected function writeText( $text, $language = null ) { - $attr = empty( $language ) ? array() : array( 'xml:lang' => $language ); + if ( !empty( $language ) && preg_match( '/^[0-9A-Za-z-]+$/', $language ) ) { + $attr = array( 'xml:lang' => $language ); + } else { + $attr = array(); + } $this->write( "\t\t" ); $this->tag( $this->currentPredicate[0], $this->currentPredicate[1], $attr, $this->escape( $text ) ); $this->write( "\n" ); } public function writeValue( $literal, $typeBase, $typeLocal = null ) { $attr = $this->getTargetAttributes( 'datatype', $typeBase, $typeLocal ); $this->write( "\t\t" ); $this->tag( $this->currentPredicate[0], $this->currentPredicate[1], $attr, $this->escape( $literal ) ); $this->write( "\n" ); } /** * @param string $role * @param BNodeLabeler $labeler * * @return RdfWriterBase */ protected function newSubWriter( $role, BNodeLabeler $labeler ) { $writer = new self( $role, $labeler ); return $writer; } /** * @return string a MIME type */ public function getMimeType() { return 'application/rdf+xml; charset=UTF-8'; } } diff --git a/tests/data/Texts.nt b/tests/data/Texts.nt index d927334..bf26a0e 100644 --- a/tests/data/Texts.nt +++ b/tests/data/Texts.nt @@ -1,2 +1,4 @@ "Bom"@de . "Bam"@en . + "Como estas"@es-419 . + "What?" . diff --git a/tests/data/Texts.rdf b/tests/data/Texts.rdf index e6d14ed..1268d8c 100644 --- a/tests/data/Texts.rdf +++ b/tests/data/Texts.rdf @@ -1,7 +1,9 @@ Bom Bam + Como estas + What? \ No newline at end of file diff --git a/tests/data/Texts.ttl b/tests/data/Texts.ttl index ccb4c2a..a602818 100644 --- a/tests/data/Texts.ttl +++ b/tests/data/Texts.ttl @@ -1,6 +1,8 @@ @prefix rdf: . @prefix xsd: . @prefix acme: . acme:Bongos acme:sounds "Bom"@de, - "Bam"@en . + "Bam"@en, + "Como estas"@es-419, + "What?" . diff --git a/tests/phpunit/RdfWriterTestBase.php b/tests/phpunit/RdfWriterTestBase.php index 72d339d..965572b 100644 --- a/tests/phpunit/RdfWriterTestBase.php +++ b/tests/phpunit/RdfWriterTestBase.php @@ -1,365 +1,368 @@ newWriter()->getMimeType(); $this->assertInternalType( 'string', $mimeType ); $this->assertRegExp( '/^\w+\/[\w-]+(\+xml)?(; charset=UTF-8)?$/', $mimeType ); } public function testTriples() { $writer = $this->newWriter(); $writer->prefix( 'acme', 'http://acme.test/' ); $writer->start(); $writer->about( 'http://foobar.test/Bananas' ) ->say( 'a' )->is( 'http://foobar.test/Fruit' ); // shorthand name "a" $writer->about( 'acme', 'Nuts' ) ->say( 'acme', 'weight' )->value( '5.5', 'xsd', 'decimal' ); // redundant about( 'acme', 'Nuts' ) $writer->about( 'acme', 'Nuts' ) ->say( 'acme', 'color' )->value( 'brown' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Triples', $rdf ); } public function testPredicates() { $writer = $this->newWriter(); $writer->prefix( '', 'http://acme.test/' ); // empty prefix $writer->start(); $writer->about( 'http://foobar.test/Bananas' ) ->a( 'http://foobar.test/Fruit' ) // shorthand function a() ->say( '', 'name' ) // empty prefix ->text( 'Banana' ) ->say( '', 'name' ) // redundant say( '', 'name' ) ->text( 'Banane', 'de' ); $writer->about( 'http://foobar.test/Apples' ) ->say( '', 'name' ) // subsequent call to say( '', 'name' ) for a different subject ->text( 'Apple' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Predicates', $rdf ); } public function testPredicates_drain() { $writer = $this->newWriter(); $writer->prefix( '', 'http://acme.test/' ); // empty prefix $writer->start(); $writer->about( 'http://foobar.test/Bananas' ) ->a( 'http://foobar.test/Fruit' ) // shorthand function a() ->say( '', 'name' ) // empty prefix ->text( 'Banana' ) ->say( '', 'name' ) // redundant say( '', 'name' ) ->text( 'Banane', 'de' ); $rdf1 = $writer->drain(); $this->assertNotEmpty( $rdf1 ); $writer->about( 'http://foobar.test/Apples' ) ->say( '', 'name' ) // subsequent call to say( '', 'name' ) for a different subject ->text( 'Apple' ); $writer->finish(); $rdf2 = $writer->drain(); $this->assertNotEmpty( $rdf2 ); $this->assertOutputLines( 'Predicates', $rdf1 . $rdf2 ); } public function testPredicates_sub() { $writer = $this->newWriter(); $writer->prefix( '', 'http://acme.test/' ); // empty prefix $writer->start(); $sub = $writer->sub(); // output of the sub writer will appear after the output of the main writer. $sub->about( 'http://foobar.test/Apples' ) ->say( '', 'name' ) // subsequent call to say( '', 'name' ) for a different subject ->text( 'Apple' ); $writer->about( 'http://foobar.test/Bananas' ) ->a( 'http://foobar.test/Fruit' ) // shorthand function a() ->say( '', 'name' ) // empty prefix ->text( 'Banana' ) ->say( '', 'name' ) // redundant say( '', 'name' ) ->text( 'Banane', 'de' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Predicates', $rdf ); } public function testPredicates_sub_drain() { $writer = $this->newWriter(); $writer->prefix( '', 'http://acme.test/' ); // empty prefix $writer->start(); $sub = $writer->sub(); $writer->about( 'http://foobar.test/Bananas' ) ->a( 'http://foobar.test/Fruit' ) // shorthand function a() ->say( '', 'name' ) // empty prefix ->text( 'Banana' ) ->say( '', 'name' ) // redundant say( '', 'name' ) ->text( 'Banane', 'de' ); $rdf1 = $writer->drain(); $this->assertNotEmpty( $rdf1 ); // sub-writer should still be usable after drain() $sub->about( 'http://foobar.test/Apples' ) ->say( '', 'name' ) // subsequent call to say( '', 'name' ) for a different subject ->text( 'Apple' ); $writer->finish(); $rdf2 = $writer->drain(); $this->assertNotEmpty( $rdf2 ); $this->assertOutputLines( 'Predicates', $rdf1 . $rdf2 ); } public function testValues() { $writer = $this->newWriter(); $writer->prefix( 'acme', 'http://acme.test/' ); $writer->start(); $writer->about( 'http://foobar.test/Bananas' ) ->say( 'acme', 'multi' ) ->value( 'A' ) ->value( 'B' ) ->value( 'C' ) ->say( 'acme', 'type' ) ->value( 'foo', 'acme', 'thing' ) ->value( '-5', 'xsd', 'integer' ) ->value( '-5', 'xsd', 'decimal' ) ->value( '-5', 'xsd', 'double' ) ->value( 'true', 'xsd', 'boolean' ) ->value( 'false', 'xsd', 'boolean' ) ->say( 'acme', 'autotype' ) ->value( -5 ) ->value( 3.14 ) ->value( true ) ->value( false ) ->say( 'acme', 'no-autotype' ) ->value( -5, 'xsd', 'decimal' ) ->value( 3.14, 'xsd', 'string' ) ->value( true, 'xsd', 'string' ) ->value( false, 'xsd', 'string' ) ->say( 'acme', 'shorthand' )->value( 'foo' ) ->say( 'acme', 'typed-shorthand' )->value( 'foo', 'acme', 'thing' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Values', $rdf ); } public function testResources() { $writer = $this->newWriter(); $writer->prefix( 'acme', 'http://acme.test/' ); $writer->start(); $writer->about( 'acme', 'Bongos' ) ->say( 'acme', 'sounds' ) ->is( 'acme', 'Bing' ) ->is( 'http://foobar.test/sound/Bang' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Resources', $rdf ); } public function testTexts() { $writer = $this->newWriter(); $writer->prefix( 'acme', 'http://acme.test/' ); $writer->start(); $writer->about( 'acme', 'Bongos' ) ->say( 'acme', 'sounds' ) ->text( 'Bom', 'de' ) - ->text( 'Bam', 'en' ); + ->text( 'Bam', 'en' ) + ->text( 'Como estas', 'es-419' ) + ->text( 'What?', 'bad tag' ) + ; $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Texts', $rdf ); } public function testNumbers() { $writer = $this->newWriter(); $writer->prefix( 'acme', 'http://acme.test/' ); $writer->start(); $writer->about( 'acme', 'Bongos' ) ->say( 'acme', 'stock' )->value( 5, 'xsd', 'integer' ) ->value( 7 ) ->about( 'acme', 'Tablas' ) ->say( 'acme', 'stock' )->value( 6 ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'Numbers', $rdf ); } public function testEricMiller() { // exampel taken from http://www.w3.org/2007/02/turtle/primer/ $writer = $this->newWriter(); $writer->prefix( 'contact', 'http://www.w3.org/2000/10/swap/pim/contact#' ); $writer->start(); $writer->about( 'http://www.w3.org/People/EM/contact#me' ) ->say( 'rdf', 'type' )->is( 'contact', 'Person' ) ->say( 'contact', 'fullName' )->text( 'Eric Miller' ) ->say( 'contact', 'mailbox' )->is( 'mailto:em@w3.org' ) ->say( 'contact', 'personalTitle' )->text( 'Dr.' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'EricMiller', $rdf ); } public function testLabeledBlankNode() { // exampel taken from http://www.w3.org/2007/02/turtle/primer/ $writer = $this->newWriter(); $writer->prefix( 'exterms', 'http://www.example.org/terms/' ); $writer->prefix( 'exstaff', 'http://www.example.org/staffid/' ); $writer->start(); $writer->about( 'exstaff', '85740' ) ->say( 'exterms', 'address' )->is( '_', $label = $writer->blank( 'johnaddress' ) ) ->about( '_', $label ) ->say( 'exterms', 'street' )->text( '1501 Grant Avenue' ) ->say( 'exterms', 'city' )->text( 'Bedfort' ) ->say( 'exterms', 'state' )->text( 'Massachusetts' ) ->say( 'exterms', 'postalCode' )->text( '01730' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'LabeledBlankNode', $rdf ); } public function testNumberedBlankNodes() { // exampel taken from http://www.w3.org/2007/02/turtle/primer/ $writer = $this->newWriter(); $writer->prefix( 'exterms', 'http://www.example.org/terms/' ); $writer->prefix( 'exstaff', 'http://www.example.org/staffid/' ); $writer->prefix( 'ex', 'http://example.org/packages/vocab#' ); $writer->start(); $writer->about( 'exstaff', 'Sue' ) ->say( 'exterms', 'publication' )->is( '_', $label1 = $writer->blank() ); $writer->about( '_', $label1 ) ->say( 'exterms', 'title' )->text( 'Antology of Time' ); $writer->about( 'exstaff', 'Jack' ) ->say( 'exterms', 'publication' )->is( '_', $label2 = $writer->blank() ); $writer->about( '_', $label2 ) ->say( 'exterms', 'title' )->text( 'Anthony of Time' ); $writer->finish(); $rdf = $writer->drain(); $this->assertOutputLines( 'NumberedBlankNode', $rdf ); } /** * @param string $datasetName * @param string[]|string $actual */ private function assertOutputLines( $datasetName, $actual ) { $path = __DIR__ . '/../data/' . $datasetName . '.' . $this->getFileSuffix(); $this->assertNTriplesEquals( file_get_contents( $path ), $actual, "Result mismatches data in $path" ); } /** * @param string[]|string $nTriples * * @return string[] Sorted alphabetically. */ protected function normalizeNTriples( $nTriples ) { if ( is_string( $nTriples ) ) { // Trim and ignore newlines at the end of the file only. $nTriples = explode( "\n", rtrim( $nTriples, "\n" ) ); } sort( $nTriples ); return $nTriples; } /** * @param string[]|string $expected * @param string[]|string $actual * @param string $message */ protected function assertNTriplesEquals( $expected, $actual, $message = '' ) { $expected = $this->normalizeNTriples( $expected ); $actual = $this->normalizeNTriples( $actual ); // Comparing $expected and $actual directly would show triples that are present in both but // shifted in position. That makes the output hard to read. Calculating the $missing and // $extra sets helps. $extra = array_diff( $actual, $expected ); $missing = array_diff( $expected, $actual ); // Cute: $missing and $extra can be equal only if they are empty. Comparing them here // directly looks a bit odd in code, but produces meaningful output, especially if the input // was sorted. $this->assertEquals( $missing, $extra, $message ); } //FIXME: test quoting/escapes! //FIXME: test non-ascii literals! //FIXME: test uerl-encoding //FIXME: test IRIs! }