You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2007/05/07 16:56:06 UTC
svn commit: r535887 - in /myfaces/tobago/trunk/core/src:
main/java/org/apache/myfaces/tobago/renderkit/html/
main/java/org/apache/myfaces/tobago/util/
main/java/org/apache/myfaces/tobago/webapp/
test/java/org/apache/myfaces/tobago/util/ test/java/org/a...
Author: lofwyr
Date: Mon May 7 07:56:03 2007
New Revision: 535887
URL: http://svn.apache.org/viewvc?view=rev&rev=535887
Log:
TOBAGO-388: close start tag in any write() methods of Writer
TOBAGO-389: Simplify code: TobagoResponseRenderer
Added:
myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/ResponseWriterBuffer.java
Modified:
myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java
myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/HtmlWriterUtil.java
myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java
myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/util/HtmlWriterUtilUnitTest.java
myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java
Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java?view=diff&rev=535887&r1=535886&r2=535887
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java Mon May 7 07:56:03 2007
@@ -53,6 +53,9 @@
public static final String SCRIPT = "script";
public static final String META = "meta";
public static final String OPTGROUP = "optgroup";
+ public static final String AREA = "area";
+ public static final String PARAM = "param";
+ public static final String BASE = "base";
private HtmlConstants() {
Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/HtmlWriterUtil.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/HtmlWriterUtil.java?view=diff&rev=535887&r1=535886&r2=535887
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/HtmlWriterUtil.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/HtmlWriterUtil.java Mon May 7 07:56:03 2007
@@ -17,9 +17,6 @@
* limitations under the License.
*/
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
import java.io.IOException;
import java.io.Writer;
@@ -30,37 +27,35 @@
*/
public final class HtmlWriterUtil {
- private static final Log LOG = LogFactory.getLog(HtmlWriterUtil.class);
+ private static final char[][] CHARS_TO_ESCAPE;
+
+ static {
+ // init lookup table
+ CHARS_TO_ESCAPE = new char[0xA0][];
+ CHARS_TO_ESCAPE['"'] = """.toCharArray();
+ CHARS_TO_ESCAPE['&'] = "&".toCharArray();
+ CHARS_TO_ESCAPE['<'] = "<".toCharArray();
+ CHARS_TO_ESCAPE['>'] = ">".toCharArray();
+ }
- private static final int BUFFER_SIZE = 1028;
private final Writer out;
- private final boolean utf8;
- private final char[][] charsToEscape;
- private final char[] buff;
- private int bufferIndex;
+ private final ResponseWriterBuffer buffer;
- public HtmlWriterUtil(Writer out, String characterEncoding, boolean attribute) {
+ private final boolean utf8;
+
+ public HtmlWriterUtil(Writer out, String characterEncoding) {
this.out = out;
utf8 = "utf-8".equalsIgnoreCase(characterEncoding);
- charsToEscape = attribute ? ATTRIBUTE_CHARS_TO_ESCAPE : TEXT_CHARS_TO_ESCAPE;
- buff = new char[BUFFER_SIZE];
-// LOG.info("utf8 = " + utf8);
-
+ buffer = new ResponseWriterBuffer(out);
}
-
public void writeAttributeValue(final String text)
throws IOException {
writeAttributeValue(text.toCharArray(), 0, text.length());
}
- public void writeAttributeValue(final char[] text)
- throws IOException {
- writeEncodedValue(text, 0, text.length, true);
- }
-
- public void writeAttributeValue(
+ private void writeAttributeValue(
final char[] text, final int start, final int length)
throws IOException {
writeEncodedValue(text, start, length, true);
@@ -71,46 +66,21 @@
writeText(text.toCharArray(), 0, text.length());
}
- public void writeText(final char[] text) throws IOException {
- writeEncodedValue(text, 0, text.length, false);
- }
-
public void writeText(final char[] text, final int start, final int length)
throws IOException {
writeEncodedValue(text, start, length, false);
}
-// static public void writeText(Writer out, char[] buffer, char[] text)
-// throws IOException {
-// writeText(out, buffer, text, 0, text.length);
-// }
-//
-// public static void writeText(
-// Writer out, char[] buff, char[] text, int start, int length)
-// throws IOException {
-// writeEncodedValue(out, buff, text, start, length, false);
-// }
-
-
private void writeEncodedValue(final char[] text, final int start,
final int length, final boolean isAttribute)
throws IOException {
-// final char[][] charsToEscape;
-// if (isAttribute) {
-// charsToEscape = ATTRIBUTE_CHARS_TO_ESCAPE;
-// } else {
-// charsToEscape = TEXT_CHARS_TO_ESCAPE;
-// }
-
-
int localIndex = -1;
-
final int end = start + length;
for (int i = start; i < end; i++) {
char ch = text[i];
- if (ch >= charsToEscape.length - 1 || charsToEscape[ch] != null) {
+ if (ch >= CHARS_TO_ESCAPE.length || CHARS_TO_ESCAPE[ch] != null) {
localIndex = i;
break;
}
@@ -127,23 +97,23 @@
final char ch = text[i];
// Tilde or less...
- if (ch < charsToEscape.length - 1) {
+ if (ch < CHARS_TO_ESCAPE.length) {
if (isAttribute && ch == '&' && (i + 1 < end) && text[i + 1] == '{') {
// HTML 4.0, section B.7.1: ampersands followed by
// an open brace don't get escaped
- addToBuffer('&');
- } else if (charsToEscape[ch] != null) {
- for (char cha : charsToEscape[ch]) {
- addToBuffer(cha);
+ buffer.addToBuffer('&');
+ } else if (CHARS_TO_ESCAPE[ch] != null) {
+ for (char cha : CHARS_TO_ESCAPE[ch]) {
+ buffer.addToBuffer(cha);
}
} else {
- addToBuffer(ch);
+ buffer.addToBuffer(ch);
}
} else if (utf8) {
- addToBuffer(ch);
+ buffer.addToBuffer(ch);
} else if (ch <= 0xff) {
// ISO-8859-1 entities: encode as needed
- flushBuffer();
+ buffer.flushBuffer();
out.write('&');
// FIXME? write(String) sets the startStillOpen=false
@@ -153,7 +123,7 @@
}
out.write(';');
} else {
- flushBuffer();
+ buffer.flushBuffer();
// Double-byte characters to encode.
// PENDING: when outputting to an encoding that
@@ -163,7 +133,7 @@
}
}
- flushBuffer();
+ buffer.flushBuffer();
}
}
@@ -212,44 +182,6 @@
out.write(';');
}
- //
- // Buffering scheme: we use a tremendously simple buffering
- // scheme that greatly reduces the number of calls into the
- // Writer/PrintWriter. In practice this has produced significant
- // measured performance gains (at least in JDK 1.3.1). We only
- // support adding single characters to the buffer, so anytime
- // multiple characters need to be written out, the entire buffer
- // gets flushed. In practice, this is good enough, and keeps
- // the core simple.
- //
-
- /**
- * Add a character to the buffer, flushing the buffer if the buffer is
- * full, and returning the new buffer index
- */
- private void addToBuffer(final char ch) throws IOException {
- if (bufferIndex >= BUFFER_SIZE) {
- out.write(buff, 0, bufferIndex);
- bufferIndex = 0;
- }
-
- buff[bufferIndex] = ch;
-
- bufferIndex += 1;
- }
-
-
- /**
- * Flush the contents of the buffer to the output stream
- * and return the reset buffer index
- */
- private void flushBuffer() throws IOException {
- if (bufferIndex > 0) {
- out.write(buff, 0, bufferIndex);
- }
- bufferIndex = 0;
- }
-
public static boolean attributeValueMustEscaped(final String name) {
// this is 30% faster then the .equals(name) version
// tested with 100 loops over 19871 names
@@ -258,18 +190,18 @@
try {
switch (name.charAt(0)) {
case 'i' : // 'id'
- if (name.charAt(1) == 'd') {
+ if (name.length() == 2 && name.charAt(1) == 'd') {
return false;
}
break;
case 'n' : // 'name'
- if (name.charAt(1) == 'a' && name.charAt(2) == 'm'
+ if (name.length() == 4 && name.charAt(1) == 'a' && name.charAt(2) == 'm'
&& name.charAt(3) == 'e') {
return false;
}
break;
case 'c' : // 'class'
- if (name.charAt(1) == 'l' && name.charAt(2) == 'a'
+ if (name.length() == 5 && name.charAt(1) == 'l' && name.charAt(2) == 'a'
&& name.charAt(3) == 's' && name.charAt(4) == 's') {
return false;
}
@@ -386,24 +318,4 @@
"thorn",
"yuml"
};
-
- private static final char[][] ATTRIBUTE_CHARS_TO_ESCAPE;
-
- private static final char[][] TEXT_CHARS_TO_ESCAPE;
-
- static {
- // init lookup arrays
- // initial values of char[][] are null see java language spec
- // '<' is not escaped in attribute values, but in text
- ATTRIBUTE_CHARS_TO_ESCAPE = new char[0xA0 + 1][];
- ATTRIBUTE_CHARS_TO_ESCAPE[0x22] = """.toCharArray(); // 0x22 '"'
- ATTRIBUTE_CHARS_TO_ESCAPE[0x26] = "&".toCharArray(); // 0x26 '&'
- ATTRIBUTE_CHARS_TO_ESCAPE[0x3E] = ">".toCharArray(); // 0x3E '>'
-
- TEXT_CHARS_TO_ESCAPE = new char[ATTRIBUTE_CHARS_TO_ESCAPE.length][];
- System.arraycopy(ATTRIBUTE_CHARS_TO_ESCAPE, 0, TEXT_CHARS_TO_ESCAPE, 0, ATTRIBUTE_CHARS_TO_ESCAPE.length);
- TEXT_CHARS_TO_ESCAPE[0x3C] = "<".toCharArray(); // 0x '<'
-
- }
-
}
Added: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/ResponseWriterBuffer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/ResponseWriterBuffer.java?view=auto&rev=535887
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/ResponseWriterBuffer.java (added)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/util/ResponseWriterBuffer.java Mon May 7 07:56:03 2007
@@ -0,0 +1,64 @@
+package org.apache.myfaces.tobago.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.IOException;
+import java.io.Writer;
+
+//
+// Buffering scheme: we use a tremendously simple buffering
+// scheme that greatly reduces the number of calls into the
+// Writer/PrintWriter. In practice this has produced significant
+// measured performance gains (at least in JDK 1.3.1). We only
+// support adding single characters to the buffer, so anytime
+// multiple characters need to be written out, the entire buffer
+// gets flushed. In practice, this is good enough, and keeps
+// the core simple.
+//
+
+/**
+ * User: lofwyr
+ * Date: 07.05.2007 12:03:26
+ */
+public class ResponseWriterBuffer {
+
+ private static final Log LOG = LogFactory.getLog(ResponseWriterBuffer.class);
+
+ private static final int BUFFER_SIZE = 64;
+
+ private final char[] buff;
+
+ private int bufferIndex;
+
+ private Writer writer;
+
+ public ResponseWriterBuffer(Writer writer) {
+ buff = new char[BUFFER_SIZE];
+ this.writer = writer;
+ }
+
+ /**
+ * Add a character to the buffer, flushing the buffer if the buffer is
+ * full, and returning the new buffer index
+ */
+ public void addToBuffer(final char ch) throws IOException {
+ if (bufferIndex >= BUFFER_SIZE) {
+ writer.write(buff, 0, bufferIndex);
+ bufferIndex = 0;
+ }
+
+ buff[bufferIndex++] = ch;
+ }
+
+ /**
+ * Flush the contents of the buffer to the output stream
+ * and return the reset buffer index
+ */
+ public void flushBuffer() throws IOException {
+ if (bufferIndex > 0) {
+ writer.write(buff, 0, bufferIndex);
+ }
+ bufferIndex = 0;
+ }
+}
Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java?view=diff&rev=535887&r1=535886&r2=535887
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriter.java Mon May 7 07:56:03 2007
@@ -25,6 +25,7 @@
import org.apache.myfaces.tobago.util.XmlUtils;
import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
+import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
import javax.faces.component.UIComponent;
import javax.faces.context.ResponseWriter;
@@ -35,24 +36,21 @@
import java.util.Set;
import java.util.Stack;
-
public class TobagoResponseWriter extends ResponseWriter {
- @Override
- public void write(String string) throws IOException {
- closeOpenTag();
- super.write(string);
- }
-
private static final Log LOG = LogFactory.getLog(TobagoResponseWriter.class);
- private static final Set<String> EMPTY_TAG
- = new HashSet<String>(
- Arrays.asList(
- new String[]{
- "br", "area", "link", "img", "param", "hr", "input", "col", "base",
- "meta"}));
-
+ private static final Set<String> EMPTY_TAG = new HashSet<String>(Arrays.asList(
+ HtmlConstants.BR,
+ HtmlConstants.AREA,
+ HtmlConstants.LINK,
+ HtmlConstants.IMG,
+ HtmlConstants.PARAM,
+ HtmlConstants.HR,
+ HtmlConstants.INPUT,
+ HtmlConstants.COL,
+ HtmlConstants.BASE,
+ HtmlConstants.META));
private Writer writer;
@@ -64,15 +62,14 @@
private String characterEncoding;
- private Stack<String> stack;
+ private Stack<String> stack;
/** use XML instead HMTL */
private boolean xml;
private boolean insideScriptOrStyle = false;
- private HtmlWriterUtil attributeWriter;
- private HtmlWriterUtil textWriter;
+ private HtmlWriterUtil helper;
public TobagoResponseWriter(final Writer writer, final String contentType,
final String characterEncoding) {
@@ -91,8 +88,7 @@
|| "text/xml".equals(contentType)) {
xml = true;
}
- attributeWriter = new HtmlWriterUtil(this, characterEncoding, true);
- textWriter = new HtmlWriterUtil(this, characterEncoding, false);
+ helper = new HtmlWriterUtil(writer, characterEncoding);
}
private String findValue(final Object value, final String property) {
@@ -131,6 +127,30 @@
writer.write(cbuf, off, len);
}
+ @Override
+ public void write(String string) throws IOException {
+ closeOpenTag();
+ writer.write(string);
+ }
+
+ @Override
+ public void write(int i) throws IOException {
+ closeOpenTag();
+ writer.write(i);
+ }
+
+ @Override
+ public void write(char[] chars) throws IOException {
+ closeOpenTag();
+ writer.write(chars);
+ }
+
+ @Override
+ public void write(String string, int i, int i1) throws IOException {
+ closeOpenTag();
+ writer.write(string, i, i1);
+ }
+
public void close() throws IOException {
closeOpenTag();
writer.close();
@@ -156,7 +176,7 @@
if (xml) {
write(XmlUtils.escape(value));
} else {
- textWriter.writeText(value);
+ helper.writeText(value);
}
}
}
@@ -178,10 +198,9 @@
writer.write(XmlUtils.escape(text.toString()).toCharArray(), offset, length);
// FIXME: not nice: XmlUtils.escape(text.toString()).toCharArray()
} else {
- textWriter.writeText(text, offset, length);
+ helper.writeText(text, offset, length);
}
}
-
}
public void startDocument() throws IOException {
@@ -327,6 +346,7 @@
throws IOException {
writeAttribute(name, value.toString(), escape);
}
+
public void writeAttribute(final String name, final String value, final boolean escape)
throws IOException {
if (!startStillOpen) {
@@ -347,7 +367,7 @@
writer.write(XmlUtils.escape(value));
} else {
if (escape && HtmlWriterUtil.attributeValueMustEscaped(name)) {
- attributeWriter.writeAttributeValue(value);
+ helper.writeAttributeValue(value);
} else {
writer.write(value);
}
@@ -379,7 +399,7 @@
writer.write(XmlUtils.escape(attribute));
} else {
if (escape && HtmlWriterUtil.attributeValueMustEscaped(name)) {
- attributeWriter.writeAttributeValue(attribute);
+ helper.writeAttributeValue(attribute);
} else {
writer.write(attribute);
}
Modified: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/util/HtmlWriterUtilUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/util/HtmlWriterUtilUnitTest.java?view=diff&rev=535887&r1=535886&r2=535887
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/util/HtmlWriterUtilUnitTest.java (original)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/util/HtmlWriterUtilUnitTest.java Mon May 7 07:56:03 2007
@@ -34,8 +34,8 @@
// HTML 4.0, section B.7.1: ampersands followed by
// an open brace don't get escaped
- public final static String[] rawTexts= {
- "oeffnende spitze klammern werden in attributen nicht escaped <tagname >",
+ public final static String[] RAW_TEXTS = {
+ "oeffnende spitze klammern werden in attributen doch escaped <tagname >",
"& followed by an { -> &{ don't get escaped in attributes",
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"\u00a0\u00a1\u00a2\u00a3\u00a4\u00a5\u00a6\u00a7\u00a8\u00a9\u00aa\u00ab\u00ac\u00ad\u00ae\u00af",
@@ -46,10 +46,10 @@
"\u00f0\u00f1\u00f2\u00f3\u00f4\u00f5\u00f6\u00f7\u00f8\u00f9\u00fa\u00fb\u00fc\u00fd\u00fe\u00ff"
};
- public final static String[] escapedTexts= {
- "oeffnende spitze klammern werden in attributen nicht escaped <tagname >",
+ public final static String[] ESCAPED_TEXTS = {
+ "oeffnende spitze klammern werden in attributen doch escaped <tagname >",
"& followed by an { -> &{ don't get escaped in attributes",
- rawTexts[2], // no escape needed
+ RAW_TEXTS[2], // no escape needed
" ¡¢£¤¥¦§¨©ª«¬­®¯",
"°±²³´µ¶·¸¹º»¼½¾¿",
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ",
@@ -58,23 +58,20 @@
"ðñòóôõö÷øùúûüýþÿ"
};
- public final static String[] escapedAttributes= {
- "oeffnende spitze klammern werden in attributen nicht escaped <tagname >",
+ public final static String[] ESCAPED_ATTRIBUTES = {
+ "oeffnende spitze klammern werden in attributen doch escaped <tagname >",
"& followed by an { -> &{ don't get escaped in attributes",
- rawTexts[2] // no escape needed
+ RAW_TEXTS[2] // no escape needed
};
-
-
public void test() {
CharArrayWriter writer = new CharArrayWriter();
- HtmlWriterUtil textUtil = new HtmlWriterUtil(writer, "", false);
- HtmlWriterUtil attributeUtil = new HtmlWriterUtil(writer, "", true);
+ HtmlWriterUtil helper = new HtmlWriterUtil(writer, "");
- for (int i = 0; i < escapedTexts.length; i++) {
- testText(textUtil, writer, rawTexts[i], escapedTexts[i]);
- if (i < escapedAttributes.length) {
- testAttributeValue(attributeUtil, writer, rawTexts[i], escapedAttributes[i]);
+ for (int i = 0; i < ESCAPED_TEXTS.length; i++) {
+ testText(helper, writer, RAW_TEXTS[i], ESCAPED_TEXTS[i]);
+ if (i < ESCAPED_ATTRIBUTES.length) {
+ testAttributeValue(helper, writer, RAW_TEXTS[i], ESCAPED_ATTRIBUTES[i]);
}
}
}
@@ -110,10 +107,10 @@
assertTrue(HtmlWriterUtil.attributeValueMustEscaped("i"));
assertTrue(HtmlWriterUtil.attributeValueMustEscaped("na"));
assertTrue(HtmlWriterUtil.attributeValueMustEscaped("cl"));
- assertTrue(!HtmlWriterUtil.attributeValueMustEscaped("id"));
- assertTrue(!HtmlWriterUtil.attributeValueMustEscaped("name"));
- assertTrue(!HtmlWriterUtil.attributeValueMustEscaped("class"));
- assertTrue(!!HtmlWriterUtil.attributeValueMustEscaped("dadfadfsadf"));
+ assertFalse(HtmlWriterUtil.attributeValueMustEscaped("id"));
+ assertFalse(HtmlWriterUtil.attributeValueMustEscaped("name"));
+ assertFalse(HtmlWriterUtil.attributeValueMustEscaped("class"));
+ assertTrue(HtmlWriterUtil.attributeValueMustEscaped("classs"));
+ assertTrue(HtmlWriterUtil.attributeValueMustEscaped("dadfadfsadf"));
}
-
}
Modified: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java?view=diff&rev=535887&r1=535886&r2=535887
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java (original)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java Mon May 7 07:56:03 2007
@@ -18,16 +18,12 @@
*/
import junit.framework.TestCase;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.io.StringWriter;
public class TobagoResponseWriterUnitTest extends TestCase {
- private static final Log LOG = LogFactory.getLog(TobagoResponseWriterUnitTest.class);
-
private StringWriter stringWriter;
private TobagoResponseWriter writer;
@@ -43,33 +39,78 @@
assertEquals("no content needed", "", stringWriter.toString());
}
- public void testElement() throws IOException {
- writer.startElement("test", null);
- writer.endElement("test");
- assertEquals("simple tag", "<test\n></test>", stringWriter.toString());
+ public void testEmptyTag() throws IOException {
+ writer.startElement("input", null);
+ writer.endElement("input");
+ assertEquals("empty tag", "<input\n>", stringWriter.toString());
+ }
+
+ public void testNormalTag() throws IOException {
+ writer.startElement("select", null);
+ writer.endElement("select");
+ assertEquals("normal tag", "<select\n></select>", stringWriter.toString());
}
public void testAttribute() throws IOException {
- writer.startElement("test", null);
- writer.endElement("test");
- assertEquals("simple tag", "<test\n></test>", stringWriter.toString());
+ writer.startElement("select", null);
+ writer.writeAttribute("value", "0", null);
+ writer.endElement("select");
+ assertEquals("attr tag", "<select value=\"0\"\n></select>", stringWriter.toString());
+ }
+
+ public void testAttributeQuoting() throws IOException {
+ writer.startElement("select", null);
+ writer.writeAttribute("value", "-<->-ü-â¬-", null);
+ writer.endElement("select");
+ assertEquals("attr tag", "<select value=\"-<->-ü-â¬-\"\n></select>", stringWriter.toString());
+ }
+
+ public void testTextQuoting() throws IOException {
+ writer.startElement("textarea", null);
+ writer.writeText("-<->-ü-â¬-", null);
+ writer.endElement("textarea");
+ assertEquals("attr tag", "<textarea\n>-<->-ü-â¬-</textarea>", stringWriter.toString());
+ }
+
+ public void testStringWriter() throws IOException {
+ stringWriter.write("-ü-â¬-");
+ assertEquals("-ü-â¬-", stringWriter.toString());
+ }
+
+ public void testManyChars() throws IOException {
+ writer.startElement("select", null);
+ StringBuffer buffer = new StringBuffer();
+ for (char c = 0x20; c < 0x1ff; c++) {
+ buffer.append(c);
+ }
+ writer.writeAttribute("value", buffer, null);
+ writer.writeText(buffer, null);
+ writer.endElement("select");
+
+ String result = buffer.toString(); // all the same but this 4 items
+ result = result.replace("&", "&");
+ result = result.replace("\"", """);
+ result = result.replace("<", "<");
+// result = result.replace(">", ">");
+ assertEquals("all chars", "<select value=\"" + result + "\"\n>" + result + "</select>", stringWriter.toString());
}
public void testNonUtf8() throws IOException {
TobagoResponseWriter writer1 = new TobagoResponseWriter(stringWriter, "", "ISO-8859-1");
writer1.startElement("input", null);
- writer1.writeAttribute("value", "Gutschein über 100 Euro.", null);
+ writer1.writeAttribute("value", "Gutschein über 100 â¬.", null);
writer1.writeAttribute("readonly", true);
writer1.endElement("input");
+ writer1.close();
+ assertEquals("<input value=\"Gutschein über 100 €.\" readonly=\"readonly\"\n>", stringWriter.toString());
}
public void testNoneScript() {
- assertTrue(!TobagoResponseWriter.isScriptOrStyle(null));
- assertTrue(!TobagoResponseWriter.isScriptOrStyle("s"));
- assertTrue(!TobagoResponseWriter.isScriptOrStyle("sc"));
+ assertFalse(TobagoResponseWriter.isScriptOrStyle(null));
+ assertFalse(TobagoResponseWriter.isScriptOrStyle("s"));
+ assertFalse(TobagoResponseWriter.isScriptOrStyle("sc"));
assertTrue(TobagoResponseWriter.isScriptOrStyle("script"));
assertTrue(TobagoResponseWriter.isScriptOrStyle("style"));
-
}
}