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