XsltHelper { public static String split(String text, int maxChars, int line, boolean isLastLine) { if (text == null || line < 1) { return null; } if ((maxChars > text.length() && line == 1) || maxChars == 0) { return text; } String textWithSeparator = WordUtils.wrap(text, maxChars, LINE_SEPARATOR, true); String[] lines = textWithSeparator.split(LINE_SEPARATOR); if (lines.length >= line) { if (isLastLine && (lines.length > line) && (maxChars > 3)) { String lastLine = lines[line - 1] + "..."; return StringUtils.abbreviate(lastLine, maxChars); } else { return lines[line - 1]; } } else { return null; } } private  XsltHelper(); static String split(String text, int maxChars, int line, boolean isLastLine);  }
@Test public void testSplit() { assertEquals(null, XsltHelper.split(null, 50, 1, false)); assertEquals(TEXT1, XsltHelper.split(TEXT1, 0, 1, false)); assertEquals(null, XsltHelper.split(TEXT1, 50, 0, false)); assertEquals(SHORT_TEXT, XsltHelper.split(SHORT_TEXT, 50, 1, false)); assertEquals(null, XsltHelper.split(SHORT_TEXT, 50, 2, false)); assertEquals("Multiscale Climatic, Topographic, and Biotic", XsltHelper.split(TEXT1, 50, 1, false)); assertEquals("Controls of Tree Invasion in a Sub-Alpine Parkland", XsltHelper.split(TEXT1, 50, 2, false)); assertEquals("Landscape, Jefferson Park, Oregon Cascades, USA", XsltHelper.split(TEXT1, 50, 3, false)); assertEquals("The Ancient Ancestors of these Indians here:", XsltHelper.split(TEXT2, 44, 1, false)); assertEquals("The Ancient Ancestors of these Indians", XsltHelper.split(TEXT2, 43, 1, false)); assertEquals("Dies ist ein", XsltHelper.split(TEXT3, 15, 1, false)); assertEquals("\"Klassiker\" zur", XsltHelper.split(TEXT3, 15, 2, false)); assertEquals("Dies ist ein", XsltHelper.split(TEXT3, 23, 1, false)); assertEquals("\"Klassiker\" zur", XsltHelper.split(TEXT3, 23, 2, false)); assertEquals("Dies ist ein \"Klassiker\"", XsltHelper.split(TEXT3, 24, 1, false)); assertEquals("zur Gesprächsführung:", XsltHelper.split(TEXT3, 24, 2, false)); assertEquals("Angehrn Tobias, Biedermann", XsltHelper.split(TEXT4, 31, 1, false)); assertEquals("Angehrn Tobias, Biedermann Anja,", XsltHelper.split(TEXT4, 32, 1, false)); }
@Test public void testSplitAbbreviate() { assertEquals(SHORT_TEXT, XsltHelper.split(SHORT_TEXT, 13, 1, true)); assertEquals("A short...", XsltHelper.split(SHORT_TEXT, 12, 1, true)); assertEquals("A short...", XsltHelper.split(SHORT_TEXT, 11, 1, true)); assertEquals("A short...", XsltHelper.split(SHORT_TEXT, 10, 1, true)); assertEquals("A shor...", XsltHelper.split(SHORT_TEXT, 9, 1, true)); assertEquals("A sho...", XsltHelper.split(SHORT_TEXT, 8, 1, true)); assertEquals("A sh...", XsltHelper.split(SHORT_TEXT, 7, 1, true)); assertEquals("A...", XsltHelper.split(SHORT_TEXT, 6, 1, true)); assertEquals("A...", XsltHelper.split(SHORT_TEXT, 5, 1, true)); assertEquals("A...", XsltHelper.split(SHORT_TEXT, 4, 1, true)); assertEquals("A", XsltHelper.split(SHORT_TEXT, 3, 1, true)); assertEquals("A", XsltHelper.split(SHORT_TEXT, 2, 1, true)); assertEquals("A", XsltHelper.split(SHORT_TEXT, 1, 1, true)); assertEquals(SHORT_TEXT, XsltHelper.split(SHORT_TEXT, 0, 1, true)); }