Skip to content

Commit

Permalink
apacheGH-2932: Fix for NodeFmtLib.strNT and added variants for Triple…
Browse files Browse the repository at this point in the history
… and Quad.
  • Loading branch information
Aklakan authored and afs committed Jan 11, 2025
1 parent 5f904cc commit 762cd45
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
28 changes: 27 additions & 1 deletion jena-arq/src/main/java/org/apache/jena/riot/out/NodeFmtLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,40 @@ public static String str(Quad q) {
return strNodesTTL(q.getGraph(), q.getSubject(), q.getPredicate(), q.getObject());
}

/** Format a triple as N-Triples. */
public static String strNT(Triple triple) {
return strNQ(triple.getSubject(), triple.getPredicate(), triple.getObject(), null);
}

/** Format a quad as N-Quads. */
public static String strNQ(Quad quad) {
return strNQ(quad.getSubject(), quad.getPredicate(), quad.getObject(), quad.getGraph());
}

/** Format the components of a quad as N-Quads. The graph component may be null. */
public static String strNQ(Node s, Node p, Node o, Node g) {
StringBuilder result = new StringBuilder();
result.append(strNT(s));
result.append(" ");
result.append(strNT(p));
result.append(" ");
result.append(strNT(o));
if (g != null && !Quad.isDefaultGraph(g)) {
result.append(" ");
result.append(strNT(g));
}
result.append(" .");
return result.toString();
}

/** With Turtle abbreviation for literals, no prefixes of base URI */
public static String strTTL(Node node) {
return strNode(node, ttlFormatter);
}

/** Format in N-triples style. */
public static String strNT(Node node) {
return strNode(node, ttlFormatter);
return strNode(node, ntFormatter);
}

/** Format in N-triples style. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import static org.junit.Assert.assertEquals;

import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.sse.SSE;
import org.apache.jena.sparql.util.NodeFactoryExtra ;
import org.apache.jena.sys.JenaSystem;
import org.apache.jena.vocabulary.RDF ;
Expand Down Expand Up @@ -65,7 +69,7 @@ private void testencdec(String input)
@Test public void fmtNode_00() { testNT ("<a>", "<a>") ; }

@Test public void fmtNode_11() { testNT ("<"+RDF.getURI()+"type>", "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>") ; }
@Test public void fmtNode_12() { testNT ("'123'^^xsd:integer", "123") ; }
@Test public void fmtNode_12() { testNT ("'123'^^xsd:integer", "\"123\"^^<http://www.w3.org/2001/XMLSchema#integer>") ; }
@Test public void fmtNode_13() { testNT ("'abc'^^xsd:integer", "\"abc\"^^<http://www.w3.org/2001/XMLSchema#integer>") ; }

// NB - Not 'a' which is position sensitive.
Expand Down Expand Up @@ -103,4 +107,44 @@ private static void testDisplay(Node node, String output)
assertEquals(output, x) ;
}

@Test
public void strNT_triple() {
Triple triple = SSE.parseTriple("(<urn:s> rdf:type <urn:o>)");
assertEquals(
"<urn:s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:o> .",
NodeFmtLib.strNT(triple));
}

@Test
public void strNT_triple_bnode() {
// Note that <_:abc> actually parses as a blank node and preserves its label.
Triple triple = SSE.parseTriple("(<_:abc> rdf:type <urn:o>)");
assertEquals(
"_:Babc <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:o> .",
NodeFmtLib.strNT(triple));
}

@Test
public void strNT_triple_int() {
Triple triple = SSE.parseTriple("(<urn:s> <urn:value> 1)");
assertEquals(
"<urn:s> <urn:value> \"1\"^^<http://www.w3.org/2001/XMLSchema#integer> .",
NodeFmtLib.strNT(triple));
}

@Test
public void strNQ_quad_namedGraph() {
Quad quad = SSE.parseQuad("(<urn:g> <urn:s> rdf:type <urn:o>)");
assertEquals(
"<urn:s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:o> <urn:g> .",
NodeFmtLib.strNQ(quad));
}

@Test
public void strNQ_quad_defaultGraph() {
Quad quad = SSE.parseQuad("(_ <urn:s> rdf:type <urn:o>)");
assertEquals(
"<urn:s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:o> .",
NodeFmtLib.strNQ(quad));
}
}

0 comments on commit 762cd45

Please sign in to comment.