001package com.hfg.css;
002
003
004import java.io.IOException;
005import java.io.StringReader;
006import java.util.*;
007
008import com.hfg.util.collection.CollectionUtil;
009import com.hfg.util.StringBuilderPlus;
010import com.hfg.util.StringUtil;
011
012//------------------------------------------------------------------------------
013/**
014 CSS Rule encapsulation.
015
016 @author J. Alex Taylor, hairyfatguy.com
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class CSSRule
040{
041   private Set<CSSMediaQuery>   mMediaQueries;
042   private List<CSSSelector>    mSelectors;
043   private List<CSSDeclaration> mDeclarations;
044
045   //##########################################################################
046   // CONSTRUCTORS
047   //##########################################################################
048
049   //--------------------------------------------------------------------------
050   public CSSRule()
051   {
052   }
053
054   //--------------------------------------------------------------------------
055   public CSSRule(String inRuleString)
056   {
057      List<CSSRule> cssRules;
058      try
059      {
060         cssRules = new CSS(new StringReader(inRuleString)).getCSSRules();
061      }
062      catch (IOException e)
063      {
064         throw new RuntimeException(e);
065      }
066
067      if (cssRules.size() != 1)
068      {
069         throw new RuntimeException("Only a single CSS rule can be passed to the CSSRule constructor!");
070      }
071
072      mSelectors    = cssRules.get(0).getSelectors();
073      mDeclarations = cssRules.get(0).getDeclarations();
074   }
075
076   //--------------------------------------------------------------------------
077   public CSSRule(List<CSSSelector> inSelectors, List<CSSDeclaration> inDeclarations)
078   {
079      mSelectors    = inSelectors;
080      mDeclarations = inDeclarations;
081   }
082
083   //##########################################################################
084   // PUBLIC METHODS
085   //##########################################################################
086
087   //--------------------------------------------------------------------------
088   @Override
089   public String toString()
090   {
091      StringBuilderPlus buffer = new StringBuilderPlus();
092
093      String indent = "";
094      if (CollectionUtil.hasValues(mMediaQueries))
095      {
096         buffer.append("@media ");
097         buffer.append(StringUtil.join(mMediaQueries, ", "));
098         buffer.appendln(" {");
099         indent = " ";
100      }
101
102      if (CollectionUtil.hasValues(mSelectors))
103      {
104         buffer.append(indent + StringUtil.join(mSelectors, ", "));
105         buffer.appendln(" {");
106         buffer.append(indent + "  ");
107         if (CollectionUtil.hasValues(mDeclarations))
108         {
109            buffer.append(StringUtil.join(mDeclarations, ";\n  " + indent));
110            buffer.appendln(";");
111         }
112         buffer.appendln(indent + "}");
113      }
114
115      if (CollectionUtil.hasValues(mMediaQueries))
116      {
117         buffer.appendln("}");
118      }
119
120      return buffer.toString();
121   }
122
123   //--------------------------------------------------------------------------
124   public String getDeclarationString()
125   {
126      String declarationString = "";
127
128      if (CollectionUtil.hasValues(mDeclarations))
129      {
130         declarationString = StringUtil.join(mDeclarations, "; ");
131      }
132
133      return declarationString;
134   }
135
136   //--------------------------------------------------------------------------
137   public CSSRule setMediaType(CSSMediaType inValue)
138   {
139      if (mMediaQueries != null)
140      {
141         mMediaQueries.clear();
142      }
143
144      addMediaQuery(new CSSMediaQuery().setMediaType(inValue));
145
146      return this;
147   }
148
149   //--------------------------------------------------------------------------
150   public CSSRule setMediaQueries(Collection<CSSMediaQuery> inValue)
151   {
152      if (! CollectionUtil.hasValues(inValue))
153      {
154         mMediaQueries = null;
155      }
156      else
157      {
158         for (CSSMediaQuery mediaQuery : inValue)
159         {
160            addMediaQuery(mediaQuery);
161         }
162      }
163
164      return this;
165   }
166
167   //--------------------------------------------------------------------------
168   public CSSRule addMediaQuery(CSSMediaQuery inValue)
169   {
170      if (null == mMediaQueries)
171      {
172         mMediaQueries = new HashSet<>(2);
173      }
174
175      mMediaQueries.add(inValue);
176
177      return this;
178   }
179
180   //--------------------------------------------------------------------------
181   // Kind of simplistic at the moment.
182   public boolean appliesTo(CSSMediaType inValue)
183   {
184      boolean applies = true;
185      if (inValue != null
186          && mMediaQueries != null)
187      {
188         applies = false;
189         for (CSSMediaQuery mediaQuery : mMediaQueries)
190         {
191            if (null == mediaQuery.getMediaType()
192                || mediaQuery.getMediaType().equals(inValue))
193            {
194               applies = true;
195               break;
196            }
197         }
198      }
199
200      return applies;
201   }
202
203   //--------------------------------------------------------------------------
204   public Set<CSSMediaQuery> getMediaQueries()
205   {
206      return mMediaQueries;
207   }
208
209   //--------------------------------------------------------------------------
210   public CSSRule addSelector(String inValue)
211   {
212      return addSelector(new CSSSelector(inValue));
213   }
214
215   //--------------------------------------------------------------------------
216   public CSSRule addSelector(CSSSelector inValue)
217   {
218      if (null == mSelectors)
219      {
220         mSelectors = new ArrayList<>(5);
221      }
222
223      mSelectors.add(inValue);
224
225      return this;
226   }
227
228   //--------------------------------------------------------------------------
229   public List<CSSSelector> getSelectors()
230   {
231      return mSelectors;
232   }
233
234   //--------------------------------------------------------------------------
235   public CSSRule addDeclaration(CSSProperty inProperty, String inValue)
236   {
237      return addDeclaration(new CSSDeclaration(inProperty, inValue));
238   }
239
240   //--------------------------------------------------------------------------
241   public CSSRule addDeclaration(CSSProperty inProperty, int inValue)
242   {
243      return addDeclaration(new CSSDeclaration(inProperty, inValue));
244   }
245
246   //--------------------------------------------------------------------------
247   public CSSRule addDeclaration(CSSDeclaration inValue)
248   {
249      if (null == mDeclarations)
250      {
251         mDeclarations = new ArrayList<>(10);
252      }
253
254      mDeclarations.add(inValue);
255
256      return this;
257   }
258
259   //--------------------------------------------------------------------------
260   public List<CSSDeclaration> getDeclarations()
261   {
262      return mDeclarations;
263   }
264}