001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio.helpers;
007:
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.LinkedHashMap;
011: import java.util.Map;
012:
013: import org.openrdf.model.Statement;
014: import org.openrdf.rio.RDFHandlerException;
015:
016: /**
017: * A RDFHandler that can be used to collect reported statements in collections.
018: *
019: * @author Arjohn Kampman
020: */
021: public class StatementCollector extends RDFHandlerBase {
022:
023: /*-----------*
024: * Variables *
025: *-----------*/
026:
027: private Collection<Statement> statements;
028:
029: private Map<String, String> namespaces;
030:
031: /*--------------*
032: * Constructors *
033: *--------------*/
034:
035: /**
036: * Creates a new StatementCollector that uses a new ArrayList to store the
037: * reported statements and a new LinkedHashMap to store the reported
038: * namespaces.
039: */
040: public StatementCollector() {
041: this (new ArrayList<Statement>());
042: }
043:
044: /**
045: * Creates a new StatementCollector that stores reported statements in the
046: * supplied collection and that uses a new LinkedHashMap to store the
047: * reported namespaces.
048: */
049: public StatementCollector(Collection<Statement> statements) {
050: this (statements, new LinkedHashMap<String, String>());
051: }
052:
053: /**
054: * Creates a new StatementCollector that stores reported statements and
055: * namespaces in the supplied containers.
056: */
057: public StatementCollector(Collection<Statement> statements,
058: Map<String, String> namespaces) {
059: this .statements = statements;
060: this .namespaces = namespaces;
061: }
062:
063: /*---------*
064: * Methods *
065: *---------*/
066:
067: /**
068: * Clear the set of collected statements.
069: */
070: public void clear() {
071: statements.clear();
072: }
073:
074: /**
075: * Gets the collection that contains the collected statements.
076: */
077: public Collection<Statement> getStatements() {
078: return statements;
079: }
080:
081: /**
082: * Gets the map that contains the collected namespaces.
083: */
084: public Map<String, String> getNamespaces() {
085: return namespaces;
086: }
087:
088: @Override
089: public void handleNamespace(String prefix, String uri)
090: throws RDFHandlerException {
091: if (!namespaces.containsKey(prefix)) {
092: namespaces.put(prefix, uri);
093: }
094: }
095:
096: @Override
097: public void handleStatement(Statement st) {
098: statements.add(st);
099: }
100: }
|