Search in sources :

Example 6 with Namespace

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

the class NotifyingRepositoryConnectionWrapper method clearNamespaces.

@Override
public void clearNamespaces() throws RepositoryException {
    if (activated && reportDeltas()) {
        RepositoryResult<Namespace> namespaces;
        namespaces = getDelegate().getNamespaces();
        List<String> prefix = new ArrayList<String>();
        try {
            while (namespaces.hasNext()) {
                Namespace ns = namespaces.next();
                prefix.add(ns.getPrefix());
            }
        } finally {
            namespaces.close();
        }
        getDelegate().clearNamespaces();
        for (String p : prefix) {
            removeNamespace(p);
        }
    } else if (activated) {
        getDelegate().clearNamespaces();
        for (RepositoryConnectionListener listener : listeners) {
            listener.clearNamespaces(getDelegate());
        }
    } else {
        getDelegate().clearNamespaces();
    }
}
Also used : RepositoryConnectionListener(org.eclipse.rdf4j.repository.event.RepositoryConnectionListener) ArrayList(java.util.ArrayList) Namespace(org.eclipse.rdf4j.model.Namespace)

Example 7 with Namespace

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

the class Namespaces method wrap.

/**
 * Wraps the given {@link Set} of {@link Namespace}s as a {@link Map} of prefix to URI mappings, so that
 * it can be used where a {@link Map} is required by the API. <br>
 * NOTE: The Map returned by this method is not synchronized.
 *
 * @param namespaces
 *        The Set to wrap.
 * @return A Map of prefix to URI mappings which is backed by the given Set of {@link Namespace}s.
 */
public static Map<String, String> wrap(final Set<Namespace> namespaces) {
    return new Map<String, String>() {

        @Override
        public void clear() {
            namespaces.clear();
        }

        @Override
        public boolean containsKey(Object nextKey) {
            if (nextKey instanceof String) {
                for (Namespace nextNamespace : namespaces) {
                    if (nextNamespace.getPrefix().equals(nextKey)) {
                        return true;
                    }
                }
            }
            return false;
        }

        @Override
        public boolean containsValue(Object nextValue) {
            if (nextValue instanceof String) {
                for (Namespace nextNamespace : namespaces) {
                    if (nextNamespace.getName().equals(nextValue)) {
                        return true;
                    }
                }
            }
            return false;
        }

        /**
         * NOTE: This entry set is immutable, and does not update the internal set through its iterator.
         */
        @Override
        public Set<java.util.Map.Entry<String, String>> entrySet() {
            Set<java.util.Map.Entry<String, String>> result = new LinkedHashSet<Map.Entry<String, String>>();
            for (Namespace nextNamespace : namespaces) {
                AbstractMap.SimpleImmutableEntry<String, String> nextEntry = new SimpleImmutableEntry<String, String>(nextNamespace.getPrefix(), nextNamespace.getName());
                result.add(nextEntry);
            }
            return Collections.unmodifiableSet(result);
        }

        @Override
        public String get(Object nextKey) {
            if (nextKey instanceof String) {
                for (Namespace nextNamespace : namespaces) {
                    if (nextNamespace.getPrefix().equals(nextKey)) {
                        return nextNamespace.getName();
                    }
                }
            }
            return null;
        }

        @Override
        public boolean isEmpty() {
            return namespaces.isEmpty();
        }

        @Override
        public Set<String> keySet() {
            Set<String> result = new LinkedHashSet<String>();
            for (Namespace nextNamespace : namespaces) {
                result.add(nextNamespace.getPrefix());
            }
            return result;
        }

        @Override
        public String put(String nextKey, String nextValue) {
            String result = null;
            for (Namespace nextNamespace : new LinkedHashSet<Namespace>(namespaces)) {
                if (nextNamespace.getPrefix().equals(nextKey)) {
                    result = nextNamespace.getName();
                    namespaces.remove(nextNamespace);
                }
            }
            namespaces.add(new SimpleNamespace(nextKey, nextValue));
            return result;
        }

        @Override
        public void putAll(Map<? extends String, ? extends String> nextSet) {
            for (Map.Entry<? extends String, ? extends String> nextEntry : nextSet.entrySet()) {
                put(nextEntry.getKey(), nextEntry.getValue());
            }
        }

        @Override
        public String remove(Object nextKey) {
            String result = null;
            for (Namespace nextNamespace : new LinkedHashSet<Namespace>(namespaces)) {
                if (nextNamespace.getPrefix().equals(nextKey)) {
                    result = nextNamespace.getName();
                    namespaces.remove(nextNamespace);
                }
            }
            return result;
        }

        @Override
        public int size() {
            return namespaces.size();
        }

        @Override
        public Collection<String> values() {
            List<String> result = new ArrayList<String>();
            for (Namespace nextNamespace : namespaces) {
                result.add(nextNamespace.getName());
            }
            return result;
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) AbstractMap(java.util.AbstractMap) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with Namespace

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

the class TreeModel method setNamespace.

@Override
public Namespace setNamespace(String prefix, String name) {
    removeNamespace(prefix);
    Namespace result = new SimpleNamespace(prefix, name);
    namespaces.add(result);
    return result;
}
Also used : Namespace(org.eclipse.rdf4j.model.Namespace)

Example 9 with Namespace

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

the class LinkedHashModel method setNamespace.

@Override
public Namespace setNamespace(String prefix, String name) {
    removeNamespace(prefix);
    Namespace result = new SimpleNamespace(prefix, name);
    namespaces.add(result);
    return result;
}
Also used : Namespace(org.eclipse.rdf4j.model.Namespace)

Example 10 with Namespace

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

the class HTTPRepositoryConnection method getNamespaces.

public RepositoryResult<Namespace> getNamespaces() throws RepositoryException {
    try {
        List<Namespace> namespaceList = new ArrayList<Namespace>();
        TupleQueryResult namespaces = client.getNamespaces();
        try {
            while (namespaces.hasNext()) {
                BindingSet bindingSet = namespaces.next();
                Value prefix = bindingSet.getValue("prefix");
                Value namespace = bindingSet.getValue("namespace");
                if (prefix instanceof Literal && namespace instanceof Literal) {
                    String prefixStr = ((Literal) prefix).getLabel();
                    String namespaceStr = ((Literal) namespace).getLabel();
                    namespaceList.add(new SimpleNamespace(prefixStr, namespaceStr));
                }
            }
        } finally {
            namespaces.close();
        }
        return createRepositoryResult(namespaceList);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } catch (IOException e) {
        throw new RepositoryException(e);
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Literal(org.eclipse.rdf4j.model.Literal) ArrayList(java.util.ArrayList) Value(org.eclipse.rdf4j.model.Value) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace)

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