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