View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      // Related to http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-696
58      if (!FacesVersion.supports21() && Arrays.equals(cbuf, XML_VERSION_1_0_ENCODING_UTF_8_CHARS)) {
59        // drop
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  }