001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     troger
018 */
019package org.nuxeo.ecm.platform.preview.adapter.base;
020
021import java.io.IOException;
022import java.nio.charset.UnsupportedCharsetException;
023import java.util.List;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.Blobs;
027import org.nuxeo.ecm.platform.preview.api.PreviewException;
028
029/**
030 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
031 */
032public class NoteHtmlPreviewAdapter extends PreprocessedHtmlPreviewAdapter {
033
034    public NoteHtmlPreviewAdapter(List<String> fieldsPaths) {
035        super(fieldsPaths);
036    }
037
038    @Override
039    public List<Blob> getPreviewBlobs() throws PreviewException {
040        List<Blob> blobs = super.getPreviewBlobs();
041        if (!blobs.isEmpty()) {
042            Blob blob = blobs.remove(0);
043            Blob newBlob = processNoteBlob(blob);
044            blobs.add(0, newBlob);
045        }
046        return blobs;
047    }
048
049    protected Blob processNoteBlob(Blob blob) throws PreviewException {
050        try {
051            String note = blob.getString();
052            StringBuilder sb = new StringBuilder();
053            sb.append("<html><body>");
054            sb.append(note);
055            sb.append("</body></html>");
056
057            String mimeType = blob.getMimeType();
058            if (mimeType == null) {
059                mimeType = "text/html";
060            }
061            return Blobs.createBlob(sb.toString(), mimeType, blob.getEncoding());
062        } catch (IOException e) {
063            throw new PreviewException(e);
064        } catch (UnsupportedCharsetException e) {
065            throw new PreviewException(e);
066        }
067    }
068
069    @Override
070    public List<Blob> getPreviewBlobs(String xpath) throws PreviewException {
071        return getPreviewBlobs();
072    }
073
074}