Search in sources :

Example 16 with Namespace

use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.

the class NamespacesTest method testAsMapMultiple.

/**
 * Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#asMap(java.util.Set)}.
 */
@Test
public final void testAsMapMultiple() {
    Set<Namespace> input = new HashSet<Namespace>();
    input.add(new SimpleNamespace(RDF.PREFIX, RDF.NAMESPACE));
    input.add(new SimpleNamespace(RDFS.PREFIX, RDFS.NAMESPACE));
    input.add(new SimpleNamespace(DC.PREFIX, DC.NAMESPACE));
    input.add(new SimpleNamespace(SKOS.PREFIX, SKOS.NAMESPACE));
    input.add(new SimpleNamespace(SESAME.PREFIX, SESAME.NAMESPACE));
    Map<String, String> map = Namespaces.asMap(input);
    assertFalse(map.isEmpty());
    assertEquals(5, map.size());
    assertTrue(map.containsKey(RDF.PREFIX));
    assertEquals(RDF.NAMESPACE, map.get(RDF.PREFIX));
    assertTrue(map.containsKey(RDFS.PREFIX));
    assertEquals(RDFS.NAMESPACE, map.get(RDFS.PREFIX));
    assertTrue(map.containsKey(DC.PREFIX));
    assertEquals(DC.NAMESPACE, map.get(DC.PREFIX));
    assertTrue(map.containsKey(SKOS.PREFIX));
    assertEquals(SKOS.NAMESPACE, map.get(SKOS.PREFIX));
    assertTrue(map.containsKey(SESAME.PREFIX));
    assertEquals(SESAME.NAMESPACE, map.get(SESAME.PREFIX));
}
Also used : SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 17 with Namespace

use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.

the class NamespacesTest method testWrapEntrySet.

/**
 * Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
 */
@Test
public final void testWrapEntrySet() throws Exception {
    Set<Namespace> testSet = new LinkedHashSet<Namespace>();
    Map<String, String> testMap = Namespaces.wrap(testSet);
    Set<Entry<String, String>> entrySet1 = testMap.entrySet();
    assertNotNull(entrySet1);
    assertTrue(entrySet1.isEmpty());
    testSet.add(new SimpleNamespace(testPrefix1, testName1));
    Set<Entry<String, String>> entrySet2 = testMap.entrySet();
    assertNotNull(entrySet2);
    assertFalse(entrySet2.isEmpty());
    assertEquals(1, entrySet2.size());
    Entry<String, String> nextEntry = entrySet2.iterator().next();
    assertEquals(testPrefix1, nextEntry.getKey());
    assertEquals(testName1, nextEntry.getValue());
    testSet.clear();
    Set<Entry<String, String>> entrySet3 = testMap.entrySet();
    assertNotNull(entrySet3);
    assertTrue(entrySet3.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Entry(java.util.Map.Entry) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Test(org.junit.Test)

Example 18 with Namespace

use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.

the class NamespacesTest method testWrapContainsKey.

/**
 * Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
 */
@Test
public final void testWrapContainsKey() throws Exception {
    Set<Namespace> testSet = new LinkedHashSet<Namespace>();
    Map<String, String> testMap = Namespaces.wrap(testSet);
    // Check no exceptions when calling containsKey on empty backing set
    assertFalse(testMap.containsKey(testPrefix1));
    testSet.add(new SimpleNamespace(testPrefix1, testName1));
    assertTrue(testMap.containsKey(testPrefix1));
    testSet.clear();
    assertFalse(testMap.containsKey(testPrefix1));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Test(org.junit.Test)

Example 19 with Namespace

use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.

the class JSONLDParserHandlerTest method writeJSONLD.

/**
 * Helper method to write the given model to JSON-LD and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeJSONLD(Model statements) throws RDFHandlerException {
    final StringWriter writer = new StringWriter();
    final RDFWriter jsonldWriter = new JSONLDWriter(writer);
    jsonldWriter.startRDF();
    for (final Namespace prefix : statements.getNamespaces()) {
        jsonldWriter.handleNamespace(prefix.getPrefix(), prefix.getName());
    }
    for (final Statement nextStatement : statements) {
        jsonldWriter.handleStatement(nextStatement);
    }
    jsonldWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : JSONLDWriter(org.eclipse.rdf4j.rio.jsonld.JSONLDWriter) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) Namespace(org.eclipse.rdf4j.model.Namespace)

Example 20 with Namespace

use of org.eclipse.rdf4j.model.Namespace in project rdf4j by eclipse.

the class Rio method write.

/**
 * Writes the given statements to the given {@link RDFHandler}.
 * <p>
 * If the collection is a {@link Model}, its namespaces will also be written.
 *
 * @param model
 *        A collection of statements, such as a {@link Model}, to be written.
 * @throws RDFHandlerException
 *         Thrown if there is an error writing the statements.
 */
public static void write(Iterable<Statement> model, RDFHandler writer) throws RDFHandlerException {
    writer.startRDF();
    if (model instanceof NamespaceAware) {
        for (Namespace nextNamespace : ((NamespaceAware) model).getNamespaces()) {
            writer.handleNamespace(nextNamespace.getPrefix(), nextNamespace.getName());
        }
    }
    for (final Statement st : model) {
        writer.handleStatement(st);
    }
    writer.endRDF();
}
Also used : NamespaceAware(org.eclipse.rdf4j.model.NamespaceAware) Statement(org.eclipse.rdf4j.model.Statement) Namespace(org.eclipse.rdf4j.model.Namespace)

Aggregations

Namespace (org.eclipse.rdf4j.model.Namespace)20 SimpleNamespace (org.eclipse.rdf4j.model.impl.SimpleNamespace)14 LinkedHashSet (java.util.LinkedHashSet)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 Statement (org.eclipse.rdf4j.model.Statement)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 AbstractMap (java.util.AbstractMap)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Literal (org.eclipse.rdf4j.model.Literal)1 NamespaceAware (org.eclipse.rdf4j.model.NamespaceAware)1 Value (org.eclipse.rdf4j.model.Value)1 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)1 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)1