cz.vutbr.web.csskit.fn.GenericGradient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jstyleparser Show documentation
Show all versions of jstyleparser Show documentation
jStyleParser is a CSS parser written in Java. It has its own application interface that is designed to allow an efficient CSS processing in Java and mapping the values to the Java data types. It parses CSS 2.1 style sheets into structures that can be efficiently assigned to DOM elements. It is intended be the primary CSS parser for the CSSBox library. While handling errors, it is user agent conforming according to the CSS specification.
/**
* GenericGradient.java
*
* Created on 17. 5. 2018, 12:52:23 by burgetr
*/
package cz.vutbr.web.csskit.fn;
import java.util.ArrayList;
import java.util.List;
import cz.vutbr.web.css.Term;
import cz.vutbr.web.css.TermColor;
import cz.vutbr.web.css.TermFunction;
import cz.vutbr.web.css.TermLengthOrPercent;
import cz.vutbr.web.css.TermFunction.Gradient.ColorStop;
import cz.vutbr.web.csskit.ColorStopImpl;
import cz.vutbr.web.csskit.TermFunctionImpl;
/**
* Implementation of common features of all the gradient functions.
*
* @author burgetr
*/
public class GenericGradient extends TermFunctionImpl
{
private List colorStops;
public GenericGradient() {
setValid(false);
}
public List getColorStops() {
return colorStops;
}
protected void loadColorStops(List>> args, int firstStop) {
colorStops = decodeColorStops(args, firstStop);
}
/**
* Loads the color stops from the gunction arguments.
* @param args the comma-separated function arguments
* @param firstStop the first argument to start with
* @return the list of color stops or {@code null} when the arguments are invalid or missing
*/
protected List decodeColorStops(List>> args, int firstStop) {
boolean valid = true;
List colorStops = null;
if (args.size() > firstStop) {
colorStops = new ArrayList<>();
for (int i = firstStop; valid && i < args.size(); i++) {
List> sarg = args.get(i);
if (sarg.size() == 1 || sarg.size() == 2) {
Term> tclr = sarg.get(0);
Term> tlen = (sarg.size() == 2) ? sarg.get(1) : null;
if (tclr instanceof TermColor
&& (tlen == null || tlen instanceof TermLengthOrPercent)) {
TermFunction.Gradient.ColorStop newStop = new ColorStopImpl((TermColor) tclr, (TermLengthOrPercent) tlen);
colorStops.add(newStop);
} else {
valid = false;
}
} else {
valid = false;
}
}
}
if (valid && colorStops != null && !colorStops.isEmpty())
return colorStops;
else
return null;
}
}