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.XmlUtils;
24  
25  import javax.faces.context.ResponseWriter;
26  import java.io.IOException;
27  import java.io.Writer;
28  import java.util.Arrays;
29  
30  public final class XmlResponseWriter extends TobagoResponseWriterBase {
31  
32    private static final String XHTML_DOCTYPE =
33        "<!DOCTYPE html      PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
34            + "     \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
35  
36    public XmlResponseWriter(
37        final Writer writer, final String contentType, final String characterEncoding) {
38      super(writer, contentType, characterEncoding);
39    }
40  
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    public void writeText(final char[] text, final int offset, final int length)
49        throws IOException {
50      closeOpenTag();
51      getWriter().write(XmlUtils.escape(text, offset, length, true));
52    }
53  
54    @Override
55    public void write(final char[] cbuf, final int off, final int len) throws IOException {
56      // Related to http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-696
57      if (Arrays.equals(cbuf, XML_VERSION_1_0_ENCODING_UTF_8_CHARS)) {
58        // drop
59      } else {
60        super.write(cbuf, off, len);
61      }
62    }
63  
64    public ResponseWriter cloneWithWriter(final Writer originalWriter) {
65      return new XmlResponseWriter(
66          originalWriter, getContentType(), getCharacterEncoding());
67    }
68  
69    @Override
70    public void closeEmptyTag() throws IOException {
71      getWriter().write("\n/>");
72    }
73  
74    @Override
75    protected void writerAttributeValue(final String value, final boolean escape) throws IOException {
76      getWriter().write(XmlUtils.escape(value));
77    }
78  
79    @Override
80    public void startDocument() throws IOException {
81      getWriter().write(XHTML_DOCTYPE);
82      getWriter().write('\n');
83      startElement(HtmlElements.HTML, null);
84      writeAttribute("xmlns", "http://www.w3.org/1999/xhtml", false);
85  
86    }
87  
88    @Override
89    public void endDocument() throws IOException {
90      endElement(HtmlElements.HTML);
91    }
92  }