001: /*
002: * Copyright James Leigh (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.base;
007:
008: import info.aduna.iteration.Iteration;
009:
010: import java.io.File;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.io.Reader;
014: import java.net.URL;
015:
016: import org.openrdf.model.Namespace;
017: import org.openrdf.model.Resource;
018: import org.openrdf.model.Statement;
019: import org.openrdf.model.URI;
020: import org.openrdf.model.Value;
021: import org.openrdf.query.BooleanQuery;
022: import org.openrdf.query.GraphQuery;
023: import org.openrdf.query.MalformedQueryException;
024: import org.openrdf.query.Query;
025: import org.openrdf.query.QueryLanguage;
026: import org.openrdf.query.TupleQuery;
027: import org.openrdf.repository.DelegatingRepositoryConnection;
028: import org.openrdf.repository.Repository;
029: import org.openrdf.repository.RepositoryConnection;
030: import org.openrdf.repository.RepositoryException;
031: import org.openrdf.repository.RepositoryResult;
032: import org.openrdf.repository.base.RepositoryConnectionBase;
033: import org.openrdf.rio.RDFFormat;
034: import org.openrdf.rio.RDFHandler;
035: import org.openrdf.rio.RDFHandlerException;
036: import org.openrdf.rio.RDFParseException;
037:
038: /**
039: * Delegates all calls to the delegate RepositoryConnection. Conditionally
040: * processes add/remove/read to common base method to make them easier to
041: * override.
042: *
043: * @author James Leigh
044: * @see #isDelegatingAdd()
045: * @see #isDelegatingRemove()
046: * @see #isDelegatingRead()
047: */
048: public class RepositoryConnectionWrapper extends
049: RepositoryConnectionBase implements
050: DelegatingRepositoryConnection {
051:
052: private RepositoryConnection delegate;
053:
054: public RepositoryConnectionWrapper(Repository repository) {
055: super (repository);
056: }
057:
058: public RepositoryConnectionWrapper(Repository repository,
059: RepositoryConnection delegate) {
060: this (repository);
061: setDelegate(delegate);
062: }
063:
064: public RepositoryConnection getDelegate()
065: throws RepositoryException {
066: return delegate;
067: }
068:
069: public void setDelegate(RepositoryConnection delegate) {
070: this .delegate = delegate;
071: }
072:
073: /**
074: * If true then each add method will call
075: * {@link #addWithoutCommit(Resource, URI, Value, Resource[])}.
076: *
077: * @return <code>false</code>
078: * @throws RepositoryException
079: */
080: protected boolean isDelegatingAdd() throws RepositoryException {
081: return true;
082: }
083:
084: /**
085: * If true then the has/export/isEmpty methods will call
086: * {@link #getStatements(Resource, URI, Value, boolean, Resource[])}.
087: *
088: * @return <code>false</code>
089: * @throws RepositoryException
090: */
091: protected boolean isDelegatingRead() throws RepositoryException {
092: return true;
093: }
094:
095: /**
096: * If true then each remove method will call
097: * {@link #removeWithoutCommit(Resource, URI, Value, Resource[])}.
098: *
099: * @return <code>false</code>
100: * @throws RepositoryException
101: */
102: protected boolean isDelegatingRemove() throws RepositoryException {
103: return true;
104: }
105:
106: @Override
107: public void add(File file, String baseURI, RDFFormat dataFormat,
108: Resource... contexts) throws IOException,
109: RDFParseException, RepositoryException {
110: if (isDelegatingAdd()) {
111: getDelegate().add(file, baseURI, dataFormat, contexts);
112: } else {
113: super .add(file, baseURI, dataFormat, contexts);
114: }
115: }
116:
117: @Override
118: public void add(InputStream in, String baseURI,
119: RDFFormat dataFormat, Resource... contexts)
120: throws IOException, RDFParseException, RepositoryException {
121: if (isDelegatingAdd()) {
122: getDelegate().add(in, baseURI, dataFormat, contexts);
123: } else {
124: super .add(in, baseURI, dataFormat, contexts);
125: }
126: }
127:
128: @Override
129: public void add(Iterable<? extends Statement> statements,
130: Resource... contexts) throws RepositoryException {
131: if (isDelegatingAdd()) {
132: getDelegate().add(statements, contexts);
133: } else {
134: super .add(statements, contexts);
135: }
136: }
137:
138: @Override
139: public <E extends Exception> void add(
140: Iteration<? extends Statement, E> statementIter,
141: Resource... contexts) throws RepositoryException, E {
142: if (isDelegatingAdd()) {
143: getDelegate().add(statementIter, contexts);
144: } else {
145: super .add(statementIter, contexts);
146: }
147: }
148:
149: @Override
150: public void add(Reader reader, String baseURI,
151: RDFFormat dataFormat, Resource... contexts)
152: throws IOException, RDFParseException, RepositoryException {
153: if (isDelegatingAdd()) {
154: getDelegate().add(reader, baseURI, dataFormat, contexts);
155: } else {
156: super .add(reader, baseURI, dataFormat, contexts);
157: }
158: }
159:
160: @Override
161: public void add(Resource subject, URI predicate, Value object,
162: Resource... contexts) throws RepositoryException {
163: if (isDelegatingAdd()) {
164: getDelegate().add(subject, predicate, object, contexts);
165: } else {
166: super .add(subject, predicate, object, contexts);
167: }
168: }
169:
170: @Override
171: public void add(Statement st, Resource... contexts)
172: throws RepositoryException {
173: if (isDelegatingAdd()) {
174: getDelegate().add(st, contexts);
175: } else {
176: super .add(st, contexts);
177: }
178: }
179:
180: @Override
181: public void add(URL url, String baseURI, RDFFormat dataFormat,
182: Resource... contexts) throws IOException,
183: RDFParseException, RepositoryException {
184: if (isDelegatingAdd()) {
185: getDelegate().add(url, baseURI, dataFormat, contexts);
186: } else {
187: super .add(url, baseURI, dataFormat, contexts);
188: }
189: }
190:
191: @Override
192: public void clear(Resource... contexts) throws RepositoryException {
193: if (isDelegatingRemove()) {
194: getDelegate().clear(contexts);
195: } else {
196: super .clear(contexts);
197: }
198: }
199:
200: @Override
201: public void close() throws RepositoryException {
202: getDelegate().close();
203: super .close();
204: }
205:
206: public void commit() throws RepositoryException {
207: getDelegate().commit();
208: }
209:
210: public void exportStatements(Resource subj, URI pred, Value obj,
211: boolean includeInferred, RDFHandler handler,
212: Resource... contexts) throws RepositoryException,
213: RDFHandlerException {
214: if (isDelegatingRead()) {
215: getDelegate().exportStatements(subj, pred, obj,
216: includeInferred, handler, contexts);
217: } else {
218: exportStatements(getStatements(subj, pred, obj,
219: includeInferred, contexts), handler);
220: }
221: }
222:
223: public RepositoryResult<Resource> getContextIDs()
224: throws RepositoryException {
225: return getDelegate().getContextIDs();
226: }
227:
228: public String getNamespace(String prefix)
229: throws RepositoryException {
230: return getDelegate().getNamespace(prefix);
231: }
232:
233: public RepositoryResult<Namespace> getNamespaces()
234: throws RepositoryException {
235: return getDelegate().getNamespaces();
236: }
237:
238: public RepositoryResult<Statement> getStatements(Resource subj,
239: URI pred, Value obj, boolean includeInferred,
240: Resource... contexts) throws RepositoryException {
241: return getDelegate().getStatements(subj, pred, obj,
242: includeInferred, contexts);
243: }
244:
245: @Override
246: public boolean hasStatement(Resource subj, URI pred, Value obj,
247: boolean includeInferred, Resource... contexts)
248: throws RepositoryException {
249: if (isDelegatingRead()) {
250: return getDelegate().hasStatement(subj, pred, obj,
251: includeInferred, contexts);
252: }
253: return super .hasStatement(subj, pred, obj, includeInferred,
254: contexts);
255: }
256:
257: @Override
258: public boolean hasStatement(Statement st, boolean includeInferred,
259: Resource... contexts) throws RepositoryException {
260: if (isDelegatingRead()) {
261: return getDelegate().hasStatement(st, includeInferred,
262: contexts);
263: }
264: return super .hasStatement(st, includeInferred, contexts);
265: }
266:
267: @Override
268: public boolean isAutoCommit() throws RepositoryException {
269: return getDelegate().isAutoCommit();
270: }
271:
272: @Override
273: public boolean isEmpty() throws RepositoryException {
274: if (isDelegatingRead()) {
275: return getDelegate().isEmpty();
276: }
277: return super .isEmpty();
278: }
279:
280: @Override
281: public boolean isOpen() throws RepositoryException {
282: return getDelegate().isOpen();
283: }
284:
285: public GraphQuery prepareGraphQuery(QueryLanguage ql, String query,
286: String baseURI) throws MalformedQueryException,
287: RepositoryException {
288: return getDelegate().prepareGraphQuery(ql, query, baseURI);
289: }
290:
291: public Query prepareQuery(QueryLanguage ql, String query,
292: String baseURI) throws MalformedQueryException,
293: RepositoryException {
294: return getDelegate().prepareQuery(ql, query, baseURI);
295: }
296:
297: public TupleQuery prepareTupleQuery(QueryLanguage ql, String query,
298: String baseURI) throws MalformedQueryException,
299: RepositoryException {
300: return getDelegate().prepareTupleQuery(ql, query, baseURI);
301: }
302:
303: public BooleanQuery prepareBooleanQuery(QueryLanguage ql,
304: String query, String baseURI)
305: throws MalformedQueryException, RepositoryException {
306: return getDelegate().prepareBooleanQuery(ql, query, baseURI);
307: }
308:
309: @Override
310: public void remove(Iterable<? extends Statement> statements,
311: Resource... contexts) throws RepositoryException {
312: if (isDelegatingRemove()) {
313: getDelegate().remove(statements, contexts);
314: } else {
315: super .remove(statements, contexts);
316: }
317: }
318:
319: @Override
320: public <E extends Exception> void remove(
321: Iteration<? extends Statement, E> statementIter,
322: Resource... contexts) throws RepositoryException, E {
323: if (isDelegatingRemove()) {
324: getDelegate().remove(statementIter, contexts);
325: } else {
326: super .remove(statementIter, contexts);
327: }
328: }
329:
330: @Override
331: public void remove(Resource subject, URI predicate, Value object,
332: Resource... contexts) throws RepositoryException {
333: if (isDelegatingRemove()) {
334: getDelegate().remove(subject, predicate, object, contexts);
335: } else {
336: super .remove(subject, predicate, object, contexts);
337: }
338: }
339:
340: @Override
341: public void remove(Statement st, Resource... contexts)
342: throws RepositoryException {
343: if (isDelegatingRemove()) {
344: getDelegate().remove(st, contexts);
345: } else {
346: super .remove(st, contexts);
347: }
348: }
349:
350: public void removeNamespace(String prefix)
351: throws RepositoryException {
352: getDelegate().removeNamespace(prefix);
353: }
354:
355: public void clearNamespaces() throws RepositoryException {
356: getDelegate().clearNamespaces();
357: }
358:
359: public void rollback() throws RepositoryException {
360: getDelegate().rollback();
361: }
362:
363: @Override
364: public void setAutoCommit(boolean autoCommit)
365: throws RepositoryException {
366: super .setAutoCommit(autoCommit);
367: getDelegate().setAutoCommit(autoCommit);
368: }
369:
370: public void setNamespace(String prefix, String name)
371: throws RepositoryException {
372: getDelegate().setNamespace(prefix, name);
373: }
374:
375: public long size(Resource... contexts) throws RepositoryException {
376: return getDelegate().size(contexts);
377: }
378:
379: @Override
380: protected void addWithoutCommit(Resource subject, URI predicate,
381: Value object, Resource... contexts)
382: throws RepositoryException {
383: super .add(subject, predicate, object, contexts);
384: }
385:
386: @Override
387: protected void removeWithoutCommit(Resource subject, URI predicate,
388: Value object, Resource... contexts)
389: throws RepositoryException {
390: super .remove(subject, predicate, object, contexts);
391: }
392:
393: /**
394: * Exports all statements contained in the supplied statement iterator and
395: * all relevant namespace information to the supplied RDFHandler.
396: */
397: protected void exportStatements(RepositoryResult<Statement> stIter,
398: RDFHandler handler) throws RepositoryException,
399: RDFHandlerException {
400: try {
401: handler.startRDF();
402: // Export namespace information
403: RepositoryResult<Namespace> nsIter = getNamespaces();
404: try {
405: while (nsIter.hasNext()) {
406: Namespace ns = nsIter.next();
407: handler.handleNamespace(ns.getPrefix(), ns
408: .getName());
409: }
410: } finally {
411: nsIter.close();
412: }
413: // Export statemnts
414: while (stIter.hasNext()) {
415: handler.handleStatement(stIter.next());
416: }
417: handler.endRDF();
418: } finally {
419: stIter.close();
420: }
421: }
422: }
|