📄 localrepository.java
字号:
throws IOException, AccessDeniedException { if (this == repository) { // Adding all data from this to this is useless. This special case // is handled to prevent potential deadlocks. return; } InputStream dataStream = repository.extractRDF(RDFFormat.RDFXML, true, true, true, false); try { addData(dataStream, "foo:bar", RDFFormat.RDFXML, false, listener); } finally { dataStream.close(); } } // implements SesameRepository.addData(Reader, String, RDFFormat, boolean, AdminListener) public void addData(Reader reader, String baseURI, RDFFormat format, boolean verifyData, AdminListener listener) throws IOException, AccessDeniedException { _ensureWriteAccess(); _ensureRdfAdminCreated(); try { _rdfAdmin.addRdfModel(reader, baseURI, listener, format, verifyData); } catch (UpdateException e) { listener.error("error while adding new triples: " + e.getMessage(), -1, -1, null); } } // implements SesameRepository.addData(InputStream, String, RDFFormat, boolean, AdminListener) public void addData(InputStream dataStream, String baseURI, RDFFormat format, boolean verifyData, AdminListener listener) throws IOException, AccessDeniedException { _ensureWriteAccess(); _ensureRdfAdminCreated(); try { _rdfAdmin.addRdfModel(dataStream, baseURI, listener, format, verifyData); } catch (UpdateException e) { listener.error("error while adding new triples: " + e.getMessage(), -1, -1, null); } } // implements SesameRepository.extractData(RDFFormat, boolean, boolean, boolean, boolean) public InputStream extractRDF(RDFFormat format, boolean ontology, boolean instances, boolean explicitOnly, boolean niceOutput) throws IOException, AccessDeniedException { _ensureReadAccess(); ByteArrayOutputStream baos = new ByteArrayOutputStream(8092); RdfDocumentWriter rdfDocWriter = null; if (RDFFormat.RDFXML.equals(format)) { rdfDocWriter = new RdfXmlWriter(baos); } else if (RDFFormat.NTRIPLES.equals(format)) { rdfDocWriter = new NTriplesWriter(baos); } else if (RDFFormat.N3.equals(format)) { rdfDocWriter = new N3Writer(baos); } else if (RDFFormat.TURTLE.equals(format)) { rdfDocWriter = new TurtleWriter(baos); } extractRDF(rdfDocWriter, ontology, instances, explicitOnly, niceOutput); return new ByteArrayInputStream(baos.toByteArray()); } /** * Extracts data from the repository and reports the triples to the supplied * <tt>RdfDocumentWriter</tt>. * * @param rdfDocWriter The <tt>RdfDocumentWriter</tt> to report the triples to. * @param ontology If <tt>true</tt> the ontological statements will be extracted. * @param instances If <tt>true</tt> the instance (non-schema) statements will be extracted. * @param explicitOnly If <tt>true</tt>, only the explicitly added statements will be extracted. * @param niceOutput If <tt>true</tt>, the extracted statements will be sorted by their subject. **/ public void extractRDF(RdfDocumentWriter rdfDocWriter, boolean ontology, boolean instances, boolean explicitOnly, boolean niceOutput) throws IOException, AccessDeniedException { _ensureReadAccess(); if (_rdfExport == null) { _rdfExport = new RdfExport(); } if (_rdfSource instanceof RdfSchemaSource) { _rdfExport.exportRdf((RdfSchemaSource)_rdfSource, rdfDocWriter, ontology, instances, explicitOnly, niceOutput); } else { _rdfExport.exportRdf(_rdfSource, rdfDocWriter, niceOutput); } } // implements SesameRepository.removeStatements(Resource, URI, Value, AdminListener) public void removeStatements(Resource subject, URI predicate, Value object, AdminListener listener) throws IOException, AccessDeniedException { _ensureWriteAccess(); _ensureRdfAdminCreated(); try { _rdfAdmin.removeStatements(subject, predicate, object, listener); } catch (UpdateException e) { listener.error("error while removing triples: " + e.getMessage(), -1, -1, null); } } // implements SesameRepository.clear(AdminListener) public void clear(AdminListener listener) throws IOException, AccessDeniedException { _ensureWriteAccess(); _ensureRdfAdminCreated(); try { _rdfAdmin.clearRepository(listener); } catch (UpdateException e) { listener.error("Update Exception: " + e.getMessage(), -1, -1, null); // this should never happen: such an exception is only thrown // when no transaction was started. throw new RuntimeException(e); } catch (SailInternalException e) { listener.error("Internal error: " + e.getMessage(), -1, -1, null); throw new IOException(e.getMessage()); } } /** * Gets the SAIL object of this repository. Note that no security * restrictions are placed on the Sail; it is the developer's responsibility * to check access restrictions before doing operations on the Sail. */ public Sail getSail() { return _rdfSource; } /** * Shuts down the repository. Once shut down, the repository can no longer * be queried or modified. **/ public synchronized void shutDown() { if (_rdfSource != null) { _rdfSource.shutDown(); _rdfSource = null; _serqlQueryEngine = null; _rqlQueryEngine = null; _rdqlQueryEngine = null; _rdfAdmin = null; _rdfExport = null; } } // implements SesameRepository.mergeGraph(Graph) public void mergeGraph(Graph graph) throws IOException, AccessDeniedException { _ensureWriteAccess(); // Write access given, therefore Sail is an RdfRepository. RdfRepository thisRep = (RdfRepository)_rdfSource; StatementIterator iter = graph.getStatements(); try { thisRep.startTransaction(); while (iter.hasNext()) { Statement st = iter.next(); Resource subject = st.getSubject(); URI predicate = st.getPredicate(); Value object = st.getObject(); thisRep.addStatement(subject, predicate, object); } } catch (SailUpdateException e) { // FIXME is this a correct conversion? throw new IOException(e.getMessage()); } finally { thisRep.commitTransaction(); iter.close(); } } // implements SesameRepository.mergeGraph(QueryLanguage, String) public void mergeGraph(QueryLanguage language, String query) throws IOException, AccessDeniedException { try { Graph graph = performGraphQuery(language, query); mergeGraph(graph); } catch (QueryEvaluationException e) { throw new IOException(e.getMessage()); } catch (MalformedQueryException e) { throw new IOException(e.getMessage()); } } // implements SesameRepository.addGraph(Graph) public void addGraph(Graph graph) throws IOException, AccessDeniedException { addGraph(graph, JOIN_BLANKNODES_DEFAULT); } public void addGraph(Graph graph, boolean joinBlankNodes) throws IOException, AccessDeniedException { _ensureWriteAccess(); // Write access given, therefore Sail is an RdfRepository. RdfRepository thisRep = (RdfRepository)_rdfSource; Map bNodesMap = null; ValueFactory factory = null; if (!joinBlankNodes) { // addGraph needs to create new blank nodes to avoid accidental // merging of blank nodes in the repository. bNodesMap = new HashMap(); factory = thisRep.getValueFactory(); } StatementIterator iter = graph.getStatements(); try { thisRep.startTransaction(); while (iter.hasNext()) { Statement st = iter.next(); Resource subject = st.getSubject(); URI predicate = st.getPredicate(); Value object = st.getObject(); if (!joinBlankNodes) { if (subject instanceof BNode) { String bNodeId = ((BNode)subject).getID(); if (bNodesMap.containsKey(bNodeId)) { // bnode was mapped before, reuse subject = (Resource)bNodesMap.get(bNodeId); } else { // create a new blank node and add it to the mapping. subject = factory.createBNode(); bNodesMap.put(bNodeId, subject); } } if (object instanceof BNode) { String bNodeId = ((BNode)object).getID(); if (bNodesMap.containsKey(bNodeId)) { // bnode was mapped before, reuse object = (Resource)bNodesMap.get(bNodeId); } else { // create a new blank node and add it to the mapping. object = factory.createBNode(); bNodesMap.put(bNodeId, object); } } } thisRep.addStatement(subject, predicate, object); } } catch (SailUpdateException e) { // FIXME is this a correct conversion? throw new IOException(e.getMessage()); } finally { thisRep.commitTransaction(); iter.close(); } } // implements SesameRepository.removeGraph(Graph) public void removeGraph(Graph graph) throws IOException, AccessDeniedException { _ensureWriteAccess(); // Write access given, therefore Sail is an RdfRepository. RdfRepository thisRep = (RdfRepository)_rdfSource; StatementIterator iter = graph.getStatements(); try { thisRep.startTransaction(); while (iter.hasNext()) { Statement st = iter.next(); thisRep.removeStatements(st.getSubject(), st.getPredicate(), st.getObject()); } } catch (SailUpdateException e) { // FIXME is this a correct conversion? throw new IOException(e.getMessage()); } finally { thisRep.commitTransaction(); iter.close(); } } /** * Creates a Graph representation of this repository. Note that any changes * on the Graph object will be reflected in this repository immediately! * * @return a Graph object that uses this repository as the backing store. * * @throws AccessDeniedException In case one does not have both read and * write access on this repository. */ public Graph getGraph() throws AccessDeniedException { _ensureReadAccess(); _ensureWriteAccess(); return new GraphImpl(this); } // implements SesameRepository.addGraph(QueryLanguage, String) public void addGraph(QueryLanguage language, String query) throws IOException, AccessDeniedException { addGraph(language, query, JOIN_BLANKNODES_DEFAULT); } public void addGraph(QueryLanguage language, String query, boolean joinBlankNodes) throws IOException, AccessDeniedException { try { Graph graph = performGraphQuery(language, query); addGraph(graph, joinBlankNodes); } catch (QueryEvaluationException e) { throw new IOException(e.getMessage()); } catch (MalformedQueryException e) { throw new IOException(e.getMessage()); } } // implements SesameRepository.removeGraph(QueryLanguage, String) public void removeGraph(QueryLanguage language, String query) throws IOException, AccessDeniedException { try { Graph graph = performGraphQuery(language, query); removeGraph(graph); } catch (QueryEvaluationException e) { throw new IOException(e.getMessage()); } catch (MalformedQueryException e) { throw new IOException(e.getMessage()); } } public String getRepositoryId() { return _id; } public void addListener(LocalRepositoryChangedListener listener) { synchronized(_listeners) { _listeners.add(listener); } // end synchronized block } public void removeListener(LocalRepositoryChangedListener listener) { synchronized(_listeners) { _listeners.remove(listener); } // end synchronized block } // listener notifications private void _notifyRepositoryChange(LocalRepositoryChangedEvent event) { synchronized(_listeners) { Iterator listeners = _listeners.iterator(); while (listeners.hasNext()) { LocalRepositoryChangedListener listener = (LocalRepositoryChangedListener)listeners.next(); listener.repositoryChanged(event); } } // end synchronized block } /* (non-Javadoc) * @see org.openrdf.sesame.sail.SailChangedListener#sailChanged(org.openrdf.sesame.sail.SailChangedEvent) */ public void sailChanged(SailChangedEvent event) { LocalRepositoryChangedEvent lrcEvent = new LocalRepositoryChangedEventImpl( event.statementsAdded(), event.statementsRemoved()); _notifyRepositoryChange(lrcEvent); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -