001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.html.ui;
023
024import java.io.IOException;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.theme.Manager;
030import org.nuxeo.theme.Utils;
031import org.nuxeo.theme.resources.ResourceType;
032import org.nuxeo.theme.types.TypeFamily;
033
034public class MVCElement {
035
036    private static final Log log = LogFactory.getLog(MVCElement.class);
037
038    public static String render(Map<String, String> params) {
039        StringBuilder sb = new StringBuilder();
040
041        String resource = params.get("resource");
042        String url = params.get("url");
043        String body = params.get("body");
044        String className = params.get("className");
045
046        sb.append(String.format("<ins class=\"%s\">", className));
047
048        /* insert the content from a file source */
049        if (null != resource) {
050            ResourceType resourceType = (ResourceType) Manager.getTypeRegistry().lookup(TypeFamily.RESOURCE, resource);
051            if (resourceType == null) {
052                log.warn("Could not find resource: " + resource);
053            } else {
054                try {
055                    sb.append(Utils.readResourceAsString(resourceType.getPath()));
056                } catch (IOException e) {
057                    log.warn("Could not find resource: " + resource);
058                }
059            }
060        }
061
062        /* get the content from a url */
063        if (null != url) {
064            sb.append(String.format(" cite=%s", url));
065        }
066
067        /* get the content from the body */
068        if (null != body) {
069            sb.append(body);
070        }
071
072        sb.append("</ins>");
073
074        return sb.toString();
075    }
076
077}