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.filters.layout;
016
017import java.util.Enumeration;
018import java.util.regex.Matcher;
019import java.util.regex.Pattern;
020
021import org.nuxeo.theme.formats.Format;
022import org.nuxeo.theme.rendering.RenderingInfo;
023import org.nuxeo.theme.views.AbstractView;
024
025public class DefaultLayoutView extends AbstractView {
026
027    static final Pattern firstTagPattern = Pattern.compile("<(.*?)>", Pattern.DOTALL);
028
029    static final Pattern otherTagsPattern = Pattern.compile("<.*?>(.*)", Pattern.DOTALL);
030
031    static final Pattern styleAttrPattern = Pattern.compile(" style=\"(.*?)\"", Pattern.DOTALL);
032
033    @Override
034    public String render(final RenderingInfo info) {
035        final String markup = info.getMarkup();
036
037        final Format format = info.getFormat();
038        if (!format.hasProperties()) {
039            return markup;
040        }
041
042        final Matcher firstMatcher = firstTagPattern.matcher(markup);
043        final Matcher othersMatcher = otherTagsPattern.matcher(markup);
044
045        if (!(firstMatcher.find() && othersMatcher.find())) {
046            return markup;
047        }
048
049        // find a 'style="...."' match
050        String inBrackets = firstMatcher.group(1);
051        final Matcher styleAttrMatcher = styleAttrPattern.matcher(inBrackets);
052
053        // build a new 'style="..."' string
054        final StringBuilder styleAttributes = new StringBuilder();
055        if (styleAttrMatcher.find()) {
056            styleAttributes.append(styleAttrMatcher.group(1));
057            if (!styleAttributes.toString().endsWith(";")) {
058                styleAttributes.append(';');
059            }
060        }
061
062        // add new attributes
063        for (Enumeration<?> propertyNames = format.getPropertyNames(); propertyNames.hasMoreElements();) {
064            String propertyName = (String) propertyNames.nextElement();
065            styleAttributes.append(propertyName);
066            styleAttributes.append(':');
067            styleAttributes.append(format.getProperty(propertyName));
068            styleAttributes.append(';');
069        }
070
071        if (styleAttributes.length() == 0) {
072            return markup;
073        }
074        // remove the old 'style="..."' attributes, if there were some
075        inBrackets = inBrackets.replaceAll(styleAttrPattern.toString(), "");
076
077        // write the final markup
078        if (inBrackets.endsWith("/")) {
079            return String.format("<%s style=\"%s\" />%s", inBrackets.replaceAll("/$", "").trim(),
080                    styleAttributes.toString(), othersMatcher.group(1));
081        }
082        return String.format("<%s style=\"%s\">%s", inBrackets, styleAttributes.toString(), othersMatcher.group(1));
083    }
084
085}