001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 */
015package org.nuxeo.ecm.platform.preview.adapter.base;
016
017import java.io.IOException;
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.Blobs;
025import org.nuxeo.ecm.core.api.PropertyException;
026import org.nuxeo.ecm.core.api.model.Property;
027import org.nuxeo.ecm.platform.preview.api.PreviewException;
028
029/**
030 * Base class for preview adapters that will use preprocessed HTML preview that is stored inside the document.
031 *
032 * @author tiry
033 */
034public class PreprocessedHtmlPreviewAdapter extends AbstractHtmlPreviewAdapter {
035
036    private static final Log log = LogFactory.getLog(PreprocessedHtmlPreviewAdapter.class);
037
038    protected List<String> storedPreviewFieldsPaths = new ArrayList<String>();
039
040    public PreprocessedHtmlPreviewAdapter(List<String> fieldsPaths) {
041        storedPreviewFieldsPaths = fieldsPaths;
042    }
043
044    @Override
045    public List<Blob> getPreviewBlobs() throws PreviewException {
046
047        List<Blob> resultBlobs = new ArrayList<Blob>();
048
049        for (String xpath : storedPreviewFieldsPaths) {
050            try {
051                Property prop = adaptedDoc.getProperty(xpath);
052                if (prop.isComplex()) {
053                    Blob blob = (Blob) prop.getValue();
054                    try {
055                        blob.getStream().reset();
056                    } catch (IOException e) {
057                        log.error(e);
058                    }
059                    resultBlobs.add(blob);
060                } else {
061                    String data = (String) prop.getValue();
062                    resultBlobs.add(Blobs.createBlob(data));
063                }
064            } catch (PropertyException e) {
065                throw new PreviewException("Unable to get property " + xpath, e);
066            }
067        }
068        return resultBlobs;
069    }
070
071    @Override
072    public List<Blob> getPreviewBlobs(String xpath) throws PreviewException {
073        return getPreviewBlobs();
074    }
075
076    @Override
077    public void cleanup() {
078        // nothing to do
079    }
080
081    @Override
082    public boolean cachable() {
083        return false;
084    }
085
086    @Override
087    public boolean hasBlobToPreview() throws PreviewException {
088        return getPreviewBlobs().size() > 0;
089    }
090
091}