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

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

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


import java.util.ArrayList;
import java.util.List;

import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;

/**
 */
//------------------------------------------------------------------------------
/**
 Class to encapsulate a CSS media query.
 See: http://www.w3.org/TR/css3-mediaqueries/

 @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 CSSMediaQuery
{
   public enum Flag { ONLY, NOT }

   private Flag         mFlag;
   private CSSMediaType mMediaType;
   private List mExpressions;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public CSSMediaQuery()
   {
   }

   //--------------------------------------------------------------------------
   public CSSMediaQuery(String inStringValue)
   {
      parse(inStringValue);
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   public CSSMediaQuery setFlag(Flag inValue)
   {
      mFlag = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public CSSMediaQuery setMediaType(CSSMediaType inValue)
   {
      mMediaType = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public CSSMediaType getMediaType()
   {
      return mMediaType;
   }

   //--------------------------------------------------------------------------
   public CSSMediaQuery addExpression(String inValue)
   {
      if (null == mExpressions)
      {
         mExpressions = new ArrayList(4);
      }

      mExpressions.add(inValue);

      return this;
   }

   //--------------------------------------------------------------------------
   public List getExpressions()
   {
      return mExpressions;
   }

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" ");

      if (mFlag != null)
      {
         buffer.append(mFlag);
      }

      if (mMediaType != null)
      {
         buffer.delimitedAppend(mMediaType);
      }

      if (CollectionUtil.hasValues(mExpressions))
      {
         for (String expression : mExpressions)
         {
            buffer.delimitedAppend(expression);
         }
      }

      return buffer.toString();
   }

   //##########################################################################
   // PRIVATE METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   private void parse(String inStringValue)
         throws CSSException
   {
      try
      {
         if (StringUtil.isSet(inStringValue))
         {
            String stringValue = inStringValue.trim();

            // Flag present?
            int firstSpaceIndex = stringValue.indexOf(" ");
            if (firstSpaceIndex > 0)
            {
               String token = stringValue.substring(0, firstSpaceIndex);
               for (Flag flag : Flag.values())
               {
                  if (token.equalsIgnoreCase(flag.name()))
                  {
                     setFlag(flag);
                     stringValue = stringValue.substring(firstSpaceIndex).trim();
                     break;
                  }
               }
            }

            // Media type present?
            firstSpaceIndex = stringValue.indexOf(" ");
            String token;
            if (firstSpaceIndex > 0)
            {
               token = stringValue.substring(0, firstSpaceIndex);
            }
            else
            {
               token = stringValue;
            }

            for (CSSMediaType mediaType : CSSMediaType.values())
            {
               if (token.equalsIgnoreCase(mediaType.name()))
               {
                  setMediaType(mediaType);
                  if (stringValue.length() > token.length())
                  {
                     stringValue = stringValue.substring(token.length()).trim();
                  }
                  else
                  {
                     stringValue = "";
                  }
                  break;
               }
            }

            // Expressions present?
            String[] expressionStrings = stringValue.split("\\b(?i)AND\\b");
            for (String expressionString : expressionStrings)
            {
               if (StringUtil.isSet(expressionString))
               {
                  addExpression(expressionString.trim());
               }
            }
         }
      }
      catch (Exception e)
      {
         throw new CSSException(e);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy