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.util.XmlUtils;
23  
24  import javax.faces.context.ResponseWriter;
25  import java.io.IOException;
26  import java.io.Writer;
27  import java.util.Arrays;
28  
29  public final class XmlResponseWriter extends TobagoResponseWriterBase {
30  
31    private static final String XHTML_DOCTYPE =
32        "<!DOCTYPE html      PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
33            + "     \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
34  
35    public XmlResponseWriter(
36        final Writer writer, final String contentType, final String characterEncoding) {
37      super(writer, contentType, characterEncoding);
38    }
39  
40    @Override
41    public void writeText(final Object text, final String property)
42        throws IOException {
43      closeOpenTag();
44      final String value = findValue(text, property);
45      write(XmlUtils.escape(value));
46    }
47  
48    @Override
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 (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    @Override
66    public ResponseWriter cloneWithWriter(final Writer originalWriter) {
67      return new XmlResponseWriter(
68          originalWriter, getContentType(), getCharacterEncoding());
69    }
70  
71    @Override
72    public void closeEmptyTag() throws IOException {
73      getWriter().write("/>");
74    }
75  
76    @Override
77    protected void writerAttributeValue(final String value, final boolean escape) throws IOException {
78      getWriter().write(XmlUtils.escape(value));
79    }
80  
81    @Override
82    public void startDocument() throws IOException {
83  //    getWriter().write(XHTML_DOCTYPE);
84  //    getWriter().write('\n');
85  //    startElement(HtmlElements.HTML);
86  //    writeAttribute(HtmlAttributes.XMLNS, "http://www.w3.org/1999/xhtml", false);
87  //
88    }
89  
90    @Override
91    public void endDocument() throws IOException {
92  //    endElement(HtmlElements.HTML);
93    }
94  }