001: package org.ontoware.rdf2go.model.impl;
002:
003: import java.util.HashMap;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import org.ontoware.rdf2go.exception.ModelRuntimeException;
008: import org.ontoware.rdf2go.model.DiffReader;
009: import org.ontoware.rdf2go.model.Model;
010: import org.ontoware.rdf2go.model.ModelChangedListener;
011: import org.ontoware.rdf2go.model.NotifyingModel;
012: import org.ontoware.rdf2go.model.Statement;
013: import org.ontoware.rdf2go.model.TriplePattern;
014: import org.ontoware.rdf2go.model.node.Node;
015: import org.ontoware.rdf2go.model.node.Resource;
016: import org.ontoware.rdf2go.model.node.URI;
017:
018: /**
019: * Adds notifying capabilites to existing models.
020: * @author voelkel
021: *
022: */
023: public class NotifyingModelLayer extends DelegatingModel implements
024: NotifyingModel {
025:
026: public NotifyingModelLayer(Model model) {
027: super (model);
028: }
029:
030: // /////////////////////////////////
031: // override methods to be able to notify
032:
033: /*
034: * (non-Javadoc)
035: *
036: * @see org.ontoware.rdf2go.core.common.CommonModelWriter#addAll(org.ontoware.rdf2go.core.common.CommonModelReader)
037: */
038: @Override
039: public void addAll(Iterator<? extends Statement> other)
040: throws ModelRuntimeException {
041: while (other.hasNext()) {
042: this .addStatement(other.next());
043: }
044: }
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(org.ontoware.rdf2go.core.node.Resource,
050: * org.ontoware.rdf2go.core.node.URI,
051: * org.ontoware.rdf2go.core.node.Node)
052: */
053: @Override
054: public void addStatement(Resource subject, URI predicate,
055: Node object) throws ModelRuntimeException {
056: this .addStatement(super .getDelegatedModel().createStatement(
057: subject, predicate, object));
058: }
059:
060: /*
061: * (non-Javadoc)
062: *
063: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(org.ontoware.rdf2go.core.node.Resource,
064: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
065: * java.lang.String)
066: */
067: @Override
068: public void addStatement(Resource subject, URI predicate,
069: String literal, String languageTag)
070: throws ModelRuntimeException {
071: this .addStatement(getDelegatedModel().createStatement(
072: subject,
073: predicate,
074: getDelegatedModel().createLanguageTagLiteral(literal,
075: languageTag)));
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(org.ontoware.rdf2go.core.node.Resource,
082: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
083: * org.ontoware.rdf2go.core.node.URI)
084: */
085: @Override
086: public void addStatement(Resource subject, URI predicate,
087: String literal, URI datatypeURI)
088: throws ModelRuntimeException {
089: this .addStatement(subject, predicate, getDelegatedModel()
090: .createDatatypeLiteral(literal, datatypeURI));
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(org.ontoware.rdf2go.core.node.Resource,
097: * org.ontoware.rdf2go.core.node.URI, java.lang.String)
098: */
099: @Override
100: public void addStatement(Resource subject, URI predicate,
101: String literal) throws ModelRuntimeException {
102: this .addStatement(getDelegatedModel().createStatement(subject,
103: predicate,
104: getDelegatedModel().createPlainLiteral(literal)));
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see org.ontoware.rdf2go.core.common.CommonModelWriter#addStatement(S)
111: */
112: @Override
113: public void addStatement(Statement statement)
114: throws ModelRuntimeException {
115: for (ModelChangedListener listener : this .modelChangeListener
116: .keySet()) {
117: TriplePattern pattern = this .modelChangeListener
118: .get(listener);
119: if (pattern == null || pattern.matches(statement)) {
120: listener.addedStatement(statement);
121: }
122: }
123: getDelegatedModel().addStatement(statement);
124: }
125:
126: /*
127: * (non-Javadoc)
128: *
129: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(java.lang.String,
130: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
131: * java.lang.String)
132: */
133: @Override
134: public void addStatement(String subjectURIString, URI predicate,
135: String literal, String languageTag)
136: throws ModelRuntimeException {
137: this .addStatement(getDelegatedModel().createStatement(
138: getDelegatedModel().createURI(subjectURIString),
139: predicate,
140: getDelegatedModel().createLanguageTagLiteral(literal,
141: languageTag)));
142: }
143:
144: /*
145: * (non-Javadoc)
146: *
147: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(java.lang.String,
148: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
149: * org.ontoware.rdf2go.core.node.URI)
150: */
151: @Override
152: public void addStatement(String subjectURIString, URI predicate,
153: String literal, URI datatypeURI)
154: throws ModelRuntimeException {
155: this .addStatement(getDelegatedModel().createStatement(
156: getDelegatedModel().createURI(subjectURIString),
157: predicate,
158: getDelegatedModel().createDatatypeLiteral(literal,
159: datatypeURI)));
160: }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see org.ontoware.rdf2go.core.triple.ModelWriter#addStatement(java.lang.String,
166: * org.ontoware.rdf2go.core.node.URI, java.lang.String)
167: */
168: @Override
169: public void addStatement(String subjectURIString, URI predicate,
170: String literal) throws ModelRuntimeException {
171: this .addStatement(getDelegatedModel().createStatement(
172: getDelegatedModel().createURI(subjectURIString),
173: predicate,
174: getDelegatedModel().createPlainLiteral(literal)));
175: }
176:
177: /////////////// remove
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see org.ontoware.rdf2go.core.common.CommonModelWriter#removeAll(org.ontoware.rdf2go.core.common.CommonModelReader)
183: */
184: @Override
185: public void removeAll(Iterator<? extends Statement> other)
186: throws ModelRuntimeException {
187: while (other.hasNext()) {
188: this .removeStatement(other.next());
189: }
190: }
191:
192: /*
193: * (non-Javadoc)
194: *
195: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(org.ontoware.rdf2go.core.node.Resource,
196: * org.ontoware.rdf2go.core.node.URI,
197: * org.ontoware.rdf2go.core.node.Node)
198: */
199: @Override
200: public void removeStatement(Resource subject, URI predicate,
201: Node object) throws ModelRuntimeException {
202: this .removeStatement(getDelegatedModel().createStatement(
203: subject, predicate, object));
204: }
205:
206: /*
207: * (non-Javadoc)
208: *
209: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(org.ontoware.rdf2go.core.node.Resource,
210: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
211: * java.lang.String)
212: */
213: @Override
214: public void removeStatement(Resource subject, URI predicate,
215: String literal, String languageTag)
216: throws ModelRuntimeException {
217: this .removeStatement(getDelegatedModel().createStatement(
218: subject,
219: predicate,
220: getDelegatedModel().createLanguageTagLiteral(literal,
221: languageTag)));
222: }
223:
224: /*
225: * (non-Javadoc)
226: *
227: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(org.ontoware.rdf2go.core.node.Resource,
228: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
229: * org.ontoware.rdf2go.core.node.URI)
230: */
231: @Override
232: public void removeStatement(Resource subject, URI predicate,
233: String literal, URI datatypeURI)
234: throws ModelRuntimeException {
235: this .removeStatement(subject, predicate, getDelegatedModel()
236: .createDatatypeLiteral(literal, datatypeURI));
237: }
238:
239: /*
240: * (non-Javadoc)
241: *
242: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(org.ontoware.rdf2go.core.node.Resource,
243: * org.ontoware.rdf2go.core.node.URI, java.lang.String)
244: */
245: @Override
246: public void removeStatement(Resource subject, URI predicate,
247: String literal) throws ModelRuntimeException {
248: this .removeStatement(getDelegatedModel().createStatement(
249: subject, predicate,
250: getDelegatedModel().createPlainLiteral(literal)));
251: }
252:
253: /*
254: * (non-Javadoc)
255: *
256: * @see org.ontoware.rdf2go.core.common.CommonModelWriter#removeStatement(S)
257: */
258: @Override
259: public void removeStatement(Statement statement)
260: throws ModelRuntimeException {
261: for (ModelChangedListener listener : this .modelChangeListener
262: .keySet()) {
263: TriplePattern pattern = this .modelChangeListener
264: .get(listener);
265: if (pattern == null || pattern.matches(statement)) {
266: listener.removedStatement(statement);
267: }
268: }
269: getDelegatedModel().removeStatement(statement);
270: }
271:
272: /*
273: * (non-Javadoc)
274: *
275: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(java.lang.String,
276: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
277: * java.lang.String)
278: */
279: @Override
280: public void removeStatement(String subjectURIString, URI predicate,
281: String literal, String languageTag)
282: throws ModelRuntimeException {
283: this .removeStatement(getDelegatedModel().createStatement(
284: getDelegatedModel().createURI(subjectURIString),
285: predicate,
286: getDelegatedModel().createLanguageTagLiteral(literal,
287: languageTag)));
288: }
289:
290: /*
291: * (non-Javadoc)
292: *
293: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(java.lang.String,
294: * org.ontoware.rdf2go.core.node.URI, java.lang.String,
295: * org.ontoware.rdf2go.core.node.URI)
296: */
297: @Override
298: public void removeStatement(String subjectURIString, URI predicate,
299: String literal, URI datatypeURI)
300: throws ModelRuntimeException {
301: this .removeStatement(getDelegatedModel().createStatement(
302: getDelegatedModel().createURI(subjectURIString),
303: predicate,
304: getDelegatedModel().createDatatypeLiteral(literal,
305: datatypeURI)));
306: }
307:
308: /*
309: * (non-Javadoc)
310: *
311: * @see org.ontoware.rdf2go.core.triple.ModelWriter#removeStatement(java.lang.String,
312: * org.ontoware.rdf2go.core.node.URI, java.lang.String)
313: */
314: @Override
315: public void removeStatement(String subjectURIString, URI predicate,
316: String literal) throws ModelRuntimeException {
317: this .removeStatement(getDelegatedModel().createStatement(
318: getDelegatedModel().createURI(subjectURIString),
319: predicate,
320: getDelegatedModel().createPlainLiteral(literal)));
321: }
322:
323: //////////// diff
324:
325: @Override
326: public void update(DiffReader diff) throws ModelRuntimeException {
327: for (ModelChangedListener listener : this .modelChangeListener
328: .keySet()) {
329: listener.performedUpdate(diff);
330: }
331: getDelegatedModel().update(diff);
332: }
333:
334: // //////////////////////////////////
335: // implement NotifyingModel
336:
337: private Map<ModelChangedListener, TriplePattern> modelChangeListener = new HashMap<ModelChangedListener, TriplePattern>();
338:
339: public void addModelChangedListener(ModelChangedListener listener,
340: TriplePattern pattern) {
341: this .modelChangeListener.put(listener, pattern);
342: }
343:
344: public void addModelChangedListener(ModelChangedListener listener) {
345: this .modelChangeListener.put(listener, null);
346: }
347:
348: public void removeModelChangedListener(ModelChangedListener listener) {
349: this.modelChangeListener.remove(listener);
350: }
351:
352: }
|