cz.vutbr.web.csskit.OutputUtil 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.
The newest version!
package cz.vutbr.web.csskit;
import java.util.List;
import cz.vutbr.web.css.PrettyOutput;
import cz.vutbr.web.css.Term;
import cz.vutbr.web.css.TermOperator;
/**
* Helper class for generation output for given CSS rules
* @author kapy
*
*/
public class OutputUtil {
public static final String EMPTY_DELIM = "";
public static final String SPACE_DELIM = " ";
public static final String DEPTH_DELIM = "\t";
public static final String QUERY_DELIM = " AND ";
public static final String RULE_OPENING = " {\n";
public static final String RULE_CLOSING = "}\n";
public static final String MEDIA_DELIM = ", ";
public static final String SELECTOR_DELIM = ", ";
public static final String IMPORT_KEYWORD = "@import ";
public static final String URL_OPENING = "url('";
public static final String URL_CLOSING = "')";
public static final String LINE_CLOSING = ";\n";
public static final String NEW_LINE = "\n";
public static final String MEDIA_KEYWORD = "@media ";
public static final String KEYFRAMES_KEYWORD = "@keyframes ";
public static final String RULE_DELIM = "\n";
public static final String CHARSET_KEYWORD = "@charset ";
public static final String CHARSET_OPENING = "\"";
public static final String CHARSET_CLOSING = "\";\n";
public static final String PROPERTY_OPENING = ": ";
public static final String PROPERTY_CLOSING = ";\n";
public static final String IMPORTANT_KEYWORD = "!important";
public static final String PAGE_KEYWORD = "@page";
public static final String PSEUDO_OPENING = ":";
public static final String PAGE_CLOSING = "";
public static final String VIEWPORT_KEYWORD = "@viewport";
public static final String FONT_FACE_KEYWORD = "@font-face";
public static final String FUNCTION_OPENING = "(";
public static final String FUNCTION_CLOSING = ")";
public static final String STRING_OPENING = "'";
public static final String STRING_CLOSING = "'";
public static final String ATTRIBUTE_OPENING = "[";
public static final String ATTRIBUTE_CLOSING = "]";
public static final String PERCENT_SIGN = "%";
public static final String HASH_SIGN = "#";
public static final String MARGIN_AREA_OPENING = "@";
public static final String MEDIA_EXPR_OPENING = "(";
public static final String MEDIA_EXPR_CLOSING = ")";
public static final String MEDIA_FEATURE_DELIM = ": ";
public static final String CALC_KEYWORD = "calc";
public static final String RECT_KEYWORD = "rect";
/**
* Appends string multiple times to buffer
* @param sb StringBuilder to be modified
* @param append String to be added
* @param times Number of times append
is added
* @return Modified StringBuilder sb
to allow chaining
*/
public static StringBuilder appendTimes(StringBuilder sb, String append, int times) {
for(;times>0; times--)
sb.append(append);
return sb;
}
/**
* Appends all elements of array
to buffer, separated by delimiter
* @param Type of elements stored in array
* @param sb StringBuilder to be modified
* @param array Array of elements
* @param delimiter Delimiter to separate elements
* @return Modified sb
to allow chaining
*/
public static StringBuilder appendArray(StringBuilder sb, T[] array, String delimiter) {
boolean firstRun = true;
for(T elem: array) {
if(!firstRun)
sb.append(delimiter);
else
firstRun = false;
sb.append(elem.toString());
}
return sb;
}
/**
* Appends all elements of list
to buffer, separated by delimiter
* @param Type of elements stored in list
* @param sb StringBuilder to be modified
* @param list List of elements
* @param delimiter Delimiter to separate elements
* @return Modified sb
to allow chaining
*/
public static StringBuilder appendList(StringBuilder sb, List list, String delimiter) {
boolean firstRun = true;
for(T elem: list) {
if(!firstRun)
sb.append(delimiter);
else
firstRun = false;
sb.append(elem.toString());
}
return sb;
}
/**
* Appends of elements of list
to list, separater by delimiter.
* Uses depth parameter to make output nicer for each element
* @param List of elements, which implements Rule
* @param sb StringBuilder to be modified
* @param list List of elements
* @param delimiter Delimeter between elements
* @param depth Depth of each element
* @return Modified sb
to allow chaining
*/
public static StringBuilder appendList(
StringBuilder sb, List list,
String delimiter, int depth) {
boolean firstRun = true;
for(T elem: list) {
if(!firstRun)
sb.append(delimiter);
else
firstRun = false;
sb.append(elem.toString(depth));
}
return sb;
}
/**
* Appends the calc() function arguments to a string builder.
* @param sb the string builder to be modified
* @param args the calc arguments
* @return Modified sb
to allow chaining
*/
public static StringBuilder appendCalcArgs(StringBuilder sb, CalcArgs args) {
final String astr = args.evaluate(CalcArgs.stringEvaluator);
if (!astr.startsWith(FUNCTION_OPENING)) {
sb.append(FUNCTION_OPENING);
sb.append(astr);
sb.append(FUNCTION_CLOSING);
} else {
sb.append(astr);
}
return sb;
}
/**
* Appends the formatted list of function arguments to a string builder. The individual
* arguments are separated by spaces with the exception of commas.
* @param sb the string builder to be modified
* @param list the list of function arguments
* @return Modified sb
to allow chaining
*/
public static StringBuilder appendFunctionArgs(StringBuilder sb, List> list) {
Term> prev = null, pprev = null;
for (Term> elem : list) {
boolean sep = true;
if (elem instanceof TermOperator && ((TermOperator) elem).getValue() == ',')
sep = false; //no spaces before commas
if ((prev != null && prev instanceof TermOperator && ((TermOperator) prev).getValue() == '-')
&& (pprev == null || pprev instanceof TermOperator)) //nothing or an operator before -
sep = false; //no spaces after unary minus
if (prev != null && sep)
sb.append(SPACE_DELIM);
pprev = prev;
prev = elem;
sb.append(elem.toString());
}
return sb;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy