001/*
002 *  Copyright 2010 Anyware Services
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 */
016package org.ametys.cms.transformation.xslt;
017
018import java.util.regex.Matcher;
019import java.util.regex.Pattern;
020
021/**
022 * This class provides a helper for the in-line editor
023 */
024public final class InlineEditorHelper
025{
026    private InlineEditorHelper()
027    {
028        // empty
029    }
030    
031    /**
032     * Retrieves the value of a CSS property in the given HTML element style attribute
033     * @param style The value of the HTML element style attribute (for example : "text-align:center; float:left; color: #000;")
034     * @param parameter The CSS property ("text-align", "float", "color", ...)
035     * @return the value of the given CSS property or empty String if not found
036     */
037    public static String getStyleValue(String style, String parameter)
038    {
039        return getStyleValue(style, parameter, "");
040    }
041
042    /**
043     * Retrieves the value of a CSS property in the given HTML element style attribute
044     * @param style The value of the HTML element style attribute (for example : "text-align:center; float:left; color: #000;")
045     * @param parameter The CSS property ("text-align", "float", "color", ...)
046     * @param defaultValue The default value return if style is empty
047     * @return the value of the given CSS property or empty String if not found
048     */
049    public static String getStyleValue(String style, String parameter, String defaultValue)
050    {
051        if (style == null || "".equals(style))
052        {
053            return defaultValue;
054        }
055        
056        Pattern pattern = Pattern.compile(parameter + "\\s*:\\s*([^;]*)");
057        Matcher matcher = pattern.matcher(style);
058        
059        if (matcher.find())
060        {
061            String result = matcher.group(1);
062            return result == null ? defaultValue : result.trim();
063        }
064        else
065        {
066            return defaultValue;
067        }
068    }
069}