com.hfg.css.CSSRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.css;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
CSS Rule encapsulation.
@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 CSSRule
{
private Set mMediaQueries;
private List mSelectors;
private List mDeclarations;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public CSSRule()
{
}
//--------------------------------------------------------------------------
public CSSRule(String inRuleString)
{
List cssRules;
try
{
cssRules = new CSS(new StringReader(inRuleString)).getCSSRules();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
if (cssRules.size() != 1)
{
throw new RuntimeException("Only a single CSS rule can be passed to the CSSRule constructor!");
}
mSelectors = cssRules.get(0).getSelectors();
mDeclarations = cssRules.get(0).getDeclarations();
}
//--------------------------------------------------------------------------
public CSSRule(List inSelectors, List inDeclarations)
{
mSelectors = inSelectors;
mDeclarations = inDeclarations;
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
public String toString()
{
StringBuilderPlus buffer = new StringBuilderPlus();
String indent = "";
if (CollectionUtil.hasValues(mMediaQueries))
{
buffer.append("@media ");
buffer.append(StringUtil.join(mMediaQueries, ", "));
buffer.appendln(" {");
indent = " ";
}
if (CollectionUtil.hasValues(mSelectors))
{
buffer.append(indent + StringUtil.join(mSelectors, ", "));
buffer.appendln(" {");
buffer.append(indent + " ");
if (CollectionUtil.hasValues(mDeclarations))
{
buffer.append(StringUtil.join(mDeclarations, ";\n " + indent));
buffer.appendln(";");
}
buffer.appendln(indent + "}");
}
if (CollectionUtil.hasValues(mMediaQueries))
{
buffer.appendln("}");
}
return buffer.toString();
}
//--------------------------------------------------------------------------
public String getDeclarationString()
{
String declarationString = "";
if (CollectionUtil.hasValues(mDeclarations))
{
declarationString = StringUtil.join(mDeclarations, "; ");
}
return declarationString;
}
//--------------------------------------------------------------------------
public CSSRule setMediaType(CSSMediaType inValue)
{
if (mMediaQueries != null)
{
mMediaQueries.clear();
}
addMediaQuery(new CSSMediaQuery().setMediaType(inValue));
return this;
}
//--------------------------------------------------------------------------
public CSSRule setMediaQueries(Collection inValue)
{
if (! CollectionUtil.hasValues(inValue))
{
mMediaQueries = null;
}
else
{
for (CSSMediaQuery mediaQuery : inValue)
{
addMediaQuery(mediaQuery);
}
}
return this;
}
//--------------------------------------------------------------------------
public CSSRule addMediaQuery(CSSMediaQuery inValue)
{
if (null == mMediaQueries)
{
mMediaQueries = new HashSet<>(2);
}
mMediaQueries.add(inValue);
return this;
}
//--------------------------------------------------------------------------
// Kind of simplistic at the moment.
public boolean appliesTo(CSSMediaType inValue)
{
boolean applies = true;
if (inValue != null
&& mMediaQueries != null)
{
applies = false;
for (CSSMediaQuery mediaQuery : mMediaQueries)
{
if (null == mediaQuery.getMediaType()
|| mediaQuery.getMediaType().equals(inValue))
{
applies = true;
break;
}
}
}
return applies;
}
//--------------------------------------------------------------------------
public Set getMediaQueries()
{
return mMediaQueries;
}
//--------------------------------------------------------------------------
public CSSRule addSelector(String inValue)
{
return addSelector(new CSSSelector(inValue));
}
//--------------------------------------------------------------------------
public CSSRule addSelector(CSSSelector inValue)
{
if (null == mSelectors)
{
mSelectors = new ArrayList<>(5);
}
mSelectors.add(inValue);
return this;
}
//--------------------------------------------------------------------------
public List getSelectors()
{
return mSelectors;
}
//--------------------------------------------------------------------------
public CSSRule addDeclaration(CSSProperty inProperty, String inValue)
{
return addDeclaration(new CSSDeclaration(inProperty, inValue));
}
//--------------------------------------------------------------------------
public CSSRule addDeclaration(CSSProperty inProperty, int inValue)
{
return addDeclaration(new CSSDeclaration(inProperty, inValue));
}
//--------------------------------------------------------------------------
public CSSRule addDeclaration(CSSDeclaration inValue)
{
if (null == mDeclarations)
{
mDeclarations = new ArrayList<>(10);
}
mDeclarations.add(inValue);
return this;
}
//--------------------------------------------------------------------------
public List getDeclarations()
{
return mDeclarations;
}
}