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