CssValueUtil { public static List<String> split(final String cssValue) { try { final Map<Integer, int[]> startAndEndIndexes = new TreeMap<>(); for (final Entry<String, String> entry : CSS_VALUE_PART_START_END_VALUES .entrySet()) { final int[][] rgbStartAndEndIndexesOf = StringUtil .startAndEndIndexesOf(cssValue, entry.getKey(), entry.getValue()); for (final int[] each : rgbStartAndEndIndexesOf) { startAndEndIndexes.put(each[0], each); } } if (startAndEndIndexes.size() == 0) { return Arrays.asList(StringUtil.splitBySpace( StringUtil.convertToSingleSpace(cssValue))); } final List<String> cssValueParts = new ArrayList<>(); int count = 0; final int startAndEndIndexesSize = startAndEndIndexes.size(); int[] previousStartEndIndex = null; for (final Entry<Integer, int[]> entry : startAndEndIndexes .entrySet()) { count++; final int[] currentStartEndIndex = entry.getValue(); final int zerothSlotCurrent = currentStartEndIndex[0]; final int firstSlotCurrent = currentStartEndIndex[1]; if (previousStartEndIndex != null) { final int firstSlotOfPrevious = previousStartEndIndex[1]; if ((zerothSlotCurrent - firstSlotOfPrevious) > 2) { final String cssValuePart2 = cssValue.substring( firstSlotOfPrevious + 1, zerothSlotCurrent); final String trimmedCssValuePart2 = StringUtil .strip(cssValuePart2); if (StringUtil.startsWithWhitespace(cssValuePart2) && StringUtil .endsWithWhitespace(cssValuePart2)) { if (!trimmedCssValuePart2.isEmpty()) { for (final String each : StringUtil .splitBySpace(trimmedCssValuePart2)) { final String trimmed; if (!(trimmed = StringUtil.strip(each)) .isEmpty()) { cssValueParts.add(trimmed); } } } } else { throw new InvalidValueException( "there must be a space between each part of cssValue, a space is expected before and/or after '" + cssValuePart2 + "'"); } } else if ((zerothSlotCurrent - firstSlotOfPrevious) == 1) { throw new InvalidValueException( "there must be a space between each part of cssValue, a space is expected before '" + cssValue.substring(zerothSlotCurrent) + "'"); } cssValueParts .add(cssValue.substring(currentStartEndIndex[0], currentStartEndIndex[1] + 1)); } else { if ((zerothSlotCurrent) > 1) { final String cssValuePart2 = cssValue.substring(0, zerothSlotCurrent); final String trimmedCssValuePart2 = StringUtil .strip(cssValuePart2); if (StringUtil.endsWithWhitespace(cssValuePart2)) { for (final String each : StringUtil .splitBySpace(trimmedCssValuePart2)) { final String trimmed; if (!(trimmed = StringUtil.strip(each)) .isEmpty()) { cssValueParts.add(trimmed); } } } else { throw new InvalidValueException( "there must be a space between each part of cssValue, a space is expected after '" + cssValuePart2 + "'"); } } cssValueParts .add(cssValue.substring(currentStartEndIndex[0], currentStartEndIndex[1] + 1)); } final int cssValueLength = cssValue.length(); if (count == startAndEndIndexesSize && (firstSlotCurrent < (cssValueLength - 1))) { final String cssValuePart2 = cssValue .substring(firstSlotCurrent + 1); final String trimmedCssValuePart2 = StringUtil .strip(cssValuePart2); if (StringUtil.startsWithWhitespace(cssValuePart2)) { for (final String each : StringUtil .splitBySpace(trimmedCssValuePart2)) { if (!each.isEmpty()) { cssValueParts.add(each); } } } else { throw new InvalidValueException( "there must be a space between each part of cssValue, a space is expected before '" + cssValuePart2 + "'"); } } previousStartEndIndex = currentStartEndIndex; } return cssValueParts; } catch (final RuntimeException e) { throw new InvalidValueException(e); } } private  CssValueUtil(); void addCssValuePartStartEndValue(final String start,
            final String end); static List<String> split(final String cssValue); static void throwExceptionForInvalidValue(final String cssValue);  }
@Test public void testSplit1() { final List<String> cssValueParts = CssValueUtil.split("hsla(7, 8, 9, 1) red rgb(3, 5, 6) rgb(7, 8, 9) rgba(7, 8, 9, 1) middle hsl(10, 11, 12) green blue"); assertEquals("hsla(7, 8, 9, 1)", cssValueParts.get(0)); assertEquals("red", cssValueParts.get(1)); assertEquals("rgb(3, 5, 6)", cssValueParts.get(2)); assertEquals("rgb(7, 8, 9)", cssValueParts.get(3)); assertEquals("rgba(7, 8, 9, 1)", cssValueParts.get(4)); assertEquals("middle", cssValueParts.get(5)); assertEquals("hsl(10, 11, 12)", cssValueParts.get(6)); assertEquals("green", cssValueParts.get(7)); assertEquals("blue", cssValueParts.get(8)); }
@Test public void testSplit2() { final List<String> cssValueParts = CssValueUtil.split("white red rgb(3, 5, 6) rgb(7, 8, 9) rgba(7, 8, 9, 1) middle hsl(10, 11, 12) green blue"); assertEquals("white", cssValueParts.get(0)); assertEquals("red", cssValueParts.get(1)); assertEquals("rgb(3, 5, 6)", cssValueParts.get(2)); assertEquals("rgb(7, 8, 9)", cssValueParts.get(3)); assertEquals("rgba(7, 8, 9, 1)", cssValueParts.get(4)); assertEquals("middle", cssValueParts.get(5)); assertEquals("hsl(10, 11, 12)", cssValueParts.get(6)); assertEquals("green", cssValueParts.get(7)); assertEquals("blue", cssValueParts.get(8)); }
@Test public void testSplit3() { final List<String> cssValueParts = CssValueUtil.split("white red rgb(3, 5, 6) rgb(7, 8, 9) rgba(7, 8, 9, 1) middle blue green hsl(10, 11, 12)"); assertEquals("white", cssValueParts.get(0)); assertEquals("red", cssValueParts.get(1)); assertEquals("rgb(3, 5, 6)", cssValueParts.get(2)); assertEquals("rgb(7, 8, 9)", cssValueParts.get(3)); assertEquals("rgba(7, 8, 9, 1)", cssValueParts.get(4)); assertEquals("middle", cssValueParts.get(5)); assertEquals("blue", cssValueParts.get(6)); assertEquals("green", cssValueParts.get(7)); assertEquals("hsl(10, 11, 12)", cssValueParts.get(8)); }
@Test public void testSplit4() { final List<String> cssValueParts = CssValueUtil.split("55px solid green"); assertEquals("55px", cssValueParts.get(0)); assertEquals("solid", cssValueParts.get(1)); assertEquals("green", cssValueParts.get(2)); }
@Test public void testSplit5() { final List<String> cssValueParts = CssValueUtil.split("55px solid green"); assertEquals("55px", cssValueParts.get(0)); assertEquals("solid", cssValueParts.get(1)); assertEquals("green", cssValueParts.get(2)); }
@Test(expected = InvalidValueException.class) public void testSplitForInvalidValue1() { CssValueUtil.split("hsla(7, 8, 9, 1)rgb(3, 5, 6)"); }
@Test(expected = InvalidValueException.class) public void testSplitForInvalidValue2() { CssValueUtil.split("hsla(7, 8, 9, 1)red"); }
@Test(expected = InvalidValueException.class) public void testSplitForInvalidValue3() { CssValueUtil.split("hsla(7, 8, 9, 1) redrgb(3, 5, 6) "); }
@Test(expected = InvalidValueException.class) public void testSplitForInvalidValue4() { CssValueUtil.split("hsla(7, 8, 9, 1)red rgb(3, 5, 6) "); }
@Test(expected = InvalidValueException.class) public void testSplitForInvalidValue5() { CssValueUtil.split("redhsla(7, 8, 9, 1)"); }