Java Code Examples for cz.vutbr.web.css.Declaration#asList()

The following examples show how to use cz.vutbr.web.css.Declaration#asList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlUtils.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method provides a workaround for bug in jStyleParser library that is currently used. To be used instead of
 * a plain {@link cz.vutbr.web.css.Declaration#toString()} method.
 * (see https://github.com/radkovo/jStyleParser/commit/356f39348929569eb8117bc14afd70687d6a23dc)
 * @param declaration CSS declaration representation to stringify.
 * @return a string representation of a CSS declaration.
 */
public static String toString(Declaration declaration) {
    StringBuilder sb = new StringBuilder();
    sb.append(declaration.getProperty()).append(OutputUtil.PROPERTY_OPENING);
    for (Term<?> term : declaration.asList()) {
        if (term instanceof TermIntegerImpl) {
            Term.Operator operator = term.getOperator();
            if (operator != null) {
                sb.append(operator.value());
            }
            sb.append(term.toString());
        } else {
            sb.append(term.toString());
        }
    }
    if (declaration.isImportant()) {
        sb.append(OutputUtil.SPACE_DELIM).append(OutputUtil.IMPORTANT_KEYWORD);
    }
    sb.append(OutputUtil.PROPERTY_CLOSING.trim());
    return sb.toString();
}
 
Example 2
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unused")
private boolean processTransform(Declaration d,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

    // just a simple value (e.g. "none")
    if (d.size() == 1 && Decoder.genericOneIdent(Transform.class, d, properties)) {
        return true;
    } else {

        TermList list = tf.createList();

        for (Term<?> t : d.asList()) {
            if (t instanceof TermFunction.TransformFunction)
                list.add(t);
            else
                return false;
        }
        // there is nothing in list after parsing
        if (list.isEmpty())
            return false;

        properties.put("transform", Transform.list_values);
        values.put("transform", list);
        return true;
    }
}
 
Example 3
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unused")
private boolean processQuotes(Declaration d,
		Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

	if (d.size() == 1
			&& Decoder.genericTermIdent(Quotes.class, d.get(0), Decoder.ALLOW_INH,
					"quotes", properties)) {
		return true;
	} else {
		TermList list = tf.createList();
		for (Term<?> term : d.asList()) {
			if (term instanceof TermString)
				list.add(term);
			else
				return false;
		}

		// there are pairs of quotes
		if (!list.isEmpty() && list.size() % 2 == 0) {
			properties.put("quotes", Quotes.list_values);
			values.put("quotes", list);
			return true;
		}
		return false;
	}
}
 
Example 4
Source File: Decoder.java    From jStyleParser with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Splits a declaration to multiple declarations by a separating term.
 * @param src the source declarations
 * @param separator separating operator
 * @return a list of declarations where each of them contains a sub-list of terms of the source declaration
 */
public static List<Declaration> splitDeclarations(Declaration src, Operator sepOperator)
{
    final List<Declaration> ret = new ArrayList<>();
    Declaration current = rf.createDeclaration();
    current.unlock();
    for (Term<?> t : src.asList()) {
        if (t.getOperator() == sepOperator) {
            ret.add(current);
            current = rf.createDeclaration();
            current.unlock();
        }
        current.add(t);
    }
    ret.add(current);
    return ret;
}
 
Example 5
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processCursor(Declaration d,
		Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

	if (d.size() == 1 && Decoder.genericOneIdent(Cursor.class, d, properties)) {
		return true;
	} else {

		final Set<Cursor> allowedCursors = EnumSet.complementOf(EnumSet
				.of(Cursor.INHERIT));

		TermList list = tf.createList();
		Cursor cur = null;
		for (Term<?> term : d.asList()) {
			if (term instanceof TermURI) {
				list.add(term);
			} else if (term instanceof TermIdent
					&& (cur = Decoder.genericPropertyRaw(Cursor.class,
							allowedCursors, (TermIdent) term)) != null) {
				// this have to be the last cursor in sequence
				// and only one Decoder.generic cursor is allowed
				if (d.indexOf(term) != d.size() - 1)
					return false;

				// passed as last cursor, insert into properties and values
				properties.put("cursor", cur);
				if (!list.isEmpty())
					values.put("cursor", list);
				return true;
			} else
				return false;
		}
		return false;
	}
}
 
Example 6
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processTextDecoration(Declaration d,
		Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

	final Set<TextDecoration> availableDecorations = EnumSet.of(
			TextDecoration.BLINK, TextDecoration.LINE_THROUGH,
			TextDecoration.OVERLINE, TextDecoration.UNDERLINE);

	// it one term
	if (d.size() == 1) {
		return Decoder.genericOneIdent(TextDecoration.class, d, properties);
	}
	// there are more terms, we have to construct list
	else {
		TermList list = tf.createList();
		TextDecoration dec = null;
		for (Term<?> term : d.asList()) {
			if (term instanceof TermIdent
					&& (dec = Decoder.genericPropertyRaw(TextDecoration.class,
							availableDecorations, (TermIdent) term)) != null) {
				// construct term with value of parsed decoration
				list.add(tf.createTerm(dec));
			} else
				return false;
		}
		if (!list.isEmpty()) {
			properties.put("text-decoration", TextDecoration.list_values);
			values.put("text-decoration", list);
			return true;
		}
		return false;
	}
}
 
Example 7
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processContent(Declaration d,
		Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

	// content contains no explicit values
	if (d.size() == 1 && Decoder.genericOneIdent(Content.class, d, properties)) {
		return true;
	} else {

		// valid term idents
		final Set<String> validTermIdents = new HashSet<String>(Arrays
				.asList("open-quote", "close-quote", "no-open-quote",
						"no-close-quote"));

		TermList list = tf.createList();

		for (Term<?> t : d.asList()) {
			// one of valid terms
			if (t instanceof TermIdent
					&& validTermIdents.contains(((TermIdent) t).getValue()
							.toLowerCase()))
				list.add(t);
			else if (t instanceof TermString)
				list.add(t);
			else if (t instanceof TermURI)
				list.add(t);
			else if (t instanceof TermFunction.CounterFunction || t instanceof TermFunction.Attr)
				list.add(t);
			else
				return false;
		}
		// there is nothing in list after parsing
		if (list.isEmpty())
			return false;

		properties.put("content", Content.list_values);
		values.put("content", list);
		return true;
	}
}
 
Example 8
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processFilter(Declaration d,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

    // single ident: none, or global ones
    if (d.size() == 1 && Decoder.genericOneIdent(Filter.class, d, properties)) {
        return true;
    } else {
        //list of uri() or <filter-function> expected
        TermList list = tf.createList();

        for (Term<?> t : d.asList()) {
            if (t instanceof TermFunction.FilterFunction)
                list.add(t);
            else if (t instanceof TermURI)
                list.add(t);
            else
                return false;
        }
        // there is nothing in list after parsing
        if (list.isEmpty())
            return false;

        properties.put("filter", Filter.list_values);
        values.put("filter", list);
        return true;
    }
}
 
Example 9
Source File: DeclarationTransformerImpl.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean processBackdropFilter(Declaration d,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values) {

    // single ident: none, or global ones
    if (d.size() == 1 && Decoder.genericOneIdent(BackdropFilter.class, d, properties)) {
        return true;
    } else {
        //list of uri() or <filter-function> expected
        TermList list = tf.createList();

        for (Term<?> t : d.asList()) {
            if (t instanceof TermFunction.FilterFunction)
                list.add(t);
            else if (t instanceof TermURI)
                list.add(t);
            else
                return false;
        }
        // there is nothing in list after parsing
        if (list.isEmpty())
            return false;

        properties.put("backdrop-filter", BackdropFilter.list_values);
        values.put("backdrop-filter", list);
        return true;
    }
}
 
Example 10
Source File: Variator.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tries a single variant that may consist of a term or a list of comma-separated terms.
 * 
 * @param variant the variant to be tried
 * @param d the declaration to be processed
 * @param properties target property map
 * @param values target value map
 * @param listValue the list_values value to be used for the property value
 * @return {@code true} when parsed successfully
 */
public boolean tryListOfOneTermVariant(int variant, Declaration d,
        Map<String, CSSProperty> properties, Map<String, Term<?>> values,
        CSSProperty listValue) {

    // try inherit variant
    if (d.size() == 1 && checkInherit(variant, d.get(0), properties))
        return true;
    //scan the list
    TermList list = tf.createList();
    final Map<String, CSSProperty> p = new HashMap<>();
    final Map<String, Term<?>> v = new HashMap<>();
    boolean first = true;
    for (Term<?> t : d.asList()) {
        //terms must be separated by commas
        if ((first && t.getOperator() != null)
                || (!first && t.getOperator() != Operator.COMMA))
            return false; 
        //process the variant for a single term
        p.clear();
        v.clear();
        this.terms = new ArrayList<Term<?>>();
        this.terms.add(t);
        if (!variant(variant, new IntegerRef(0), p, v))
            return false;
        //collect the resulting term
        final CSSProperty prop = p.values().iterator().next();
        final Term<?> val = (v.values().isEmpty()) ? null : v.values().iterator().next();
        final TermPropertyValue pval = tf.createPropertyValue(prop, val);
        if (!first)
            pval.setOperator(Operator.COMMA);
        list.add(pval);
        first = false;
    }
    //store the result
    properties.put(names.get(variant), listValue);
    values.put(names.get(variant), list);
    return true;
}
 
Example 11
Source File: Variator.java    From jStyleParser with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Assigns terms from declaration
 * 
 * @param d
 *            Declaration which contains terms
 */
public void assignTermsFromDeclaration(Declaration d) {
	this.terms = d.asList();
}