org.openrdf.model.Namespace Java Examples

The following examples show how to use org.openrdf.model.Namespace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public List<PrefixNamespace> getNamespaces() {
    List<PrefixNamespace> namespaces = new ArrayList<>();
    RepositoryConnection cnx = null;
    try {
        cnx = repo.getConnection();
        RepositoryResult<Namespace> ns = cnx.getNamespaces();
        while (ns.hasNext()) {
            Namespace namespace = ns.next();
            namespaces.add(new PrefixNamespace(namespace.getPrefix(), namespace.getName()));
        }
    } catch (RepositoryException x) {
        throw new TripleStoreException("Getting namespaces", x);
    } finally {
        closeConnection(cnx, "Getting namespaces");
    }
    return namespaces;
}
 
Example #2
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetNamespaces()
	throws Exception
{
	con.begin();
	con.setNamespace("rdf", RDF.NAMESPACE);
	con.commit();

	CloseableIteration<? extends Namespace, SailException> namespaces = con.getNamespaces();
	try {
		assertTrue(namespaces.hasNext());
		Namespace rdf = namespaces.next();
		assertEquals("rdf", rdf.getPrefix());
		assertEquals(RDF.NAMESPACE, rdf.getName());
		assertTrue(!namespaces.hasNext());
	}
	finally {
		namespaces.close();
	}
}
 
Example #3
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Map<String,String> getNamespaces() {
	assertModel();
	Map<String,String> nsMap = new HashMap<String,String>();
	try {
		RepositoryResult<Namespace> openrdfMap = this.connection.getNamespaces();
		openrdfMap.enableDuplicateFilter();
		List<Namespace> openrdfList =  Iterations.asList(openrdfMap);
		for(Namespace openrdfNamespace : openrdfList) {
			nsMap.put(openrdfNamespace.getPrefix(), openrdfNamespace.getName());
		}
		return nsMap;
	} catch(RepositoryException e) {
		throw new ModelRuntimeException(e);
	}
}
 
Example #4
Source File: RepositoryModelSet.java    From semweb4j with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Map<String,String> getNamespaces() {
	this.assertModel();
	Map<String,String> nsMap = new HashMap<String,String>();
	try {
		RepositoryResult<Namespace> openrdfMap = this.connection.getNamespaces();
		openrdfMap.enableDuplicateFilter();
		while (openrdfMap.hasNext()) {
			Namespace openrdfNamespace = openrdfMap.next();
			nsMap.put(openrdfNamespace.getPrefix(), openrdfNamespace.getName());
		}
		return nsMap;
	} catch(RepositoryException e) {
		throw new ModelRuntimeException(e);
	}
}
 
Example #5
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private static void copyNamespaces(RepositoryConnection source, RepositoryConnection target) {
    try {
        RepositoryResult<Namespace> ns = source.getNamespaces();
        for (Namespace namespace : Iterations.asList(ns)) {
            target.setNamespace(namespace.getPrefix(), namespace.getName());
        }
    } catch (RepositoryException e) {
        LOG.error("Copying Namespaces : {}", e.getMessage());
    }
}
 
Example #6
Source File: OWLCompiler.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private String findPrefix(String ns, Model model) {
	for (Namespace e : model.getNamespaces()) {
		if (ns.equals(e.getName()) && e.getPrefix().length() > 0) {
			return e.getPrefix();
		}
	}
	if (resolvingPrefix) {
		String prefix = NamespacePrefixService.getInstance().prefix(ns);
		if (prefix != null && model.getNamespace(prefix) == null) {
			model.setNamespace(prefix, ns);
			return prefix;
		}
	}
	return "ns" + Integer.toHexString(ns.hashCode());
}
 
Example #7
Source File: RDFModelParser.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
private Namespaces getNamespaces(RepositoryConnection connection) throws RepositoryException {
	Namespaces ns = new Namespaces();
	RepositoryResult<Namespace> rr = null;
	try {
		rr=connection.getNamespaces();
		while(rr.hasNext()) {
			Namespace n=rr.next();
			ns.addPrefix(n.getPrefix(), n.getName());
		}
	} finally {
		closeQuietly(rr,"Could not close results after retrieving namespaces");
	}
	return ns;
}
 
Example #8
Source File: TripleStoreBlazegraph.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
private static void copyNamespacesToModel(RepositoryConnection conn, Model m) throws RepositoryException {
    RepositoryResult<Namespace> ns = conn.getNamespaces();
    while (ns.hasNext()) {
        m.setNamespace(ns.next());
    }
}
 
Example #9
Source File: BigdataSailRemoteRepositoryConnection.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
public RepositoryResult<Namespace> getNamespaces()
		throws RepositoryException {
	throw new UnsupportedOperationException();
}
 
Example #10
Source File: NamespacesHandler.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param repository the Repository object
 * @param request the HttpServletRequest object 
 * @param response the HttpServletResponse oject
 * @return QueryResultView object include the namespaces
 * @throws ClientHTTPException throws when errors in the request
 * @throws ServerHTTPException throws when errors in the Repository
 * @throws RepositoryException throws when errors in closing the RepositoryConnection 
 */
private ModelAndView getExportNamespacesResult(final Repository repository, final HttpServletRequest request, final HttpServletResponse response)
		throws ClientHTTPException, ServerHTTPException, RepositoryException {
	final boolean headersOnly = METHOD_HEAD.equals(request.getMethod());

	Map<String, Object> model = new HashMap<String, Object>();
	if (!headersOnly) {
		List<String> columnNames = Arrays.asList("prefix", "namespace");
		List<BindingSet> namespaces = new ArrayList<BindingSet>();

		RepositoryConnection repositoryCon = repository.getConnection();
		synchronized (repositoryCon) {
			try {
				CloseableIteration<? extends Namespace, RepositoryException> iter = repositoryCon.getNamespaces();

				try {
					while (iter.hasNext()) {
						Namespace ns = iter.next();

						Literal prefix = new LiteralImpl(ns.getPrefix());
						Literal namespace = new LiteralImpl(ns.getName());

						BindingSet bindingSet = new ListBindingSet(columnNames, prefix, namespace);
						namespaces.add(bindingSet);
					}
				} finally {
					iter.close();
				}
			} catch (RepositoryException e) {
				throw new ServerHTTPException("Repository error: " + e.getMessage(), e);
			}
		}
		model.put(QueryResultView.QUERY_RESULT_KEY, new TupleQueryResultImpl(columnNames, namespaces));
		repositoryCon.close();
	}

	TupleQueryResultWriterFactory factory = ProtocolUtil.getAcceptableService(request, response,
			TupleQueryResultWriterRegistry.getInstance());

	model.put(QueryResultView.FILENAME_HINT_KEY, "namespaces");
	model.put(QueryResultView.HEADERS_ONLY, headersOnly);
	model.put(QueryResultView.FACTORY_KEY, factory);

	return new ModelAndView(TupleQueryResultView.getInstance(), model);
}
 
Example #11
Source File: BigdataSail.java    From database with GNU General Public License v2.0 3 votes vote down vote up
public Namespace next() {
 
    final Map.Entry<String/*prefix*/,String/*namespace*/> current = src.next();
    
    return new NamespaceImpl(current.getKey(), current.getValue());
    
}