Search in sources :

Example 11 with Namespace

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

the class ModelBuilder method convertPrefixedName.

/**
 * Convert the given prefixed name string to an IRI if possible.
 *
 * @param prefixedName
 *        a prefixed name string, e.g. "rdf:type"
 * @return the IRI corresponding to the prefixed name, or {@code null} if the supplied string couldn't be
 *         converted.
 */
private IRI convertPrefixedName(String prefixedName) {
    if (prefixedName.indexOf(':') < 0) {
        return null;
    }
    final String prefix = prefixedName.substring(0, prefixedName.indexOf(':'));
    final ValueFactory vf = SimpleValueFactory.getInstance();
    for (Namespace ns : model.getNamespaces()) {
        if (prefix.equals(ns.getPrefix())) {
            return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1));
        }
    }
    // try mapping using some of the default / well-known namespaces
    for (Namespace ns : getDefaultNamespaces()) {
        if (prefix.equals(ns.getPrefix())) {
            model.setNamespace(ns);
            return vf.createIRI(ns.getName(), prefixedName.substring(prefixedName.indexOf(':') + 1));
        }
    }
    return null;
}
Also used : ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) Namespace(org.eclipse.rdf4j.model.Namespace)

Example 12 with Namespace

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

the class RepositoryConnectionWrapper method exportStatements.

/**
 * Exports all statements contained in the supplied statement iterator and all relevant namespace
 * information to the supplied RDFHandler.
 */
protected void exportStatements(RepositoryResult<Statement> stIter, RDFHandler handler) throws RepositoryException, RDFHandlerException {
    try {
        handler.startRDF();
        // Export namespace information
        RepositoryResult<Namespace> nsIter = getNamespaces();
        try {
            while (nsIter.hasNext()) {
                Namespace ns = nsIter.next();
                handler.handleNamespace(ns.getPrefix(), ns.getName());
            }
        } finally {
            nsIter.close();
        }
        // Export statemnts
        while (stIter.hasNext()) {
            handler.handleStatement(stIter.next());
        }
        handler.endRDF();
    } finally {
        stIter.close();
    }
}
Also used : Namespace(org.eclipse.rdf4j.model.Namespace)

Example 13 with Namespace

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

the class NamespacesTest method testWrapKeySet.

/**
 * Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
 */
@Test
public final void testWrapKeySet() throws Exception {
    Set<Namespace> testSet = new LinkedHashSet<Namespace>();
    Map<String, String> testMap = Namespaces.wrap(testSet);
    Set<String> keySet1 = testMap.keySet();
    assertNotNull(keySet1);
    assertTrue(keySet1.isEmpty());
    testSet.add(new SimpleNamespace(testPrefix1, testName1));
    Set<String> keySet2 = testMap.keySet();
    assertNotNull(keySet2);
    assertFalse(keySet2.isEmpty());
    assertEquals(1, keySet2.size());
    String nextKey = keySet2.iterator().next();
    assertEquals(testPrefix1, nextKey);
    testSet.clear();
    Set<String> keySet3 = testMap.keySet();
    assertNotNull(keySet3);
    assertTrue(keySet3.isEmpty());
}
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 14 with Namespace

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

the class NamespacesTest method testWrapRemove.

/**
 * Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#wrap(java.util.Set)}.
 */
@Test
public final void testWrapRemove() throws Exception {
    Set<Namespace> testSet = new LinkedHashSet<Namespace>();
    Map<String, String> testMap = Namespaces.wrap(testSet);
    assertTrue(testMap.isEmpty());
    assertEquals(0, testMap.size());
    assertTrue(testSet.isEmpty());
    assertEquals(0, testSet.size());
    assertFalse(testMap.containsKey(testPrefix1));
    assertFalse(testMap.containsValue(testName1));
    // Directly add to Set, and then try to remove it using the Map
    testSet.add(new SimpleNamespace(testPrefix1, testName1));
    assertFalse(testMap.isEmpty());
    assertEquals(1, testMap.size());
    assertFalse(testSet.isEmpty());
    assertEquals(1, testSet.size());
    assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1)));
    assertTrue(testMap.containsKey(testPrefix1));
    assertTrue(testMap.containsValue(testName1));
    testSet.remove(new SimpleNamespace(testPrefix1, testName1));
    assertTrue(testMap.isEmpty());
    assertEquals(0, testMap.size());
    assertTrue(testSet.isEmpty());
    assertEquals(0, testSet.size());
    assertFalse(testMap.containsKey(testPrefix1));
    assertFalse(testMap.containsValue(testName1));
    testSet.clear();
    // Try again after clear
    testSet.add(new SimpleNamespace(testPrefix1, testName1));
    assertFalse(testMap.isEmpty());
    assertEquals(1, testMap.size());
    assertFalse(testSet.isEmpty());
    assertEquals(1, testSet.size());
    assertTrue(testSet.contains(new SimpleNamespace(testPrefix1, testName1)));
    assertTrue(testMap.containsKey(testPrefix1));
    assertTrue(testMap.containsValue(testName1));
    testSet.remove(new SimpleNamespace(testPrefix1, testName1));
    assertTrue(testMap.isEmpty());
    assertEquals(0, testMap.size());
    assertTrue(testSet.isEmpty());
    assertEquals(0, testSet.size());
    assertFalse(testMap.containsKey(testPrefix1));
    assertFalse(testMap.containsValue(testName1));
}
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 15 with Namespace

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

the class NamespacesTest method testAsMapOne.

/**
 * Test method for {@link org.eclipse.rdf4j.model.util.Namespaces#asMap(java.util.Set)}.
 */
@Test
public final void testAsMapOne() {
    Set<Namespace> input = new HashSet<Namespace>();
    input.add(new SimpleNamespace(RDF.PREFIX, RDF.NAMESPACE));
    Map<String, String> map = Namespaces.asMap(input);
    assertFalse(map.isEmpty());
    assertEquals(1, map.size());
    assertTrue(map.containsKey(RDF.PREFIX));
    assertEquals(RDF.NAMESPACE, map.get(RDF.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)

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