001/**
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 *
006 */
007package org.fcrepo.migration.foxml;
008
009import java.io.File;
010import java.io.FileFilter;
011import java.util.Iterator;
012import java.util.regex.Pattern;
013
014import org.apache.commons.io.filefilter.RegexFileFilter;
015import org.fcrepo.migration.FedoraObjectProcessor;
016import org.fcrepo.migration.ObjectSource;
017
018/**
019 * An ObjectSource implementation that exposes FOXML from a provided directory.
020 * @author mdurbin
021 */
022
023public class NativeFoxmlDirectoryObjectSource implements ObjectSource {
024
025    private InternalIDResolver resolver;
026
027    private URLFetcher fetcher;
028
029    private File root;
030
031    private String localFedoraServer;
032
033    /**
034     * Defaults to match any filename that doesn't begin with a "." character.
035     */
036    private FileFilter fileFilter = new RegexFileFilter(Pattern.compile("^[^\\.].*$"));
037
038    /**
039     * A constructor for use with the data storage directories that underly a
040     * fedora 3.x repository.  First, this constructor will build an index of
041     * all of the datastreams in the provided datastreamStore directory for use
042     * resolving internal id references within the foxml.
043     * @param objectStore a directory containing just directories and FOXML files
044     * @param resolver an InternalIDResolver implementation that can resolve
045     *                 references to internally managed datastreams.
046     * @param localFedoraServer the domain and port for the server that hosted the fedora objects in the format
047     *                          "localhost:8080".
048     */
049    public NativeFoxmlDirectoryObjectSource(final File objectStore,
050            final InternalIDResolver resolver, final String localFedoraServer) {
051        this.root = objectStore;
052        this.resolver = resolver;
053        this.fetcher = new HttpClientURLFetcher();
054        this.localFedoraServer = localFedoraServer;
055    }
056
057    /**
058     * set the fetcher.
059     * @param fetcher the fetcher
060     */
061    public void setFetcher(final URLFetcher fetcher) {
062        this.fetcher = fetcher;
063    }
064
065    /**
066     * Sets a FileFilter to determine which files will be considered as object
067     * files in the source directories.
068     * @param fileFilter a FileFilter implementation
069     */
070    public void setFileFilter(final FileFilter fileFilter) {
071        this.fileFilter = fileFilter;
072    }
073
074    @Override
075    public Iterator<FedoraObjectProcessor> iterator() {
076        return new FoxmlDirectoryDFSIterator(root, resolver, fetcher, localFedoraServer, fileFilter);
077    }
078
079}