001package com.hfg.css;
002
003
004import java.util.ArrayList;
005import java.util.List;
006
007import com.hfg.util.collection.CollectionUtil;
008import com.hfg.util.StringBuilderPlus;
009import com.hfg.util.StringUtil;
010
011/**
012 */
013//------------------------------------------------------------------------------
014/**
015 Class to encapsulate a CSS media query.
016 See: http://www.w3.org/TR/css3-mediaqueries/
017
018 @author J. Alex Taylor, hairyfatguy.com
019 */
020//------------------------------------------------------------------------------
021// com.hfg XML/HTML Coding Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041
042public class CSSMediaQuery
043{
044   public enum Flag { ONLY, NOT }
045
046   private Flag         mFlag;
047   private CSSMediaType mMediaType;
048   private List<String> mExpressions;
049
050   //##########################################################################
051   // CONSTRUCTORS
052   //##########################################################################
053
054   //--------------------------------------------------------------------------
055   public CSSMediaQuery()
056   {
057   }
058
059   //--------------------------------------------------------------------------
060   public CSSMediaQuery(String inStringValue)
061   {
062      parse(inStringValue);
063   }
064
065   //##########################################################################
066   // PUBLIC METHODS
067   //##########################################################################
068
069   //--------------------------------------------------------------------------
070   public CSSMediaQuery setFlag(Flag inValue)
071   {
072      mFlag = inValue;
073      return this;
074   }
075
076   //--------------------------------------------------------------------------
077   public CSSMediaQuery setMediaType(CSSMediaType inValue)
078   {
079      mMediaType = inValue;
080      return this;
081   }
082
083   //--------------------------------------------------------------------------
084   public CSSMediaType getMediaType()
085   {
086      return mMediaType;
087   }
088
089   //--------------------------------------------------------------------------
090   public CSSMediaQuery addExpression(String inValue)
091   {
092      if (null == mExpressions)
093      {
094         mExpressions = new ArrayList<String>(4);
095      }
096
097      mExpressions.add(inValue);
098
099      return this;
100   }
101
102   //--------------------------------------------------------------------------
103   public List<String> getExpressions()
104   {
105      return mExpressions;
106   }
107
108   //--------------------------------------------------------------------------
109   @Override
110   public String toString()
111   {
112      StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" ");
113
114      if (mFlag != null)
115      {
116         buffer.append(mFlag);
117      }
118
119      if (mMediaType != null)
120      {
121         buffer.delimitedAppend(mMediaType);
122      }
123
124      if (CollectionUtil.hasValues(mExpressions))
125      {
126         for (String expression : mExpressions)
127         {
128            buffer.delimitedAppend(expression);
129         }
130      }
131
132      return buffer.toString();
133   }
134
135   //##########################################################################
136   // PRIVATE METHODS
137   //##########################################################################
138
139   //--------------------------------------------------------------------------
140   private void parse(String inStringValue)
141         throws CSSException
142   {
143      try
144      {
145         if (StringUtil.isSet(inStringValue))
146         {
147            String stringValue = inStringValue.trim();
148
149            // Flag present?
150            int firstSpaceIndex = stringValue.indexOf(" ");
151            if (firstSpaceIndex > 0)
152            {
153               String token = stringValue.substring(0, firstSpaceIndex);
154               for (Flag flag : Flag.values())
155               {
156                  if (token.equalsIgnoreCase(flag.name()))
157                  {
158                     setFlag(flag);
159                     stringValue = stringValue.substring(firstSpaceIndex).trim();
160                     break;
161                  }
162               }
163            }
164
165            // Media type present?
166            firstSpaceIndex = stringValue.indexOf(" ");
167            String token;
168            if (firstSpaceIndex > 0)
169            {
170               token = stringValue.substring(0, firstSpaceIndex);
171            }
172            else
173            {
174               token = stringValue;
175            }
176
177            for (CSSMediaType mediaType : CSSMediaType.values())
178            {
179               if (token.equalsIgnoreCase(mediaType.name()))
180               {
181                  setMediaType(mediaType);
182                  if (stringValue.length() > token.length())
183                  {
184                     stringValue = stringValue.substring(token.length()).trim();
185                  }
186                  else
187                  {
188                     stringValue = "";
189                  }
190                  break;
191               }
192            }
193
194            // Expressions present?
195            String[] expressionStrings = stringValue.split("\\b(?i)AND\\b");
196            for (String expressionString : expressionStrings)
197            {
198               if (StringUtil.isSet(expressionString))
199               {
200                  addExpression(expressionString.trim());
201               }
202            }
203         }
204      }
205      catch (Exception e)
206      {
207         throw new CSSException(e);
208      }
209   }
210}