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.File;
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.nuxeo.common.utils.FileUtils;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.preview.adapter.BlobPostProcessor;
028import org.nuxeo.ecm.platform.preview.adapter.PreviewAdapterManager;
029import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter;
030import org.nuxeo.ecm.platform.preview.api.PreviewException;
031import org.nuxeo.ecm.platform.preview.helper.PreviewHelper;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Abstract base class for PreviewAdapters
036 *
037 * @author tiry
038 */
039public abstract class AbstractHtmlPreviewAdapter implements HtmlPreviewAdapter {
040
041    // private static final String TITLE_REGEXP = "<title\b[^>]*>(.*?)</title>";
042    private static final String TITLE_REGEXP = "<title>(.*?)</title>";
043
044    // private static final String TITLE_REGEXP = "<title[^>]*>[^<]*</title>";
045    private static final Pattern TITLE_PATTERN = Pattern.compile(TITLE_REGEXP, Pattern.CASE_INSENSITIVE);
046
047    protected DocumentModel adaptedDoc;
048
049    protected static PreviewAdapterManager previewManager;
050
051    protected PreviewAdapterManager getPreviewManager() {
052        if (previewManager == null) {
053            previewManager = Framework.getService(PreviewAdapterManager.class);
054        }
055        return previewManager;
056    }
057
058    @Override
059    public void setAdaptedDocument(DocumentModel doc) {
060        adaptedDoc = doc;
061    }
062
063    @Override
064    public String getFilePreviewURL() {
065        return PreviewHelper.getPreviewURL(adaptedDoc);
066    }
067
068    @Override
069    public String getFilePreviewURL(String xpath) {
070        return PreviewHelper.getPreviewURL(adaptedDoc, xpath);
071    }
072
073    protected String updateTitleInHtml(String htmlContent) {
074        Matcher m = TITLE_PATTERN.matcher(htmlContent);
075        // if (m.matches())
076        // return m.replaceFirst("<title>" + getPreviewTitle() + "</title>");
077        if (m.find()) {
078            String found = m.group();
079            htmlContent = htmlContent.replaceFirst(found, "<title>" + getPreviewTitle() + "</title>");
080        }
081
082        return htmlContent;
083    }
084
085    protected void updateTitleInHtml(File file) throws IOException {
086        String htmlContent = FileUtils.readFile(file);
087        htmlContent = updateTitleInHtml(htmlContent);
088        FileUtils.writeFile(file, htmlContent);
089    }
090
091    protected String getPreviewTitle() {
092        StringBuilder sb = new StringBuilder();
093
094        sb.append(adaptedDoc.getTitle());
095        sb.append(" ");
096        String vl = adaptedDoc.getVersionLabel();
097        if (vl != null) {
098            sb.append(vl);
099        }
100        sb.append(" (preview)");
101
102        return sb.toString();
103    }
104
105    @Override
106    public List<Blob> getFilePreviewBlobs() throws PreviewException {
107        return getFilePreviewBlobs(false);
108    }
109
110    @Override
111    public List<Blob> getFilePreviewBlobs(String xpath) throws PreviewException {
112        return getFilePreviewBlobs(xpath, false);
113    }
114
115    @Override
116    public List<Blob> getFilePreviewBlobs(boolean postProcess) throws PreviewException {
117        List<Blob> blobs = getPreviewBlobs();
118        if (postProcess) {
119            blobs = postProcessBlobs(blobs);
120        }
121        return blobs;
122    }
123
124    protected abstract List<Blob> getPreviewBlobs() throws PreviewException;
125
126    @Override
127    public List<Blob> getFilePreviewBlobs(String xpath, boolean postProcess) throws PreviewException {
128        List<Blob> blobs = getPreviewBlobs(xpath);
129        if (postProcess) {
130            blobs = postProcessBlobs(blobs);
131        }
132        return blobs;
133    }
134
135    protected abstract List<Blob> getPreviewBlobs(String xpath) throws PreviewException;
136
137    protected List<Blob> postProcessBlobs(List<Blob> blobs) throws PreviewException {
138        List<Blob> processedBlobs = new ArrayList<Blob>();
139        for (Blob blob : blobs) {
140            for (BlobPostProcessor processor : getPreviewManager().getBlobPostProcessors()) {
141                blob = processor.process(blob);
142            }
143            processedBlobs.add(blob);
144        }
145        return processedBlobs;
146    }
147
148}