1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.internal.webapp;
21
22 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
23 import org.apache.myfaces.tobago.util.FacesVersion;
24 import org.apache.myfaces.tobago.util.XmlUtils;
25
26 import javax.faces.context.ResponseWriter;
27 import java.io.IOException;
28 import java.io.Writer;
29 import java.util.Arrays;
30
31 public final class XmlResponseWriter extends TobagoResponseWriterBase {
32
33 private static final String XHTML_DOCTYPE =
34 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
35 + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
36
37 public XmlResponseWriter(
38 Writer writer, String contentType, String characterEncoding) {
39 super(writer, contentType, characterEncoding);
40 }
41
42 public void writeText(final Object text, final String property)
43 throws IOException {
44 closeOpenTag();
45 final String value = findValue(text, property);
46 write(XmlUtils.escape(value));
47 }
48
49 public void writeText(final char[] text, final int offset, final int length)
50 throws IOException {
51 closeOpenTag();
52 getWriter().write(XmlUtils.escape(text, offset, length, true));
53 }
54
55 @Override
56 public void write(final char[] cbuf, final int off, final int len) throws IOException {
57
58 if (!FacesVersion.supports21() && Arrays.equals(cbuf, XML_VERSION_1_0_ENCODING_UTF_8_CHARS)) {
59
60 } else {
61 super.write(cbuf, off, len);
62 }
63 }
64
65 public ResponseWriter cloneWithWriter(final Writer originalWriter) {
66 return new XmlResponseWriter(
67 originalWriter, getContentType(), getCharacterEncoding());
68 }
69
70 @Override
71 public void closeEmptyTag() throws IOException {
72 getWriter().write("\n/>");
73 }
74
75 @Override
76 protected void writerAttributeValue(String value, boolean escape) throws IOException {
77 getWriter().write(XmlUtils.escape(value));
78 }
79
80 @Override
81 public void startDocument() throws IOException {
82 getWriter().write(XHTML_DOCTYPE);
83 getWriter().write('\n');
84 startElement(HtmlElements.HTML, null);
85 writeAttribute("xmlns", "http://www.w3.org/1999/xhtml", false);
86
87 }
88
89 @Override
90 public void endDocument() throws IOException {
91 endElement(HtmlElements.HTML);
92 }
93 }