/* * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Contributors: * Nuxeo - initial API and implementation * * $Id: DocumentTreeReader.java 29029 2008-01-14 18:38:14Z ldoguin $ */ package org.nuxeo.ecm.core.io.impl.plugins; import java.io.IOException; import org.nuxeo.ecm.core.api.CoreSession; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentRef; import org.nuxeo.ecm.core.api.DocumentTreeIterator; import org.nuxeo.ecm.core.io.ExportedDocument; import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl; /** * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> */ public class DocumentTreeReader extends DocumentModelReader { protected DocumentTreeIterator iterator; protected int pathSegmentsToRemove = 0; public DocumentTreeReader(CoreSession session, DocumentModel root, boolean excludeRoot) { super(session); iterator = new DocumentTreeIterator(session, root, excludeRoot); pathSegmentsToRemove = root.getPath().segmentCount() - (excludeRoot ? 0 : 1); } public DocumentTreeReader(CoreSession session, DocumentRef root) { this(session, session.getDocument(root)); } public DocumentTreeReader(CoreSession session, DocumentModel root) { this(session, root, false); } @Override public void close() { super.close(); iterator.reset(); iterator = null; } @Override public ExportedDocument read() throws IOException { if (iterator.hasNext()) { DocumentModel docModel = iterator.next(); if (pathSegmentsToRemove > 0) { // remove unwanted leading segments return new ExportedDocumentImpl(docModel, docModel.getPath().removeFirstSegments(pathSegmentsToRemove), inlineBlobs); } else { return new ExportedDocumentImpl(docModel, inlineBlobs); } } return null; } }