001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: SingleDocumentReader.java 30256 2008-02-18 21:52:11Z tdelprat $
020 */
021
022package org.nuxeo.ecm.core.io.impl.plugins;
023
024import java.io.IOException;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.io.ExportedDocument;
030import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class SingleDocumentReader extends DocumentModelReader {
036
037    protected DocumentModel doc;
038
039    protected boolean enableRepeatedReads = false;
040
041    protected boolean readDone = false;
042
043    public SingleDocumentReader(CoreSession session, DocumentModel root) {
044        super(session);
045        doc = root;
046    }
047
048    public SingleDocumentReader(CoreSession session, DocumentRef root) {
049        this(session, session.getDocument(root));
050    }
051
052    @Override
053    public void close() {
054        super.close();
055        session = null;
056        doc = null;
057    }
058
059    @Override
060    public ExportedDocument read() throws IOException {
061        if (doc != null) {
062            if (readDone && !enableRepeatedReads) {
063                return null;
064            } else {
065                readDone = true;
066                return new ExportedDocumentImpl(doc);
067            }
068        }
069        doc = null;
070        return null;
071    }
072
073    public void setEnableRepeatedReads(boolean enableRepeatedReads) {
074        this.enableRepeatedReads = enableRepeatedReads;
075    }
076
077}