protected void doOutputImage(ImageField image) { if (image == null || image.getImageURL() == null) return; outputCellStart(image); ImageIcon icon = image.getImageIcon(); // For width and height String url = image.getImageURL().toString(); if (url.startsWith("file:")) url = url.substring(5); // Make an alt attribute from the URL String alt = url; int pos = alt.lastIndexOf("/"); if (pos != -1) alt = alt.substring(pos + 1); out.print( "<img src=\"" + StringUtils.escapeHTML(url) + "\" alt=\"" + StringUtils.escapeHTML(alt) + "\" width=\"" + icon.getIconWidth() + "\" height=\"" + icon.getIconHeight() + "\">"); outputCellEnd(); }
public void testEscapeXML() { assertEquals("", StringUtils.escapeXML(null)); assertEquals("", StringUtils.escapeXML("")); assertEquals("abc&def", StringUtils.escapeXML("abc&def")); assertEquals("abc"def'", StringUtils.escapeXML("abc\"def'")); assertEquals("<>", StringUtils.escapeXML("<>")); }
protected void doOutputField(Field field) { Format format = outputCellStart(field); // Style attribute courtesy of Brendon Price <*****@*****.**> out.print("<span style=\""); if (format.getFontFamilyName() != null) out.print("font-family: " + format.getFontFamilyName() + "; "); out.print("font-size: " + format.getSize() + "pt; "); outputColor(format.getColor()); // Border code courtesy of Khadiyd Idris <*****@*****.**> Border b = field.getBorderOrDefault(); String bcolor = "black"; if (b.getColor() != null) { bcolor = "#" + Integer.toHexString(b.getColor().getRed()) + Integer.toHexString(b.getColor().getGreen()) + Integer.toHexString(b.getColor().getBlue()); } if (b.getTop() != null) out.print("border-top: solid " + bcolor + " " + b.getTop().getThickness() + "pt; "); if (b.getLeft() != null) out.print("border-left: solid " + bcolor + " " + b.getLeft().getThickness() + "pt; "); if (b.getBottom() != null) out.print("border-bottom: solid " + bcolor + " " + b.getBottom().getThickness() + "pt; "); if (b.getRight() != null) out.print("border-right: solid " + bcolor + " " + b.getRight().getThickness() + "pt; "); out.print("\">"); if (format.isBold()) out.print("<b>"); if (format.isItalic()) out.print("<i>"); if (format.isUnderline()) out.print("<u>"); String str = field.toString(); if (str == null || str.length() == 0) str = " "; // Fix courtesy of Brendon Price <*****@*****.**> if (" ".equals(str)) out.print(str); else out.print(StringUtils.newlinesToXHTMLBreaks(StringUtils.escapeHTML(str))); if (format.isUnderline()) out.print("</u>"); if (format.isItalic()) out.print("</i>"); if (format.isBold()) out.print("</b>"); out.print("</span>"); outputCellEnd(); }
public void testNewlines() { assertEquals("", StringUtils.newlinesToXHTMLBreaks(null)); assertEquals("", StringUtils.newlinesToXHTMLBreaks("")); assertEquals("\n<br />", StringUtils.newlinesToXHTMLBreaks("\n")); assertEquals("\n<br />", StringUtils.newlinesToXHTMLBreaks("\r")); assertEquals("\n<br />", StringUtils.newlinesToXHTMLBreaks("\r\n")); assertEquals("hello\n<br />world", StringUtils.newlinesToXHTMLBreaks("hello\nworld")); assertEquals("hello\n<br />world", StringUtils.newlinesToXHTMLBreaks("hello\rworld")); assertEquals("hello\n<br />world", StringUtils.newlinesToXHTMLBreaks("hello\r\nworld")); }
public void testJoin() { assertEquals("", StringUtils.join(null, null)); assertEquals("", StringUtils.join(null, ",")); ArrayList<String> list = new ArrayList<String>(); assertEquals("", StringUtils.join(list, null)); assertEquals("", StringUtils.join(list, ",")); list.add("a"); assertEquals("a", StringUtils.join(list, null)); assertEquals("a", StringUtils.join(list, ",")); list.add("xyzzy"); assertEquals("axyzzy", StringUtils.join(list, null)); assertEquals("a,xyzzy", StringUtils.join(list, ",")); assertEquals("a-*-xyzzy", StringUtils.join(list, "-*-")); }
public void testSplitUp() { StringBuilder buf = new StringBuilder(); StringUtils.splitUp(buf, null); assertEquals("", buf.toString()); buf = new StringBuilder(); StringUtils.splitUp(buf, "abcde abcde abcde abcde", 12); assertEquals("abcde abcde\nabcde abcde", buf.toString()); buf = new StringBuilder(); StringUtils.splitUp(buf, "abcdeabcdeabcdeabcde", 12); assertEquals("abcdeabcdeabcdeabcde", buf.toString()); buf = new StringBuilder(); StringUtils.splitUp(buf, " abcde", 12); assertEquals("abcde", buf.toString()); buf = new StringBuilder(); StringUtils.splitUp(buf, " abcde abcde abcde abcde ", 12); assertEquals("abcde\nabcde\nabcde\nabcde", buf.toString()); }
public void testAvoidInfiniteLoop() { final int[] count = new int[1]; count[0] = 0; Replacer r = new Replacer() { public Object replace(String s) { if (++count[0] == 2) fail("caught in an infinite loop"); return "{" + s + "}"; } }; assertEquals("a{}b", StringUtils.replaceDelimited(null, "{", "}", r, "a{}b")); }
public void testReplace() { Replacer r = new Replacer() { public Object replace(String s) { return "x"; } }; assertNull(StringUtils.replaceDelimited("", "", null, null)); assertNull(StringUtils.replaceDelimited(null, null, null, null)); assertEquals("abcde", StringUtils.replaceDelimited(null, null, r, "abcde")); assertEquals("abcde", StringUtils.replaceDelimited("", "", r, "abcde")); assertEquals("", StringUtils.replaceDelimited("{%", "}", r, "")); assertEquals("abxde", StringUtils.replaceDelimited("{%", "}", r, "ab{%c}de")); assertEquals("xabxdex", StringUtils.replaceDelimited("{%", "}", r, "{%}ab{%c}de{% }")); assertEquals("abcd{%e", StringUtils.replaceDelimited("{%", "}", r, "abcd{%e")); assertEquals( "x.nil? ? nil : x / 100.0", StringUtils.replaceDelimited( "{", "}", r, "{jobs.hourly rate}.nil? ? nil : {jobs.hourly rate} / 100.0")); }
public void testSplit() { ArrayList<String> typicalAnswer = new ArrayList<String>(); typicalAnswer.add("a"); typicalAnswer.add("b"); typicalAnswer.add("c"); ArrayList<String> origStringAnswer = new ArrayList<String>(); origStringAnswer.add("abc"); // Special cases assertNull(StringUtils.split(null, null)); assertEquals(origStringAnswer, StringUtils.split("abc", null)); assertEquals(origStringAnswer, StringUtils.split("abc", "x")); assertEquals(typicalAnswer, StringUtils.split("a.b.c", ".")); assertEquals(typicalAnswer, StringUtils.split("a**&*b**&*c", "**&*")); ArrayList<String> answer = new ArrayList<String>(typicalAnswer); answer.add(0, ""); assertEquals(answer, StringUtils.split(".a.b.c", ".")); assertEquals(answer, StringUtils.split("**&*a**&*b**&*c", "**&*")); answer = new ArrayList<String>(typicalAnswer); answer.add(1, ""); assertEquals(answer, StringUtils.split("a..b.c", ".")); assertEquals(answer, StringUtils.split("a**&***&*b**&*c", "**&*")); answer = new ArrayList<String>(typicalAnswer); answer.add(""); assertEquals(answer, StringUtils.split("a.b.c.", ".")); assertEquals(answer, StringUtils.split("a**&*b**&*c**&*", "**&*")); answer = new ArrayList<String>(typicalAnswer); answer.add(1, ""); answer.add(""); assertEquals(answer, StringUtils.split("a..b.c.", ".")); assertEquals(answer, StringUtils.split("a**&***&*b**&*c**&*", "**&*")); }
public void testLineSplit() { ArrayList<String> answer = new ArrayList<String>(); answer.add(""); List<String> split = StringUtils.splitIntoLines(""); assertEquals(answer.size(), split.size()); assertEquals(answer, split); // The three flavors of line endings String[] endings = {"\n", "\r", "\r\n"}; // We want to see an array with one empty string (the text before the // newline). There is no string after the newline. for (int i = 0; i < endings.length; ++i) { split = StringUtils.splitIntoLines(endings[i]); assertEquals(answer.size(), split.size()); assertEquals(answer, split); } answer.add("x"); for (int i = 0; i < endings.length; ++i) { split = StringUtils.splitIntoLines(endings[i] + "x"); assertEquals(answer.size(), split.size()); assertEquals(answer, split); } answer = new ArrayList<String>(); answer.add("line one"); answer.add("part deux"); answer.add("three's a crowd, eh?"); answer.add(""); split = StringUtils.splitIntoLines("line one\npart deux\nthree's a crowd, eh?" + "\n\n"); assertEquals(answer.size(), split.size()); assertEquals(answer, split); // Test two newlines in a row answer = new ArrayList<String>(); answer.add("two newlines after this line"); answer.add(""); answer.add("part deux"); answer.add("three's a crowd, eh?"); answer.add(""); split = StringUtils.splitIntoLines( "two newlines after this line\n\npart deux\nthree's a crowd, eh?" + "\n\n"); assertEquals(answer.size(), split.size()); assertEquals(answer, split); split = StringUtils.splitIntoLines( "two newlines after this line\r\n\r\npart deux\r\nthree's a crowd, eh?" + "\n\n"); assertEquals(answer.size(), split.size()); assertEquals(answer, split); split = StringUtils.splitIntoLines( "two newlines after this line\r\rpart deux\rthree's a crowd, eh?" + "\r\r"); assertEquals(answer.size(), split.size()); assertEquals(answer, split); }
public void testReplaceNotAfter() { Replacer r = new Replacer() { public Object replace(String s) { return "x"; } }; assertNull(StringUtils.replaceDelimited("#", "", "", null, null)); assertNull(StringUtils.replaceDelimited("#", "#", "#", null, null)); assertNull(StringUtils.replaceDelimited("#", null, null, null, null)); assertEquals("abcde", StringUtils.replaceDelimited("#", null, null, r, "abcde")); assertEquals("abcde", StringUtils.replaceDelimited("#", "", "", r, "abcde")); assertEquals("", StringUtils.replaceDelimited("#", "{%", "}", r, "")); assertEquals("abxde", StringUtils.replaceDelimited("#", "{%", "}", r, "ab{%c}de")); assertEquals("xabxdex", StringUtils.replaceDelimited("#", "{%", "}", r, "{%}ab{%c}de{% }")); assertEquals("abcd{%e", StringUtils.replaceDelimited("#", "{%", "}", r, "abcd{%e")); assertEquals("ab#{c}de", StringUtils.replaceDelimited("#", "{", "}", r, "ab#{c}de")); assertEquals("ab##{c}de", StringUtils.replaceDelimited("#", "{", "}", r, "ab##{c}de")); assertEquals("ab##{x}dex", StringUtils.replaceDelimited("#", "{", "}", r, "ab##{{c}}de{z}")); assertEquals("ab#{x}de", StringUtils.replaceDelimited("#", "{", "}", r, "ab#{{c}}de")); assertEquals("ab#{x}de", StringUtils.replaceDelimited("#", "{%", "}", r, "ab#{{%c}}de")); }