001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id: SingleDocumentReader.java 30256 2008-02-18 21:52:11Z tdelprat $
013 */
014
015package org.nuxeo.ecm.core.io.impl.plugins;
016
017import java.io.IOException;
018
019import org.nuxeo.ecm.core.api.CoreSession;
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.DocumentRef;
022import org.nuxeo.ecm.core.io.ExportedDocument;
023import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
024
025/**
026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
027 */
028public class SingleDocumentReader extends DocumentModelReader {
029
030    protected DocumentModel doc;
031
032    protected boolean enableRepeatedReads = false;
033
034    protected boolean readDone = false;
035
036    public SingleDocumentReader(CoreSession session, DocumentModel root) {
037        super(session);
038        doc = root;
039    }
040
041    public SingleDocumentReader(CoreSession session, DocumentRef root) {
042        this(session, session.getDocument(root));
043    }
044
045    @Override
046    public void close() {
047        super.close();
048        session = null;
049        doc = null;
050    }
051
052    @Override
053    public ExportedDocument read() throws IOException {
054        if (doc != null) {
055            if (readDone && !enableRepeatedReads) {
056                return null;
057            } else {
058                readDone = true;
059                return new ExportedDocumentImpl(doc);
060            }
061        }
062        doc = null;
063        return null;
064    }
065
066    public void setEnableRepeatedReads(boolean enableRepeatedReads) {
067        this.enableRepeatedReads = enableRepeatedReads;
068    }
069
070}