001/*
002 * (C) Copyright 2006-2009 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 * Contributors:
015 *     troger
016 */
017package org.nuxeo.ecm.platform.preview.adapter.base;
018
019import java.io.IOException;
020import java.nio.charset.UnsupportedCharsetException;
021import java.util.List;
022
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.Blobs;
025import org.nuxeo.ecm.platform.preview.api.PreviewException;
026
027/**
028 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
029 */
030public class NoteHtmlPreviewAdapter extends PreprocessedHtmlPreviewAdapter {
031
032    public NoteHtmlPreviewAdapter(List<String> fieldsPaths) {
033        super(fieldsPaths);
034    }
035
036    @Override
037    public List<Blob> getPreviewBlobs() throws PreviewException {
038        List<Blob> blobs = super.getPreviewBlobs();
039        if (!blobs.isEmpty()) {
040            Blob blob = blobs.remove(0);
041            Blob newBlob = processNoteBlob(blob);
042            blobs.add(0, newBlob);
043        }
044        return blobs;
045    }
046
047    protected Blob processNoteBlob(Blob blob) throws PreviewException {
048        try {
049            String note = blob.getString();
050            StringBuilder sb = new StringBuilder();
051            sb.append("<html><body>");
052            sb.append(note);
053            sb.append("</body></html>");
054
055            String mimeType = blob.getMimeType();
056            if (mimeType == null) {
057                mimeType = "text/html";
058            }
059            return Blobs.createBlob(sb.toString(), mimeType, blob.getEncoding());
060        } catch (IOException e) {
061            throw new PreviewException(e);
062        } catch (UnsupportedCharsetException e) {
063            throw new PreviewException(e);
064        }
065    }
066
067    @Override
068    public List<Blob> getPreviewBlobs(String xpath) throws PreviewException {
069        return getPreviewBlobs();
070    }
071
072}