001/*
002 * (C) Copyright 2011 Nuxeo SA (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 *     Quentin Lamerand <qlamerand@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.platform.forms.layout.actions;
019
020import static org.jboss.seam.ScopeType.CONVERSATION;
021import static org.jboss.seam.annotations.Install.FRAMEWORK;
022
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.jboss.seam.annotations.Install;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.Blobs;
034import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
035import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
036import org.nuxeo.ecm.core.convert.api.ConversionException;
037import org.nuxeo.ecm.core.convert.api.ConversionService;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Seam component used for HTML conversion in rich text with MIME type widget
042 *
043 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a>
044 * @author <a href="mailto:bdelbosc@nuxeo.com">Benoit Delbosc</a>
045 * @since 5.5
046 */
047@Name("richTextEditorActions")
048@Scope(CONVERSATION)
049@Install(precedence = FRAMEWORK)
050// TODO: move elsewhere, this is not related to layouts and widgets management
051// and could be moved to a JSF function instead of adding a dependency to Seam
052public class RichTextEditorActions implements Serializable {
053
054    private static final long serialVersionUID = 1L;
055
056    private static final Log log = LogFactory.getLog(RichTextEditorActions.class);
057
058    public String convertToHtml(String text, String mimeType) {
059        BlobHolder bh = new SimpleBlobHolder(Blobs.createBlob(text, mimeType, "UTF-8"));
060        Map<String, Serializable> parameters = new HashMap<String, Serializable>();
061        parameters.put("bodyContentOnly", Boolean.TRUE);
062        try {
063            bh = Framework.getService(ConversionService.class).convertToMimeType("text/html", bh, parameters);
064            text = bh.getBlob().getString();
065        } catch (ConversionException | IOException e) {
066            log.error("Failed to convert to HTML.", e);
067        }
068        return text;
069    }
070
071}