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; 023 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.theme.elements.Element; 030import org.nuxeo.theme.fragments.Fragment; 031import org.nuxeo.theme.fragments.FragmentType; 032import org.nuxeo.theme.rendering.RenderingInfo; 033import org.nuxeo.theme.views.TemplateView; 034import org.nuxeo.theme.views.ViewType; 035 036public class HTMLView extends TemplateView { 037 038 private static final Log log = LogFactory.getLog(HTMLView.class); 039 040 private static final Pattern firstTagPattern = Pattern.compile("<([a-zA-Z0-9:]*)[^>]*>", Pattern.DOTALL); 041 042 private static final String[] ALLOWED_TAGS = { "html", "body", "table", "tr", "td", "div" }; 043 044 @Override 045 public String render(final RenderingInfo info) { 046 final ViewType viewType = getViewType(); 047 final String template = viewType.getTemplate(); 048 String result = getTemplateContent(template); 049 050 // Sanity check 051 final Matcher matcher = firstTagPattern.matcher(result); 052 if (matcher.find()) { 053 final String tag = matcher.group(1).toLowerCase(); 054 boolean found = false; 055 for (String allowedTag : ALLOWED_TAGS) { 056 if (tag.equals(allowedTag)) { 057 found = true; 058 } 059 } 060 if (!found) { 061 // log.warn(String.format( 062 // "First HTML tag of view template: %s (<%s>) not one of <html>, <body>, <div>, <table>, <tr>, <td>", 063 // template, tag)); 064 } 065 } else { 066 log.warn("First HTML tag of view template: " + template + " not found"); 067 } 068 069 // replace model expressions 070 final Element element = info.getElement(); 071 if (element instanceof Fragment) { 072 final Fragment fragment = (Fragment) element; 073 if (!info.isDirty() && hasModelExpressions(result)) { 074 final FragmentType fragmentType = fragment.getFragmentType(); 075 String fragmentTypeName = fragmentType.getTypeName(); 076 log.warn(String.format( 077 "Cannot render \"%s\" with the view \"%s\" because the view's template contains a 'nxthemesInfo' variable and the fragment type is not declared as dynamic. Try to add <dynamic>true</dynamic> in the <fragment name=\"%s\"> definition.", 078 fragmentTypeName, getViewType().getViewName(), fragmentTypeName)); 079 return ""; 080 } 081 result = replaceModelExpressions(info, result); 082 } 083 084 String cssClassName = element.getCssClassName(); 085 if (cssClassName != null) { 086 result = CSSUtils.insertCssClass(result, cssClassName); 087 } 088 089 // replace [nxthemes markup] strings with the actual markup 090 result = result.replace("[nxthemes markup]", info.getMarkup()); 091 092 return result; 093 } 094 095 public String replaceModelExpressions(@SuppressWarnings("unused") final RenderingInfo info, final String html) { 096 return html; 097 } 098 099 public boolean hasModelExpressions(final String html) { 100 return html.contains("nxthemesInfo"); 101 } 102 103}