Skip to content

Commit cd0c88e

Browse files
committed
refactor: add util to flatten a CSS composed construct
1 parent 0a452a0 commit cd0c88e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main/java/org/idpf/epubcheck/util/css/CssGrammar.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@
3939
import static org.idpf.epubcheck.util.css.CssToken.Matchers.MATCH_STAR_PIPE;
4040
import static org.idpf.epubcheck.util.css.CssTokenList.Filters.FILTER_NONE;
4141

42+
import java.util.Arrays;
4243
import java.util.List;
4344
import java.util.Locale;
4445
import java.util.Map;
46+
import java.util.stream.Collectors;
4547

4648
import org.idpf.epubcheck.util.css.CssExceptions.CssErrorCode;
4749
import org.idpf.epubcheck.util.css.CssExceptions.CssException;
4850
import org.idpf.epubcheck.util.css.CssExceptions.CssGrammarException;
51+
import org.idpf.epubcheck.util.css.CssGrammar.CssComposedConstruct;
52+
import org.idpf.epubcheck.util.css.CssGrammar.CssConstruct;
4953
import org.idpf.epubcheck.util.css.CssParser.ContextRestrictions;
5054
import org.idpf.epubcheck.util.css.CssTokenList.CssTokenIterator;
5155

@@ -66,6 +70,23 @@
6670
public class CssGrammar
6771
{
6872

73+
/**
74+
* Utility methodd to flatten a nested construct in a list
75+
* of its atomic components
76+
*/
77+
public static List<CssConstruct> flatten(CssConstruct construct)
78+
{
79+
if (construct instanceof CssComposedConstruct)
80+
{
81+
return ((CssComposedConstruct) construct).getComponents().stream()
82+
.flatMap(c -> flatten(c).stream()).collect(Collectors.toList());
83+
}
84+
else
85+
{
86+
return Arrays.asList(construct);
87+
}
88+
}
89+
6990
/**
7091
* Abstract base for all CssConstructs.
7192
*/

0 commit comments

Comments
 (0)