001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Jean-Marc Orliaguet, Chalmers 011 * 012 * $Id$ 013 */ 014 015package org.nuxeo.theme.html.ui; 016 017import java.util.HashMap; 018import java.util.Map; 019import java.util.Properties; 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.theme.Manager; 025import org.nuxeo.theme.elements.ElementFormatter; 026import org.nuxeo.theme.elements.ThemeElement; 027import org.nuxeo.theme.formats.widgets.Widget; 028 029public class Head { 030 031 private static final Log log = LogFactory.getLog(Head.class); 032 033 protected static Map<String, String> iconsMime = new HashMap<>(); 034 035 public static String render(Map<String, String> params) { 036 StringBuilder sb = new StringBuilder(); 037 038 String themeName = params.get("themeName"); 039 final ThemeElement theme = Manager.getThemeManager().getThemeByName(themeName); 040 final Widget widget = (Widget) ElementFormatter.getFormatFor(theme, "widget"); 041 042 if (widget == null) { 043 log.warn("Theme " + themeName + " has no widget."); 044 } else { 045 final Properties properties = widget.getProperties(); 046 047 // Charset 048 final String charset = properties.getProperty("charset", "utf-8"); 049 sb.append(String.format("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>", charset)); 050 051 // Site icon 052 String icon = properties.getProperty("icon", "/favicon.ico"); 053 String mimeType = getMimeType(icon); 054 sb.append(String.format("<link rel=\"icon\" href=\"%s\" type=\"" + mimeType + "\"/>", icon)); 055 056 // If specified use a real .ico file for IE 057 final String iconIco = properties.getProperty("iconIco", null); 058 if (StringUtils.isNotEmpty(iconIco)) { 059 icon = iconIco; 060 } 061 sb.append(String.format("<link rel=\"shortcut icon\" href=\"%s\" type=\"image/x-icon\"/>", icon)); 062 } 063 064 // Base URL 065 final String baseUrl = params.get("baseUrl"); 066 if (baseUrl != null) { 067 sb.append(String.format("<base href=\"%s\" />", baseUrl)); 068 } 069 070 return sb.toString(); 071 } 072 073 protected static String getMimeType(String ico) { 074 // Use a map to cache mimetype for ico 075 if (!iconsMime.containsKey(ico)) { 076 iconsMime.put(ico, resolveMimeType(ico)); 077 } 078 return iconsMime.get(ico); 079 } 080 081 protected static String resolveMimeType(String ico) { 082 int index = ico.lastIndexOf("."); 083 if (index > 0) { 084 // Handle only gif and png 085 String ext = ico.substring(1 + index); 086 switch (ext) { 087 case "gif": 088 return "image/gif"; 089 case "png": 090 return "image/png"; 091 } 092 } 093 return "image/x-icon"; 094 } 095 096}