All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.css.CssUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.css;

import java.awt.Color;
import java.util.Map;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import com.hfg.graphics.ColorUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;

//------------------------------------------------------------------------------
/**
 * Contains CSS-related utility functions.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class CssUtil
{
   //###########################################################################
   // PUBLIC FUNCTIONS
   //###########################################################################

   //--------------------------------------------------------------------------
   public static String colorToCssValue(Color inValue)
   {
      String value = "";
      if (inValue != null)
      {
         if (255 == inValue.getAlpha())
         {
            // No opacity
            value = "#" + ColorUtil.colorToHex(inValue);
         }
         else
         {
            // Use rgba() format to specify the opacity
            value = "rgba(" + inValue.getRed() + ", " + inValue.getGreen() + ", " + inValue.getBlue()
                    + ", " + String.format("%.1f", inValue.getAlpha()/255f)+ ")";
         }
      }

      return value;
   }

   //--------------------------------------------------------------------------
   /**
    Adds the specified style attribute content to the specified tag.
    @param inTag    extends XMLTag instead of HTMLTag so that the SVG classes can use it as well.
    @param inValue  style value to add
    */
   public static void addStyle(XMLTag inTag, String inValue)
   {
      if (StringUtil.isSet(inValue))
      {
         String oldValue = inTag.getAttributeValue(CSS.STYLE);
         if (StringUtil.isSet(oldValue))
         {
            String[] attrPairs = inValue.trim().split(";");
            Map styleAttrMap = new HashMap<>(attrPairs.length);
            for (String pair : attrPairs)
            {
               String[] pieces = pair.split(":");
               styleAttrMap.put(pieces[0].trim(), pieces[1].trim());
            }

            StringBuilderPlus buffer = new StringBuilderPlus(oldValue.trim()).setDelimiter(";");
            for (String attrName : styleAttrMap.keySet())
            {
               boolean newAttr = true;
               if (buffer.indexOf(attrName) >= 0)
               {
                  // The specified attribute may already be present. Check more carefully
                  Pattern pattern = Pattern.compile("(?:^|;|\\s)" + attrName + "\\s*:\\s*(.+?)(?:;|$)");
                  Matcher m = pattern.matcher(buffer.toString());
                  if (m.find())
                  {
                     if (styleAttrMap.get(attrName).contains("!important")
                           || ! m.group(1).contains("!important"))
                     {
                        buffer.replace(m.start(1), m.end(1), styleAttrMap.get(attrName));
                     }

                     newAttr = false;
                  }
               }

               if (newAttr)
               {
                  // Add the new style attribute
                  if (buffer.charAt(buffer.length() -1) == ';')
                  {
                     buffer.append(' ');
                     buffer.append(attrName);
                  }
                  else
                  {
                     buffer.delimitedAppend(attrName);
                  }

                  buffer.append(":");
                  buffer.append(styleAttrMap.get(attrName));
               }

            }

            inValue = buffer.toString();
         }

         inTag.setAttribute(CSS.STYLE, inValue);
      }
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy