All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.daisy.pipeline.braille.css.impl.BrailleCssSerializer Maven / Gradle / Ivy

The newest version!
package org.daisy.pipeline.braille.css.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import com.google.common.collect.Iterables;

import cz.vutbr.web.css.Declaration;
import cz.vutbr.web.css.Rule;
import cz.vutbr.web.css.RuleBlock;
import cz.vutbr.web.css.RulePage;
import cz.vutbr.web.css.Selector;
import cz.vutbr.web.css.Selector.Combinator;
import cz.vutbr.web.css.Term;
import cz.vutbr.web.csskit.OutputUtil;

import static org.daisy.common.stax.XMLStreamWriterHelper.writeAttribute;
import static org.daisy.common.stax.XMLStreamWriterHelper.writeStartElement;

import org.daisy.braille.css.InlineStyle;
import org.daisy.braille.css.InlineStyle.RuleMainBlock;
import org.daisy.braille.css.InlineStyle.RuleRelativeBlock;
import org.daisy.braille.css.LanguageRange;
import org.daisy.braille.css.PropertyValue;
import org.daisy.braille.css.SimpleInlineStyle;
import org.daisy.pipeline.css.CssSerializer;

public final class BrailleCssSerializer {

	private BrailleCssSerializer() {}

	/* =================================================== */
	/* toString                                            */
	/* =================================================== */

	public static String toString(Term term) {
		return CssSerializer.toString(term);
	}

	public static String toString(Declaration declaration) {
		return declaration.getProperty() + ": " + serializeTermList((List>)declaration) + ";";
	}

	public static String toString(BrailleCssTreeBuilder.Style style) {
		return toString(style, null);
	}

	private static String toString(BrailleCssTreeBuilder.Style style, String base) {
		StringBuilder b = new StringBuilder();
		StringBuilder rel = new StringBuilder();
		if (style.declarations != null)
			b.append(serializeDeclarationList(style.declarations));
		if (style.nestedStyles != null)
			for (Map.Entry e : style.nestedStyles.entrySet()) {
				if (base != null && e.getKey().startsWith("&")) {
					if (rel.length() > 0) rel.append(" ");
					rel.append(toString(e.getValue(), base + e.getKey().substring(1)));
				} else {
					if (b.length() > 0) b.append(" ");
					b.append(toString(e.getValue(), e.getKey()));
				}
			}
		if (base != null && b.length() > 0) {
			b.insert(0, base + " { ");
			b.append(" }");
		}
		if (rel.length() > 0) {
			if (b.length() > 0) b.append(" ");
			b.append(rel);
		}
		return b.toString();
	}

	public static String toString(InlineStyle style) {
		return toString(BrailleCssTreeBuilder.Style.of(style));
	}

	public static String toString(SimpleInlineStyle style) {
		List declarations = new ArrayList<>();
		for (String p : style.getPropertyNames())
			declarations.add(p + ": " + serializePropertyValue(style.get(p)));
		Collections.sort(declarations);
		StringBuilder s = new StringBuilder();
		Iterator it = declarations.iterator();
		while (it.hasNext()) {
			s.append(it.next());
			if (it.hasNext()) s.append("; ");
		}
		return s.toString();
	}

	public static String serializePropertyValue(PropertyValue propValue) {
		Term value = propValue.getValue();
		if (value != null)
			return toString(value);
		else
			return propValue.getProperty().toString();
	}

	public static String toString(RuleMainBlock rule) {
		return serializeDeclarationList(rule);
	}

	public static String toString(RuleRelativeBlock rule) {
		StringBuilder b = new StringBuilder();
		boolean first = true;
		for (Selector s : rule.getSelector()) {
			Combinator c = s.getCombinator();
			if (first) {
				if (c == null)
					b.append("&");
				else if (c != Combinator.CHILD)
					b.append(c.value());
				first = false;
			} else if (c != null) // should always be true
				b.append(c.value());
			b = OutputUtil.appendList(b, s, OutputUtil.EMPTY_DELIM);
		}
		b.append(" { ");
		b.append(toString((RuleBlock>)rule));
		b.append(" }");
		return b.toString();
	}

	public static String toString(RuleBlock> ruleBlock) {
		StringBuilder b = new StringBuilder();
		b.append(serializeDeclarationList(Iterables.filter(ruleBlock, Declaration.class)));
		for (Rule r : ruleBlock)
			if (r instanceof Declaration);
			else if (r instanceof RulePage)
				b.append(toString((RulePage)r));
			else
				throw new RuntimeException("not implemented");
		return b.toString();
	}

	public static String toString(RulePage page) {
		return toString(
			new BrailleCssTreeBuilder.Style().add(
				"@page",
				BrailleCssTreeBuilder.Style.of(page)));
	}

	public static String serializeRuleBlockList(Iterable>> ruleBlocks) {
		String b = null;
		for (RuleBlock> r : ruleBlocks) {
			String s;
			if (r instanceof RuleMainBlock)
				s = BrailleCssSerializer.toString((RuleMainBlock)r);
			else if (r instanceof RuleRelativeBlock)
				s = BrailleCssSerializer.toString((RuleRelativeBlock)r);
			else
				s = BrailleCssSerializer.toString(r);
			if (!s.isEmpty())
				if (b == null)
					b = s;
				else {
					if (!(b.endsWith("}") || b.endsWith(";")))
						b = b + ";";
					b += " ";
					b += s; }}
		if (b == null) b = "";
		return b;
	}

	public static String serializeTermList(List> termList) {
		return CssSerializer.serializeTermList(termList);
	}

	public static String serializeLanguageRanges(List languageRanges) {
		return OutputUtil.appendList(new StringBuilder(), languageRanges, OutputUtil.SELECTOR_DELIM).toString();
	}

	/* = PRIVATE ========================================= */

	private static String serializeDeclarationList(Iterable declarations) {
		List sortedDeclarations = new ArrayList();
		for (Declaration d : declarations) sortedDeclarations.add(d);
		Collections.sort(sortedDeclarations);
		StringBuilder s = new StringBuilder();
		Iterator it = sortedDeclarations.iterator();
		while (it.hasNext()) {
			s.append(BrailleCssSerializer.toString(it.next()));
			if (it.hasNext()) s.append(" ");
		}
		return s.toString();
	}

	/* =================================================== */
	/* toXml                                               */
	/* =================================================== */

	private static final String XMLNS_CSS = "http://www.daisy.org/ns/pipeline/braille-css";
	private static final QName CSS_RULE = new QName(XMLNS_CSS, "rule", "css");
	private static final QName CSS_PROPERTY = new QName(XMLNS_CSS, "property", "css");
	private static final QName SELECTOR = new QName("selector");
	private static final QName STYLE = new QName("style");
	private static final QName NAME = new QName("name");
	private static final QName VALUE = new QName("value");

	public static void toXml(BrailleCssTreeBuilder.Style style, XMLStreamWriter writer, boolean deep) throws XMLStreamException {
		toXml(style, writer, deep, false);
	}

	private static void toXml(BrailleCssTreeBuilder.Style style,
	                          XMLStreamWriter w,
	                          boolean deep,
	                          boolean recursive) throws XMLStreamException {
		if (style.declarations != null) {
			if (!deep || !recursive || style.nestedStyles != null)
				writeStartElement(w, CSS_RULE);
			if (!deep)
				writeAttribute(w, STYLE, serializeDeclarationList(style.declarations));
			if (deep) {
				for (Declaration d : style.declarations) {
					writeStartElement(w, CSS_PROPERTY);
					writeAttribute(w, NAME, d.getProperty());
					writeAttribute(w, VALUE, serializeTermList((List>)d));
					w.writeEndElement();
				}
			}
			if (!deep || !recursive || style.nestedStyles != null)
				w.writeEndElement();
		}
		if (style.nestedStyles != null)
			for (Map.Entry e : style.nestedStyles.entrySet()) {
				writeStartElement(w, CSS_RULE);
				writeAttribute(w, SELECTOR, e.getKey());
				if (!deep)
					writeAttribute(w, STYLE, e.getValue().toString());
				if (deep)
					toXml(e.getValue(), w, true, true);
				w.writeEndElement();
			}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy