001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.repository.util;
007:
008: import org.openrdf.model.Resource;
009: import org.openrdf.model.Statement;
010: import org.openrdf.repository.RepositoryConnection;
011: import org.openrdf.repository.RepositoryException;
012: import org.openrdf.rio.RDFHandlerException;
013: import org.openrdf.rio.helpers.RDFHandlerBase;
014:
015: /**
016: * An RDFHandler that removes RDF data from a repository.
017: */
018: public class RDFRemover extends RDFHandlerBase {
019:
020: /*-----------*
021: * Constants *
022: *-----------*/
023:
024: /**
025: * The connection to use for the removal operations.
026: */
027: private final RepositoryConnection con;
028:
029: /*-----------*
030: * Variables *
031: *-----------*/
032:
033: /**
034: * Flag indicating whether the context specified for this RDFRemover should
035: * be enforced upon all reported statements.
036: */
037: private boolean enforceContext;
038:
039: /**
040: * The context to remove the statements from; <tt>null</tt> to indicate the
041: * null context. This context value is used when enforceContext is set to
042: * true.
043: */
044: private Resource context;
045:
046: /*--------------*
047: * Constructors *
048: *--------------*/
049:
050: /**
051: * Creates a new RDFRemover object that removes the data from the default
052: * context.
053: *
054: * @param con
055: * The connection to use for the removal operations.
056: */
057: public RDFRemover(RepositoryConnection con) {
058: this .con = con;
059: this .enforceContext = false;
060: }
061:
062: /*---------*
063: * Methods *
064: *---------*/
065:
066: /**
067: * Enforces the supplied context upon all statements that are reported to
068: * this RDFRemover.
069: *
070: * @param context
071: * A Resource identifying the context, or <tt>null</tt> for the null
072: * context.
073: */
074: public void enforceContext(Resource context) {
075: this .context = context;
076: enforceContext = true;
077: }
078:
079: /**
080: * Checks whether this RDFRemover enforces its context upon all statements
081: * that are reported to it.
082: *
083: * @return <tt>true</tt> if it enforces its context, <tt>false</tt>
084: * otherwise.
085: */
086: public boolean enforcesContext() {
087: return enforceContext;
088: }
089:
090: /**
091: * Gets the context identifier that this RDFRemover enforces upon all
092: * statements that are reported to it (in case <tt>enforcesContext()</tt>
093: * returns <tt>true</tt>).
094: *
095: * @return A Resource identifying the context, or <tt>null</tt> if the null
096: * context is enforced.
097: */
098: public Resource getContext() {
099: return context;
100: }
101:
102: @Override
103: public void handleStatement(Statement st)
104: throws RDFHandlerException {
105: try {
106: if (enforceContext) {
107: // Override supplied context info
108: con.remove(st.getSubject(), st.getPredicate(), st
109: .getObject(), context);
110: } else {
111: con.remove(st.getSubject(), st.getPredicate(), st
112: .getObject(), st.getContext());
113: }
114: } catch (RepositoryException e) {
115: throw new RDFHandlerException(e);
116: }
117: }
118: }
|