001/*
002 * (C) Copyright 2006-2015 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.preview.adapter;
023
024import java.io.IOException;
025import java.io.UnsupportedEncodingException;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.commons.lang.StringUtils;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.platform.preview.api.PreviewException;
034
035import com.ibm.icu.text.CharsetDetector;
036
037public class PlainTextPreviewer extends AbstractPreviewer implements MimeTypePreviewer {
038
039    public String htmlContent(String content) {
040        // & and < are the only two that we really need to escape
041        // then we also make newlines visible
042        String escaped = StringUtils.replaceEach(content, //
043                new String[] { "&", "<", "\n" }, //
044                new String[] { "&amp;", "&lt;", "<br/>" });
045        return "<pre>" + escaped + "</pre>";
046    }
047
048    @Override
049    public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
050        List<Blob> blobResults = new ArrayList<Blob>();
051
052        StringBuilder htmlPage = new StringBuilder();
053
054        byte[] data;
055        try {
056            data = blob.getByteArray();
057        } catch (IOException e) {
058            throw new PreviewException("Cannot fetch blob content", e);
059        }
060        String encoding = blob.getEncoding();
061        if (StringUtils.isEmpty(encoding)) {
062            CharsetDetector detector = new CharsetDetector();
063            detector.setText(data);
064            encoding = detector.detect().getName();
065        }
066
067        String content = null;
068        try {
069            content = new String(data, encoding);
070        } catch (UnsupportedEncodingException e) {
071            throw new PreviewException("Cannot encode blob content to string", e);
072        }
073
074        htmlPage.append("<?xml version=\"1.0\" encoding=\"UTF-8\"/>");
075        htmlPage.append("<html>");
076        htmlPage.append("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head>");
077        htmlPage.append("<body>");
078        htmlPage.append(htmlContent(content));
079        htmlPage.append("</body></html>");
080
081        Blob mainBlob = Blobs.createBlob(htmlPage.toString(), "text/html", "UTF-8", "index.html");
082
083        blobResults.add(mainBlob);
084        return blobResults;
085    }
086
087}